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