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