You are here

abstract class Bundle in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/http-kernel/Bundle/Bundle.php \Symfony\Component\HttpKernel\Bundle\Bundle
  2. 8 core/modules/views/src/Plugin/views/filter/Bundle.php \Drupal\views\Plugin\views\filter\Bundle
Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/Bundle/Bundle.php \Symfony\Component\HttpKernel\Bundle\Bundle

An implementation of BundleInterface that adds a few conventions for DependencyInjection extensions and Console commands.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of Bundle

5 files declare their use of Bundle
ExtensionAbsentBundle.php in vendor/symfony/http-kernel/Tests/Fixtures/ExtensionAbsentBundle/ExtensionAbsentBundle.php
ExtensionLoadedBundle.php in vendor/symfony/http-kernel/Tests/Fixtures/ExtensionLoadedBundle/ExtensionLoadedBundle.php
ExtensionNotValidBundle.php in vendor/symfony/http-kernel/Tests/Fixtures/ExtensionNotValidBundle/ExtensionNotValidBundle.php
ExtensionPresentBundle.php in vendor/symfony/http-kernel/Tests/Fixtures/ExtensionPresentBundle/ExtensionPresentBundle.php
FooBarBundle.php in vendor/symfony/http-kernel/Tests/Fixtures/FooBarBundle.php
12 string references to 'Bundle'
BundleConstraintValidatorTest::assertValidation in core/modules/system/src/Tests/Entity/BundleConstraintValidatorTest.php
Executes the BundleConstraintValidator test for a given bundle.
ConfigTranslationFieldListBuilder::buildHeader in core/modules/config_translation/src/Controller/ConfigTranslationFieldListBuilder.php
Builds the header row for the entity listing.
ConfigTranslationFieldListBuilder::getFilterLabels in core/modules/config_translation/src/Controller/ConfigTranslationFieldListBuilder.php
Provides user facing strings for the filter element.
core.data_types.schema.yml in core/config/schema/core.data_types.schema.yml
core/config/schema/core.data_types.schema.yml
core.entity.schema.yml in core/config/schema/core.entity.schema.yml
core/config/schema/core.entity.schema.yml

... See full list

File

vendor/symfony/http-kernel/Bundle/Bundle.php, line 27

Namespace

Symfony\Component\HttpKernel\Bundle
View source
abstract class Bundle extends ContainerAware implements BundleInterface {
  protected $name;
  protected $extension;
  protected $path;

  /**
   * Boots the Bundle.
   */
  public function boot() {
  }

  /**
   * Shutdowns the Bundle.
   */
  public function shutdown() {
  }

  /**
   * Builds the bundle.
   *
   * It is only ever called once when the cache is empty.
   *
   * This method can be overridden to register compilation passes,
   * other extensions, ...
   *
   * @param ContainerBuilder $container A ContainerBuilder instance
   */
  public function build(ContainerBuilder $container) {
  }

  /**
   * Returns the bundle's container extension.
   *
   * @return ExtensionInterface|null The container extension
   *
   * @throws \LogicException
   */
  public function getContainerExtension() {
    if (null === $this->extension) {
      $class = $this
        ->getContainerExtensionClass();
      if (class_exists($class)) {
        $extension = new $class();
        if (!$extension instanceof ExtensionInterface) {
          throw new \LogicException(sprintf('Extension %s must implement Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface.', $class));
        }

        // check naming convention
        $basename = preg_replace('/Bundle$/', '', $this
          ->getName());
        $expectedAlias = Container::underscore($basename);
        if ($expectedAlias != $extension
          ->getAlias()) {
          throw new \LogicException(sprintf('Users will expect the alias of the default extension of a bundle to be the underscored version of the bundle name ("%s"). You can override "Bundle::getContainerExtension()" if you want to use "%s" or another alias.', $expectedAlias, $extension
            ->getAlias()));
        }
        $this->extension = $extension;
      }
      else {
        $this->extension = false;
      }
    }
    if ($this->extension) {
      return $this->extension;
    }
  }

  /**
   * Gets the Bundle namespace.
   *
   * @return string The Bundle namespace
   */
  public function getNamespace() {
    $class = get_class($this);
    return substr($class, 0, strrpos($class, '\\'));
  }

  /**
   * Gets the Bundle directory path.
   *
   * @return string The Bundle absolute path
   */
  public function getPath() {
    if (null === $this->path) {
      $reflected = new \ReflectionObject($this);
      $this->path = dirname($reflected
        ->getFileName());
    }
    return $this->path;
  }

  /**
   * Returns the bundle parent name.
   *
   * @return string The Bundle parent name it overrides or null if no parent
   */
  public function getParent() {
  }

  /**
   * Returns the bundle name (the class short name).
   *
   * @return string The Bundle name
   */
  public final function getName() {
    if (null !== $this->name) {
      return $this->name;
    }
    $name = get_class($this);
    $pos = strrpos($name, '\\');
    return $this->name = false === $pos ? $name : substr($name, $pos + 1);
  }

  /**
   * Finds and registers Commands.
   *
   * Override this method if your bundle commands do not follow the conventions:
   *
   * * Commands are in the 'Command' sub-directory
   * * Commands extend Symfony\Component\Console\Command\Command
   *
   * @param Application $application An Application instance
   */
  public function registerCommands(Application $application) {
    if (!is_dir($dir = $this
      ->getPath() . '/Command')) {
      return;
    }
    $finder = new Finder();
    $finder
      ->files()
      ->name('*Command.php')
      ->in($dir);
    $prefix = $this
      ->getNamespace() . '\\Command';
    foreach ($finder as $file) {
      $ns = $prefix;
      if ($relativePath = $file
        ->getRelativePath()) {
        $ns .= '\\' . strtr($relativePath, '/', '\\');
      }
      $class = $ns . '\\' . $file
        ->getBasename('.php');
      if ($this->container) {
        $alias = 'console.command.' . strtolower(str_replace('\\', '_', $class));
        if ($this->container
          ->has($alias)) {
          continue;
        }
      }
      $r = new \ReflectionClass($class);
      if ($r
        ->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r
        ->isAbstract() && !$r
        ->getConstructor()
        ->getNumberOfRequiredParameters()) {
        $application
          ->add($r
          ->newInstance());
      }
    }
  }

  /**
   * Returns the bundle's container extension class.
   *
   * @return string
   */
  protected function getContainerExtensionClass() {
    $basename = preg_replace('/Bundle$/', '', $this
      ->getName());
    return $this
      ->getNamespace() . '\\DependencyInjection\\' . $basename . 'Extension';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Bundle::$extension protected property
Bundle::$name protected property
Bundle::$path protected property
Bundle::boot public function Boots the Bundle. Overrides BundleInterface::boot
Bundle::build public function Builds the bundle. Overrides BundleInterface::build
Bundle::getContainerExtension public function Returns the bundle's container extension. Overrides BundleInterface::getContainerExtension
Bundle::getContainerExtensionClass protected function Returns the bundle's container extension class.
Bundle::getName final public function Returns the bundle name (the class short name). Overrides BundleInterface::getName
Bundle::getNamespace public function Gets the Bundle namespace. Overrides BundleInterface::getNamespace
Bundle::getParent public function Returns the bundle parent name. Overrides BundleInterface::getParent
Bundle::getPath public function Gets the Bundle directory path. Overrides BundleInterface::getPath
Bundle::registerCommands public function Finds and registers Commands.
Bundle::shutdown public function Shutdowns the Bundle. Overrides BundleInterface::shutdown
ContainerAware::$container protected property
ContainerAware::setContainer public function Sets the Container associated with this Controller. Overrides ContainerAwareInterface::setContainer