[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/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\DependencyInjection\ContainerAware;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  use Symfony\Component\DependencyInjection\Container;
  17  use Symfony\Component\Console\Application;
  18  use Symfony\Component\Finder\Finder;
  19  use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  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 extends ContainerAware implements BundleInterface
  28  {
  29      protected $name;
  30      protected $reflected;
  31      protected $extension;
  32  
  33      /**
  34       * Boots the Bundle.
  35       */
  36      public function boot()
  37      {
  38      }
  39  
  40      /**
  41       * Shutdowns the Bundle.
  42       */
  43      public function shutdown()
  44      {
  45      }
  46  
  47      /**
  48       * Builds the bundle.
  49       *
  50       * It is only ever called once when the cache is empty.
  51       *
  52       * This method can be overridden to register compilation passes,
  53       * other extensions, ...
  54       *
  55       * @param ContainerBuilder $container A ContainerBuilder instance
  56       */
  57      public function build(ContainerBuilder $container)
  58      {
  59      }
  60  
  61      /**
  62       * Returns the bundle's container extension.
  63       *
  64       * @return ExtensionInterface|null The container extension
  65       *
  66       * @throws \LogicException
  67       */
  68      public function getContainerExtension()
  69      {
  70          if (null === $this->extension) {
  71              $basename = preg_replace('/Bundle$/', '', $this->getName());
  72  
  73              $class = $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  74              if (class_exists($class)) {
  75                  $extension = new $class();
  76  
  77                  if (!$extension instanceof ExtensionInterface) {
  78                      throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', $class));
  79                  }
  80  
  81                  // check naming convention
  82                  $expectedAlias = Container::underscore($basename);
  83                  if ($expectedAlias != $extension->getAlias()) {
  84                      throw new \LogicException(sprintf(
  85                          'The extension alias for the default extension of a '.
  86                          'bundle must be the underscored version of the '.
  87                          'bundle name ("%s" instead of "%s")',
  88                          $expectedAlias, $extension->getAlias()
  89                      ));
  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       * Gets the Bundle namespace.
 105       *
 106       * @return string The Bundle namespace
 107       */
 108      public function getNamespace()
 109      {
 110          if (null === $this->reflected) {
 111              $this->reflected = new \ReflectionObject($this);
 112          }
 113  
 114          return $this->reflected->getNamespaceName();
 115      }
 116  
 117      /**
 118       * Gets the Bundle directory path.
 119       *
 120       * @return string The Bundle absolute path
 121       */
 122      public function getPath()
 123      {
 124          if (null === $this->reflected) {
 125              $this->reflected = new \ReflectionObject($this);
 126          }
 127  
 128          return dirname($this->reflected->getFileName());
 129      }
 130  
 131      /**
 132       * Returns the bundle parent name.
 133       *
 134       * @return string The Bundle parent name it overrides or null if no parent
 135       */
 136      public function getParent()
 137      {
 138      }
 139  
 140      /**
 141       * Returns the bundle name (the class short name).
 142       *
 143       * @return string The Bundle name
 144       */
 145      final public function getName()
 146      {
 147          if (null !== $this->name) {
 148              return $this->name;
 149          }
 150  
 151          $name = get_class($this);
 152          $pos = strrpos($name, '\\');
 153  
 154          return $this->name = false === $pos ? $name : substr($name, $pos + 1);
 155      }
 156  
 157      /**
 158       * Finds and registers Commands.
 159       *
 160       * Override this method if your bundle commands do not follow the conventions:
 161       *
 162       * * Commands are in the 'Command' sub-directory
 163       * * Commands extend Symfony\Component\Console\Command\Command
 164       *
 165       * @param Application $application An Application instance
 166       */
 167      public function registerCommands(Application $application)
 168      {
 169          if (!is_dir($dir = $this->getPath().'/Command')) {
 170              return;
 171          }
 172  
 173          if (!class_exists('Symfony\Component\Finder\Finder')) {
 174              throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
 175          }
 176  
 177          $finder = new Finder();
 178          $finder->files()->name('*Command.php')->in($dir);
 179  
 180          $prefix = $this->getNamespace().'\\Command';
 181          foreach ($finder as $file) {
 182              $ns = $prefix;
 183              if ($relativePath = $file->getRelativePath()) {
 184                  $ns .= '\\'.str_replace('/', '\\', $relativePath);
 185              }
 186              $r = new \ReflectionClass($ns.'\\'.$file->getBasename('.php'));
 187              if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
 188                  $application->add($r->newInstance());
 189              }
 190          }
 191      }
 192  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1