comparison vendor/symfony/http-kernel/Bundle/Bundle.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\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;
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 * Boots the Bundle.
38 */
39 public function boot()
40 {
41 }
42
43 /**
44 * Shutdowns the Bundle.
45 */
46 public function shutdown()
47 {
48 }
49
50 /**
51 * Builds the bundle.
52 *
53 * It is only ever called once when the cache is empty.
54 *
55 * This method can be overridden to register compilation passes,
56 * other extensions, ...
57 *
58 * @param ContainerBuilder $container A ContainerBuilder instance
59 */
60 public function build(ContainerBuilder $container)
61 {
62 }
63
64 /**
65 * Returns the bundle's container extension.
66 *
67 * @return ExtensionInterface|null The container extension
68 *
69 * @throws \LogicException
70 */
71 public function getContainerExtension()
72 {
73 if (null === $this->extension) {
74 $extension = $this->createContainerExtension();
75
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)));
79 }
80
81 // check naming convention
82 $basename = preg_replace('/Bundle$/', '', $this->getName());
83 $expectedAlias = Container::underscore($basename);
84
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()
89 ));
90 }
91
92 $this->extension = $extension;
93 } else {
94 $this->extension = false;
95 }
96 }
97
98 if ($this->extension) {
99 return $this->extension;
100 }
101 }
102
103 /**
104 * Gets the Bundle namespace.
105 *
106 * @return string The Bundle namespace
107 */
108 public function getNamespace()
109 {
110 if (null === $this->namespace) {
111 $this->parseClassName();
112 }
113
114 return $this->namespace;
115 }
116
117 /**
118 * Gets the Bundle directory path.
119 *
120 * @return string The Bundle absolute path
121 */
122 public function getPath()
123 {
124 if (null === $this->path) {
125 $reflected = new \ReflectionObject($this);
126 $this->path = dirname($reflected->getFileName());
127 }
128
129 return $this->path;
130 }
131
132 /**
133 * Returns the bundle parent name.
134 *
135 * @return string|null The Bundle parent name it overrides or null if no parent
136 */
137 public function getParent()
138 {
139 }
140
141 /**
142 * Returns the bundle name (the class short name).
143 *
144 * @return string The Bundle name
145 */
146 final public function getName()
147 {
148 if (null === $this->name) {
149 $this->parseClassName();
150 }
151
152 return $this->name;
153 }
154
155 /**
156 * Finds and registers Commands.
157 *
158 * Override this method if your bundle commands do not follow the conventions:
159 *
160 * * Commands are in the 'Command' sub-directory
161 * * Commands extend Symfony\Component\Console\Command\Command
162 *
163 * @param Application $application An Application instance
164 */
165 public function registerCommands(Application $application)
166 {
167 if (!is_dir($dir = $this->getPath().'/Command')) {
168 return;
169 }
170
171 if (!class_exists('Symfony\Component\Finder\Finder')) {
172 throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
173 }
174
175 $finder = new Finder();
176 $finder->files()->name('*Command.php')->in($dir);
177
178 $prefix = $this->getNamespace().'\\Command';
179 foreach ($finder as $file) {
180 $ns = $prefix;
181 if ($relativePath = $file->getRelativePath()) {
182 $ns .= '\\'.str_replace('/', '\\', $relativePath);
183 }
184 $class = $ns.'\\'.$file->getBasename('.php');
185 if ($this->container) {
186 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
187 if ($this->container->has($alias)) {
188 continue;
189 }
190 }
191 $r = new \ReflectionClass($class);
192 if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
193 $application->add($r->newInstance());
194 }
195 }
196 }
197
198 /**
199 * Returns the bundle's container extension class.
200 *
201 * @return string
202 */
203 protected function getContainerExtensionClass()
204 {
205 $basename = preg_replace('/Bundle$/', '', $this->getName());
206
207 return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
208 }
209
210 /**
211 * Creates the bundle's container extension.
212 *
213 * @return ExtensionInterface|null
214 */
215 protected function createContainerExtension()
216 {
217 if (class_exists($class = $this->getContainerExtensionClass())) {
218 return new $class();
219 }
220 }
221
222 private function parseClassName()
223 {
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);
228 }
229 }
230 }