[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Bundle/ -> Bundle.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\Bundle;
  13  
  14  use Symfony\Component\Console\Application;
  15  use Symfony\Component\DependencyInjection\Container;
  16  use Symfony\Component\DependencyInjection\ContainerBuilder;
  17  use Symfony\Component\DependencyInjection\ContainerInterface;
  18  use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  19  use Symfony\Component\Finder\Finder;
  20  
  21  /**
  22   * An implementation of BundleInterface that adds a few conventions
  23   * for DependencyInjection extensions and Console commands.
  24   *
  25   * @author Fabien Potencier <fabien@symfony.com>
  26   */
  27  abstract class Bundle implements BundleInterface
  28  {
  29      /**
  30       * @var ContainerInterface
  31       */
  32      protected $container;
  33      protected $name;
  34      protected $extension;
  35      protected $path;
  36  
  37      /**
  38       * {@inheritdoc}
  39       */
  40      public function boot()
  41      {
  42      }
  43  
  44      /**
  45       * {@inheritdoc}
  46       */
  47      public function shutdown()
  48      {
  49      }
  50  
  51      /**
  52       * {@inheritdoc}
  53       *
  54       * This method can be overridden to register compilation passes,
  55       * other extensions, ...
  56       */
  57      public function build(ContainerBuilder $container)
  58      {
  59      }
  60  
  61      /**
  62       * {@inheritdoc}
  63       */
  64      public function setContainer(ContainerInterface $container = null)
  65      {
  66          $this->container = $container;
  67      }
  68  
  69      /**
  70       * {@inheritdoc}
  71       *
  72       * @throws \LogicException
  73       */
  74      public function getContainerExtension()
  75      {
  76          if (null === $this->extension) {
  77              $extension = $this->createContainerExtension();
  78  
  79              if (null !== $extension) {
  80                  if (!$extension instanceof ExtensionInterface) {
  81                      throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', \get_class($extension)));
  82                  }
  83  
  84                  // check naming convention
  85                  $basename = preg_replace('/Bundle$/', '', $this->getName());
  86                  $expectedAlias = Container::underscore($basename);
  87  
  88                  if ($expectedAlias != $extension->getAlias()) {
  89                      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()));
  90                  }
  91  
  92                  $this->extension = $extension;
  93              } else {
  94                  $this->extension = false;
  95              }
  96          }
  97  
  98          if ($this->extension) {
  99              return $this->extension;
 100          }
 101      }
 102  
 103      /**
 104       * {@inheritdoc}
 105       */
 106      public function getNamespace()
 107      {
 108          $class = \get_class($this);
 109  
 110          return substr($class, 0, strrpos($class, '\\'));
 111      }
 112  
 113      /**
 114       * {@inheritdoc}
 115       */
 116      public function getPath()
 117      {
 118          if (null === $this->path) {
 119              $reflected = new \ReflectionObject($this);
 120              $this->path = \dirname($reflected->getFileName());
 121          }
 122  
 123          return $this->path;
 124      }
 125  
 126      /**
 127       * {@inheritdoc}
 128       */
 129      public function getParent()
 130      {
 131      }
 132  
 133      /**
 134       * {@inheritdoc}
 135       */
 136      final public function getName()
 137      {
 138          if (null !== $this->name) {
 139              return $this->name;
 140          }
 141  
 142          $name = \get_class($this);
 143          $pos = strrpos($name, '\\');
 144  
 145          return $this->name = false === $pos ? $name : substr($name, $pos + 1);
 146      }
 147  
 148      /**
 149       * Finds and registers Commands.
 150       *
 151       * Override this method if your bundle commands do not follow the conventions:
 152       *
 153       * * Commands are in the 'Command' sub-directory
 154       * * Commands extend Symfony\Component\Console\Command\Command
 155       */
 156      public function registerCommands(Application $application)
 157      {
 158          if (!is_dir($dir = $this->getPath().'/Command')) {
 159              return;
 160          }
 161  
 162          if (!class_exists('Symfony\Component\Finder\Finder')) {
 163              throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
 164          }
 165  
 166          $finder = new Finder();
 167          $finder->files()->name('*Command.php')->in($dir);
 168  
 169          $prefix = $this->getNamespace().'\\Command';
 170          foreach ($finder as $file) {
 171              $ns = $prefix;
 172              if ($relativePath = $file->getRelativePath()) {
 173                  $ns .= '\\'.str_replace('/', '\\', $relativePath);
 174              }
 175              $class = $ns.'\\'.$file->getBasename('.php');
 176              if ($this->container) {
 177                  $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
 178                  if ($this->container->has($alias)) {
 179                      continue;
 180                  }
 181              }
 182              $r = new \ReflectionClass($class);
 183              if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
 184                  $application->add($r->newInstance());
 185              }
 186          }
 187      }
 188  
 189      /**
 190       * Returns the bundle's container extension class.
 191       *
 192       * @return string
 193       */
 194      protected function getContainerExtensionClass()
 195      {
 196          $basename = preg_replace('/Bundle$/', '', $this->getName());
 197  
 198          return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
 199      }
 200  
 201      /**
 202       * Creates the bundle's container extension.
 203       *
 204       * @return ExtensionInterface|null
 205       */
 206      protected function createContainerExtension()
 207      {
 208          if (class_exists($class = $this->getContainerExtensionClass())) {
 209              return new $class();
 210          }
 211      }
 212  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1