Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-kernel / Bundle / Bundle.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Bundle;
13
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;
20
21 /**
22  * An implementation of BundleInterface that adds a few conventions
23  * for DependencyInjection extensions and Console commands.
24  *
25  * @author Fabien Potencier <fabien@symfony.com>
26  */
27 abstract class Bundle implements BundleInterface
28 {
29     /**
30      * @var ContainerInterface
31      */
32     protected $container;
33     protected $name;
34     protected $extension;
35     protected $path;
36
37     /**
38      * Boots the Bundle.
39      */
40     public function boot()
41     {
42     }
43
44     /**
45      * Shutdowns the Bundle.
46      */
47     public function shutdown()
48     {
49     }
50
51     /**
52      * Builds the bundle.
53      *
54      * It is only ever called once when the cache is empty.
55      *
56      * This method can be overridden to register compilation passes,
57      * other extensions, ...
58      *
59      * @param ContainerBuilder $container A ContainerBuilder instance
60      */
61     public function build(ContainerBuilder $container)
62     {
63     }
64
65     /**
66      * Sets the container.
67      *
68      * @param ContainerInterface|null $container A ContainerInterface instance or null
69      */
70     public function setContainer(ContainerInterface $container = null)
71     {
72         $this->container = $container;
73     }
74
75     /**
76      * Returns the bundle's container extension.
77      *
78      * @return ExtensionInterface|null The container extension
79      *
80      * @throws \LogicException
81      */
82     public function getContainerExtension()
83     {
84         if (null === $this->extension) {
85             $extension = $this->createContainerExtension();
86
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)));
90                 }
91
92                 // check naming convention
93                 $basename = preg_replace('/Bundle$/', '', $this->getName());
94                 $expectedAlias = Container::underscore($basename);
95
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()
100                     ));
101                 }
102
103                 $this->extension = $extension;
104             } else {
105                 $this->extension = false;
106             }
107         }
108
109         if ($this->extension) {
110             return $this->extension;
111         }
112     }
113
114     /**
115      * Gets the Bundle namespace.
116      *
117      * @return string The Bundle namespace
118      */
119     public function getNamespace()
120     {
121         $class = get_class($this);
122
123         return substr($class, 0, strrpos($class, '\\'));
124     }
125
126     /**
127      * Gets the Bundle directory path.
128      *
129      * @return string The Bundle absolute path
130      */
131     public function getPath()
132     {
133         if (null === $this->path) {
134             $reflected = new \ReflectionObject($this);
135             $this->path = dirname($reflected->getFileName());
136         }
137
138         return $this->path;
139     }
140
141     /**
142      * Returns the bundle parent name.
143      *
144      * @return string|null The Bundle parent name it overrides or null if no parent
145      */
146     public function getParent()
147     {
148     }
149
150     /**
151      * Returns the bundle name (the class short name).
152      *
153      * @return string The Bundle name
154      */
155     final public function getName()
156     {
157         if (null !== $this->name) {
158             return $this->name;
159         }
160
161         $name = get_class($this);
162         $pos = strrpos($name, '\\');
163
164         return $this->name = false === $pos ? $name : substr($name, $pos + 1);
165     }
166
167     /**
168      * Finds and registers Commands.
169      *
170      * Override this method if your bundle commands do not follow the conventions:
171      *
172      * * Commands are in the 'Command' sub-directory
173      * * Commands extend Symfony\Component\Console\Command\Command
174      *
175      * @param Application $application An Application instance
176      */
177     public function registerCommands(Application $application)
178     {
179         if (!is_dir($dir = $this->getPath().'/Command')) {
180             return;
181         }
182
183         if (!class_exists('Symfony\Component\Finder\Finder')) {
184             throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
185         }
186
187         $finder = new Finder();
188         $finder->files()->name('*Command.php')->in($dir);
189
190         $prefix = $this->getNamespace().'\\Command';
191         foreach ($finder as $file) {
192             $ns = $prefix;
193             if ($relativePath = $file->getRelativePath()) {
194                 $ns .= '\\'.str_replace('/', '\\', $relativePath);
195             }
196             $class = $ns.'\\'.$file->getBasename('.php');
197             if ($this->container) {
198                 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
199                 if ($this->container->has($alias)) {
200                     continue;
201                 }
202             }
203             $r = new \ReflectionClass($class);
204             if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
205                 $application->add($r->newInstance());
206             }
207         }
208     }
209
210     /**
211      * Returns the bundle's container extension class.
212      *
213      * @return string
214      */
215     protected function getContainerExtensionClass()
216     {
217         $basename = preg_replace('/Bundle$/', '', $this->getName());
218
219         return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
220     }
221
222     /**
223      * Creates the bundle's container extension.
224      *
225      * @return ExtensionInterface|null
226      */
227     protected function createContainerExtension()
228     {
229         if (class_exists($class = $this->getContainerExtensionClass())) {
230             return new $class();
231         }
232     }
233 }