[ Index ]

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


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1