f42895a733e8db7e55200b29b3097d2a3baa6969
[yaffs-website] / 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\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;
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     use ContainerAwareTrait;
30
31     protected $name;
32     protected $extension;
33     protected $path;
34     private $namespace;
35
36     /**
37      * {@inheritdoc}
38      */
39     public function boot()
40     {
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     public function shutdown()
47     {
48     }
49
50     /**
51      * {@inheritdoc}
52      *
53      * This method can be overridden to register compilation passes,
54      * other extensions, ...
55      */
56     public function build(ContainerBuilder $container)
57     {
58     }
59
60     /**
61      * Returns the bundle's container extension.
62      *
63      * @return ExtensionInterface|null The container extension
64      *
65      * @throws \LogicException
66      */
67     public function getContainerExtension()
68     {
69         if (null === $this->extension) {
70             $extension = $this->createContainerExtension();
71
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)));
75                 }
76
77                 // check naming convention
78                 $basename = preg_replace('/Bundle$/', '', $this->getName());
79                 $expectedAlias = Container::underscore($basename);
80
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()));
83                 }
84
85                 $this->extension = $extension;
86             } else {
87                 $this->extension = false;
88             }
89         }
90
91         if ($this->extension) {
92             return $this->extension;
93         }
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     public function getNamespace()
100     {
101         if (null === $this->namespace) {
102             $this->parseClassName();
103         }
104
105         return $this->namespace;
106     }
107
108     /**
109      * {@inheritdoc}
110      */
111     public function getPath()
112     {
113         if (null === $this->path) {
114             $reflected = new \ReflectionObject($this);
115             $this->path = \dirname($reflected->getFileName());
116         }
117
118         return $this->path;
119     }
120
121     /**
122      * {@inheritdoc}
123      */
124     public function getParent()
125     {
126     }
127
128     /**
129      * {@inheritdoc}
130      */
131     final public function getName()
132     {
133         if (null === $this->name) {
134             $this->parseClassName();
135         }
136
137         return $this->name;
138     }
139
140     /**
141      * Finds and registers Commands.
142      *
143      * Override this method if your bundle commands do not follow the conventions:
144      *
145      * * Commands are in the 'Command' sub-directory
146      * * Commands extend Symfony\Component\Console\Command\Command
147      */
148     public function registerCommands(Application $application)
149     {
150         if (!is_dir($dir = $this->getPath().'/Command')) {
151             return;
152         }
153
154         if (!class_exists('Symfony\Component\Finder\Finder')) {
155             throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
156         }
157
158         $finder = new Finder();
159         $finder->files()->name('*Command.php')->in($dir);
160
161         $prefix = $this->getNamespace().'\\Command';
162         foreach ($finder as $file) {
163             $ns = $prefix;
164             if ($relativePath = $file->getRelativePath()) {
165                 $ns .= '\\'.str_replace('/', '\\', $relativePath);
166             }
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)) {
172                     continue;
173                 }
174             }
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);
178
179                 $application->add($r->newInstance());
180             }
181         }
182     }
183
184     /**
185      * Returns the bundle's container extension class.
186      *
187      * @return string
188      */
189     protected function getContainerExtensionClass()
190     {
191         $basename = preg_replace('/Bundle$/', '', $this->getName());
192
193         return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
194     }
195
196     /**
197      * Creates the bundle's container extension.
198      *
199      * @return ExtensionInterface|null
200      */
201     protected function createContainerExtension()
202     {
203         if (class_exists($class = $this->getContainerExtensionClass())) {
204             return new $class();
205         }
206     }
207
208     private function parseClassName()
209     {
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);
214         }
215     }
216 }