4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Bundle;
14 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
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;
22 * An implementation of BundleInterface that adds a few conventions
23 * for DependencyInjection extensions and Console commands.
25 * @author Fabien Potencier <fabien@symfony.com>
27 abstract class Bundle implements BundleInterface
29 use ContainerAwareTrait;
39 public function boot()
44 * Shutdowns the Bundle.
46 public function shutdown()
53 * It is only ever called once when the cache is empty.
55 * This method can be overridden to register compilation passes,
56 * other extensions, ...
58 public function build(ContainerBuilder $container)
63 * Returns the bundle's container extension.
65 * @return ExtensionInterface|null The container extension
67 * @throws \LogicException
69 public function getContainerExtension()
71 if (null === $this->extension) {
72 $extension = $this->createContainerExtension();
74 if (null !== $extension) {
75 if (!$extension instanceof ExtensionInterface) {
76 throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
79 // check naming convention
80 $basename = preg_replace('/Bundle$/', '', $this->getName());
81 $expectedAlias = Container::underscore($basename);
83 if ($expectedAlias != $extension->getAlias()) {
84 throw new \LogicException(sprintf(
85 '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.',
86 $expectedAlias, $extension->getAlias()
90 $this->extension = $extension;
92 $this->extension = false;
96 if ($this->extension) {
97 return $this->extension;
102 * Gets the Bundle namespace.
104 * @return string The Bundle namespace
106 public function getNamespace()
108 if (null === $this->namespace) {
109 $this->parseClassName();
112 return $this->namespace;
116 * Gets the Bundle directory path.
118 * @return string The Bundle absolute path
120 public function getPath()
122 if (null === $this->path) {
123 $reflected = new \ReflectionObject($this);
124 $this->path = dirname($reflected->getFileName());
131 * Returns the bundle parent name.
133 * @return string|null The Bundle parent name it overrides or null if no parent
135 public function getParent()
140 * Returns the bundle name (the class short name).
142 * @return string The Bundle name
144 final public function getName()
146 if (null === $this->name) {
147 $this->parseClassName();
154 * Finds and registers Commands.
156 * Override this method if your bundle commands do not follow the conventions:
158 * * Commands are in the 'Command' sub-directory
159 * * Commands extend Symfony\Component\Console\Command\Command
161 public function registerCommands(Application $application)
163 if (!is_dir($dir = $this->getPath().'/Command')) {
167 if (!class_exists('Symfony\Component\Finder\Finder')) {
168 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
171 $finder = new Finder();
172 $finder->files()->name('*Command.php')->in($dir);
174 $prefix = $this->getNamespace().'\\Command';
175 foreach ($finder as $file) {
177 if ($relativePath = $file->getRelativePath()) {
178 $ns .= '\\'.str_replace('/', '\\', $relativePath);
180 $class = $ns.'\\'.$file->getBasename('.php');
181 if ($this->container) {
182 $commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : array();
183 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
184 if (isset($commandIds[$alias]) || $this->container->has($alias)) {
188 $r = new \ReflectionClass($class);
189 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
190 @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);
192 $application->add($r->newInstance());
198 * Returns the bundle's container extension class.
202 protected function getContainerExtensionClass()
204 $basename = preg_replace('/Bundle$/', '', $this->getName());
206 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
210 * Creates the bundle's container extension.
212 * @return ExtensionInterface|null
214 protected function createContainerExtension()
216 if (class_exists($class = $this->getContainerExtensionClass())) {
221 private function parseClassName()
223 $pos = strrpos(static::class, '\\');
224 $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
225 if (null === $this->name) {
226 $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);