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

Open in your IDE?
  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.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function boot()
  32.     {
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function shutdown()
  38.     {
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      *
  43.      * This method can be overridden to register compilation passes,
  44.      * other extensions, ...
  45.      */
  46.     public function build(ContainerBuilder $container)
  47.     {
  48.     }
  49.     /**
  50.      * Returns the bundle's container extension.
  51.      *
  52.      * @throws \LogicException
  53.      */
  54.     public function getContainerExtension(): ?ExtensionInterface
  55.     {
  56.         if (null === $this->extension) {
  57.             $extension $this->createContainerExtension();
  58.             if (null !== $extension) {
  59.                 if (!$extension instanceof ExtensionInterface) {
  60.                     throw new \LogicException(sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.'get_debug_type($extension)));
  61.                 }
  62.                 // check naming convention
  63.                 $basename preg_replace('/Bundle$/'''$this->getName());
  64.                 $expectedAlias Container::underscore($basename);
  65.                 if ($expectedAlias != $extension->getAlias()) {
  66.                     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()));
  67.                 }
  68.                 $this->extension $extension;
  69.             } else {
  70.                 $this->extension false;
  71.             }
  72.         }
  73.         return $this->extension ?: null;
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function getNamespace(): string
  79.     {
  80.         if (!isset($this->namespace)) {
  81.             $this->parseClassName();
  82.         }
  83.         return $this->namespace;
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public function getPath(): string
  89.     {
  90.         if (null === $this->path) {
  91.             $reflected = new \ReflectionObject($this);
  92.             $this->path \dirname($reflected->getFileName());
  93.         }
  94.         return $this->path;
  95.     }
  96.     /**
  97.      * Returns the bundle name (the class short name).
  98.      */
  99.     final public function getName(): string
  100.     {
  101.         if (null === $this->name) {
  102.             $this->parseClassName();
  103.         }
  104.         return $this->name;
  105.     }
  106.     public function registerCommands(Application $application)
  107.     {
  108.     }
  109.     /**
  110.      * Returns the bundle's container extension class.
  111.      */
  112.     protected function getContainerExtensionClass(): string
  113.     {
  114.         $basename preg_replace('/Bundle$/'''$this->getName());
  115.         return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  116.     }
  117.     /**
  118.      * Creates the bundle's container extension.
  119.      */
  120.     protected function createContainerExtension(): ?ExtensionInterface
  121.     {
  122.         return class_exists($class $this->getContainerExtensionClass()) ? new $class() : null;
  123.     }
  124.     private function parseClassName()
  125.     {
  126.         $pos strrpos(static::class, '\\');
  127.         $this->namespace false === $pos '' substr(static::class, 0$pos);
  128.         if (null === $this->name) {
  129.             $this->name false === $pos ? static::class : substr(static::class, $pos 1);
  130.         }
  131.     }
  132. }