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\ContainerInterface;
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
30 * @var ContainerInterface
40 public function boot()
45 * Shutdowns the Bundle.
47 public function shutdown()
54 * It is only ever called once when the cache is empty.
56 * This method can be overridden to register compilation passes,
57 * other extensions, ...
59 * @param ContainerBuilder $container A ContainerBuilder instance
61 public function build(ContainerBuilder $container)
68 * @param ContainerInterface|null $container A ContainerInterface instance or null
70 public function setContainer(ContainerInterface $container = null)
72 $this->container = $container;
76 * Returns the bundle's container extension.
78 * @return ExtensionInterface|null The container extension
80 * @throws \LogicException
82 public function getContainerExtension()
84 if (null === $this->extension) {
85 $extension = $this->createContainerExtension();
87 if (null !== $extension) {
88 if (!$extension instanceof ExtensionInterface) {
89 throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
92 // check naming convention
93 $basename = preg_replace('/Bundle$/', '', $this->getName());
94 $expectedAlias = Container::underscore($basename);
96 if ($expectedAlias != $extension->getAlias()) {
97 throw new \LogicException(sprintf(
98 '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.',
99 $expectedAlias, $extension->getAlias()
103 $this->extension = $extension;
105 $this->extension = false;
109 if ($this->extension) {
110 return $this->extension;
115 * Gets the Bundle namespace.
117 * @return string The Bundle namespace
119 public function getNamespace()
121 $class = get_class($this);
123 return substr($class, 0, strrpos($class, '\\'));
127 * Gets the Bundle directory path.
129 * @return string The Bundle absolute path
131 public function getPath()
133 if (null === $this->path) {
134 $reflected = new \ReflectionObject($this);
135 $this->path = dirname($reflected->getFileName());
142 * Returns the bundle parent name.
144 * @return string|null The Bundle parent name it overrides or null if no parent
146 public function getParent()
151 * Returns the bundle name (the class short name).
153 * @return string The Bundle name
155 final public function getName()
157 if (null !== $this->name) {
161 $name = get_class($this);
162 $pos = strrpos($name, '\\');
164 return $this->name = false === $pos ? $name : substr($name, $pos + 1);
168 * Finds and registers Commands.
170 * Override this method if your bundle commands do not follow the conventions:
172 * * Commands are in the 'Command' sub-directory
173 * * Commands extend Symfony\Component\Console\Command\Command
175 * @param Application $application An Application instance
177 public function registerCommands(Application $application)
179 if (!is_dir($dir = $this->getPath().'/Command')) {
183 if (!class_exists('Symfony\Component\Finder\Finder')) {
184 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
187 $finder = new Finder();
188 $finder->files()->name('*Command.php')->in($dir);
190 $prefix = $this->getNamespace().'\\Command';
191 foreach ($finder as $file) {
193 if ($relativePath = $file->getRelativePath()) {
194 $ns .= '\\'.str_replace('/', '\\', $relativePath);
196 $class = $ns.'\\'.$file->getBasename('.php');
197 if ($this->container) {
198 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
199 if ($this->container->has($alias)) {
203 $r = new \ReflectionClass($class);
204 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
205 $application->add($r->newInstance());
211 * Returns the bundle's container extension class.
215 protected function getContainerExtensionClass()
217 $basename = preg_replace('/Bundle$/', '', $this->getName());
219 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
223 * Creates the bundle's container extension.
225 * @return ExtensionInterface|null
227 protected function createContainerExtension()
229 if (class_exists($class = $this->getContainerExtensionClass())) {