vendor/symfony/http-kernel/Bundle/Bundle.php line 132

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\Bundle;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\DependencyInjection\Container;
  13. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  16. /**
  17.  * An implementation of BundleInterface that adds a few conventions for DependencyInjection extensions.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. abstract class Bundle implements BundleInterface
  22. {
  23.     use ContainerAwareTrait;
  24.     protected $name;
  25.     protected $extension;
  26.     protected $path;
  27.     private string $namespace;
  28.     public function boot()
  29.     {
  30.     }
  31.     public function shutdown()
  32.     {
  33.     }
  34.     /**
  35.      * This method can be overridden to register compilation passes,
  36.      * other extensions, ...
  37.      */
  38.     public function build(ContainerBuilder $container)
  39.     {
  40.     }
  41.     /**
  42.      * Returns the bundle's container extension.
  43.      *
  44.      * @throws \LogicException
  45.      */
  46.     public function getContainerExtension(): ?ExtensionInterface
  47.     {
  48.         if (null === $this->extension) {
  49.             $extension $this->createContainerExtension();
  50.             if (null !== $extension) {
  51.                 if (!$extension instanceof ExtensionInterface) {
  52.                     throw new \LogicException(sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.'get_debug_type($extension)));
  53.                 }
  54.                 // check naming convention
  55.                 $basename preg_replace('/Bundle$/'''$this->getName());
  56.                 $expectedAlias Container::underscore($basename);
  57.                 if ($expectedAlias != $extension->getAlias()) {
  58.                     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()));
  59.                 }
  60.                 $this->extension $extension;
  61.             } else {
  62.                 $this->extension false;
  63.             }
  64.         }
  65.         return $this->extension ?: null;
  66.     }
  67.     public function getNamespace(): string
  68.     {
  69.         if (!isset($this->namespace)) {
  70.             $this->parseClassName();
  71.         }
  72.         return $this->namespace;
  73.     }
  74.     public function getPath(): string
  75.     {
  76.         if (null === $this->path) {
  77.             $reflected = new \ReflectionObject($this);
  78.             $this->path \dirname($reflected->getFileName());
  79.         }
  80.         return $this->path;
  81.     }
  82.     /**
  83.      * Returns the bundle name (the class short name).
  84.      */
  85.     final public function getName(): string
  86.     {
  87.         if (null === $this->name) {
  88.             $this->parseClassName();
  89.         }
  90.         return $this->name;
  91.     }
  92.     public function registerCommands(Application $application)
  93.     {
  94.     }
  95.     /**
  96.      * Returns the bundle's container extension class.
  97.      */
  98.     protected function getContainerExtensionClass(): string
  99.     {
  100.         $basename preg_replace('/Bundle$/'''$this->getName());
  101.         return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  102.     }
  103.     /**
  104.      * Creates the bundle's container extension.
  105.      */
  106.     protected function createContainerExtension(): ?ExtensionInterface
  107.     {
  108.         return class_exists($class $this->getContainerExtensionClass()) ? new $class() : null;
  109.     }
  110.     private function parseClassName()
  111.     {
  112.         $pos strrpos(static::class, '\\');
  113.         $this->namespace false === $pos '' substr(static::class, 0$pos);
  114.         $this->name ??= false === $pos ? static::class : substr(static::class, $pos 1);
  115.     }
  116. }