123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
-
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Symfony\Component\HttpKernel\Bundle;
-
- use Symfony\Component\DependencyInjection\ContainerAwareTrait;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\Console\Application;
- use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
-
- /**
- * An implementation of BundleInterface that adds a few conventions
- * for DependencyInjection extensions and Console commands.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- abstract class Bundle implements BundleInterface
- {
- use ContainerAwareTrait;
-
- protected $name;
- protected $extension;
- protected $path;
- private $namespace;
-
- /**
- * Boots the Bundle.
- */
- public function boot()
- {
- }
-
- /**
- * Shutdowns the Bundle.
- */
- public function shutdown()
- {
- }
-
- /**
- * Builds the bundle.
- *
- * It is only ever called once when the cache is empty.
- *
- * This method can be overridden to register compilation passes,
- * other extensions, ...
- */
- public function build(ContainerBuilder $container)
- {
- }
-
- /**
- * Returns the bundle's container extension.
- *
- * @return ExtensionInterface|null The container extension
- *
- * @throws \LogicException
- */
- public function getContainerExtension()
- {
- if (null === $this->extension) {
- $extension = $this->createContainerExtension();
-
- if (null !== $extension) {
- if (!$extension instanceof ExtensionInterface) {
- throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
- }
-
- // check naming convention
- $basename = preg_replace('/Bundle$/', '', $this->getName());
- $expectedAlias = Container::underscore($basename);
-
- if ($expectedAlias != $extension->getAlias()) {
- 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()
- ));
- }
-
- $this->extension = $extension;
- } else {
- $this->extension = false;
- }
- }
-
- if ($this->extension) {
- return $this->extension;
- }
- }
-
- /**
- * Gets the Bundle namespace.
- *
- * @return string The Bundle namespace
- */
- public function getNamespace()
- {
- if (null === $this->namespace) {
- $this->parseClassName();
- }
-
- return $this->namespace;
- }
-
- /**
- * Gets the Bundle directory path.
- *
- * @return string The Bundle absolute path
- */
- public function getPath()
- {
- if (null === $this->path) {
- $reflected = new \ReflectionObject($this);
- $this->path = dirname($reflected->getFileName());
- }
-
- return $this->path;
- }
-
- /**
- * Returns the bundle name (the class short name).
- *
- * @return string The Bundle name
- */
- final public function getName()
- {
- if (null === $this->name) {
- $this->parseClassName();
- }
-
- return $this->name;
- }
-
- public function registerCommands(Application $application)
- {
- }
-
- /**
- * Returns the bundle's container extension class.
- *
- * @return string
- */
- protected function getContainerExtensionClass()
- {
- $basename = preg_replace('/Bundle$/', '', $this->getName());
-
- return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
- }
-
- /**
- * Creates the bundle's container extension.
- *
- * @return ExtensionInterface|null
- */
- protected function createContainerExtension()
- {
- if (class_exists($class = $this->getContainerExtensionClass())) {
- return new $class();
- }
- }
-
- private function parseClassName()
- {
- $pos = strrpos(static::class, '\\');
- $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
- if (null === $this->name) {
- $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
- }
- }
- }
|