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\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;
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()
46 public function shutdown()
53 * This method can be overridden to register compilation passes,
54 * other extensions, ...
56 public function build(ContainerBuilder $container)
61 * Returns the bundle's container extension.
63 * @return ExtensionInterface|null The container extension
65 * @throws \LogicException
67 public function getContainerExtension()
69 if (null === $this->extension) {
70 $extension = $this->createContainerExtension();
72 if (null !== $extension) {
73 if (!$extension instanceof ExtensionInterface) {
74 throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', \get_class($extension)));
77 // check naming convention
78 $basename = preg_replace('/Bundle$/', '', $this->getName());
79 $expectedAlias = Container::underscore($basename);
81 if ($expectedAlias != $extension->getAlias()) {
82 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()));
85 $this->extension = $extension;
87 $this->extension = false;
91 if ($this->extension) {
92 return $this->extension;
99 public function getNamespace()
101 if (null === $this->namespace) {
102 $this->parseClassName();
105 return $this->namespace;
111 public function getPath()
113 if (null === $this->path) {
114 $reflected = new \ReflectionObject($this);
115 $this->path = \dirname($reflected->getFileName());
124 public function getParent()
131 final public function getName()
133 if (null === $this->name) {
134 $this->parseClassName();
141 * Finds and registers Commands.
143 * Override this method if your bundle commands do not follow the conventions:
145 * * Commands are in the 'Command' sub-directory
146 * * Commands extend Symfony\Component\Console\Command\Command
148 public function registerCommands(Application $application)
150 if (!is_dir($dir = $this->getPath().'/Command')) {
154 if (!class_exists('Symfony\Component\Finder\Finder')) {
155 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
158 $finder = new Finder();
159 $finder->files()->name('*Command.php')->in($dir);
161 $prefix = $this->getNamespace().'\\Command';
162 foreach ($finder as $file) {
164 if ($relativePath = $file->getRelativePath()) {
165 $ns .= '\\'.str_replace('/', '\\', $relativePath);
167 $class = $ns.'\\'.$file->getBasename('.php');
168 if ($this->container) {
169 $commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : array();
170 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
171 if (isset($commandIds[$alias]) || $this->container->has($alias)) {
175 $r = new \ReflectionClass($class);
176 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
177 @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);
179 $application->add($r->newInstance());
185 * Returns the bundle's container extension class.
189 protected function getContainerExtensionClass()
191 $basename = preg_replace('/Bundle$/', '', $this->getName());
193 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
197 * Creates the bundle's container extension.
199 * @return ExtensionInterface|null
201 protected function createContainerExtension()
203 if (class_exists($class = $this->getContainerExtensionClass())) {
208 private function parseClassName()
210 $pos = strrpos(static::class, '\\');
211 $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
212 if (null === $this->name) {
213 $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);