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 * @param ContainerBuilder $container A ContainerBuilder instance
60 public function build(ContainerBuilder $container)
65 * Returns the bundle's container extension.
67 * @return ExtensionInterface|null The container extension
69 * @throws \LogicException
71 public function getContainerExtension()
73 if (null === $this->extension) {
74 $extension = $this->createContainerExtension();
76 if (null !== $extension) {
77 if (!$extension instanceof ExtensionInterface) {
78 throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
81 // check naming convention
82 $basename = preg_replace('/Bundle$/', '', $this->getName());
83 $expectedAlias = Container::underscore($basename);
85 if ($expectedAlias != $extension->getAlias()) {
86 throw new \LogicException(sprintf(
87 '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.',
88 $expectedAlias, $extension->getAlias()
92 $this->extension = $extension;
94 $this->extension = false;
98 if ($this->extension) {
99 return $this->extension;
104 * Gets the Bundle namespace.
106 * @return string The Bundle namespace
108 public function getNamespace()
110 if (null === $this->namespace) {
111 $this->parseClassName();
114 return $this->namespace;
118 * Gets the Bundle directory path.
120 * @return string The Bundle absolute path
122 public function getPath()
124 if (null === $this->path) {
125 $reflected = new \ReflectionObject($this);
126 $this->path = dirname($reflected->getFileName());
133 * Returns the bundle parent name.
135 * @return string|null The Bundle parent name it overrides or null if no parent
137 public function getParent()
142 * Returns the bundle name (the class short name).
144 * @return string The Bundle name
146 final public function getName()
148 if (null === $this->name) {
149 $this->parseClassName();
156 * Finds and registers Commands.
158 * Override this method if your bundle commands do not follow the conventions:
160 * * Commands are in the 'Command' sub-directory
161 * * Commands extend Symfony\Component\Console\Command\Command
163 * @param Application $application An Application instance
165 public function registerCommands(Application $application)
167 if (!is_dir($dir = $this->getPath().'/Command')) {
171 if (!class_exists('Symfony\Component\Finder\Finder')) {
172 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
175 $finder = new Finder();
176 $finder->files()->name('*Command.php')->in($dir);
178 $prefix = $this->getNamespace().'\\Command';
179 foreach ($finder as $file) {
181 if ($relativePath = $file->getRelativePath()) {
182 $ns .= '\\'.str_replace('/', '\\', $relativePath);
184 $class = $ns.'\\'.$file->getBasename('.php');
185 if ($this->container) {
186 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
187 if ($this->container->has($alias)) {
191 $r = new \ReflectionClass($class);
192 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
193 $application->add($r->newInstance());
199 * Returns the bundle's container extension class.
203 protected function getContainerExtensionClass()
205 $basename = preg_replace('/Bundle$/', '', $this->getName());
207 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
211 * Creates the bundle's container extension.
213 * @return ExtensionInterface|null
215 protected function createContainerExtension()
217 if (class_exists($class = $this->getContainerExtensionClass())) {
222 private function parseClassName()
224 $pos = strrpos(static::class, '\\');
225 $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
226 if (null === $this->name) {
227 $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);