Updated to Drupal 8.5. Core Media not yet in use.
[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\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     public function build(ContainerBuilder $container)
59     {
60     }
61
62     /**
63      * Returns the bundle's container extension.
64      *
65      * @return ExtensionInterface|null The container extension
66      *
67      * @throws \LogicException
68      */
69     public function getContainerExtension()
70     {
71         if (null === $this->extension) {
72             $extension = $this->createContainerExtension();
73
74             if (null !== $extension) {
75                 if (!$extension instanceof ExtensionInterface) {
76                     throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
77                 }
78
79                 // check naming convention
80                 $basename = preg_replace('/Bundle$/', '', $this->getName());
81                 $expectedAlias = Container::underscore($basename);
82
83                 if ($expectedAlias != $extension->getAlias()) {
84                     throw new \LogicException(sprintf(
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.',
86                         $expectedAlias, $extension->getAlias()
87                     ));
88                 }
89
90                 $this->extension = $extension;
91             } else {
92                 $this->extension = false;
93             }
94         }
95
96         if ($this->extension) {
97             return $this->extension;
98         }
99     }
100
101     /**
102      * Gets the Bundle namespace.
103      *
104      * @return string The Bundle namespace
105      */
106     public function getNamespace()
107     {
108         if (null === $this->namespace) {
109             $this->parseClassName();
110         }
111
112         return $this->namespace;
113     }
114
115     /**
116      * Gets the Bundle directory path.
117      *
118      * @return string The Bundle absolute path
119      */
120     public function getPath()
121     {
122         if (null === $this->path) {
123             $reflected = new \ReflectionObject($this);
124             $this->path = dirname($reflected->getFileName());
125         }
126
127         return $this->path;
128     }
129
130     /**
131      * Returns the bundle parent name.
132      *
133      * @return string|null The Bundle parent name it overrides or null if no parent
134      */
135     public function getParent()
136     {
137     }
138
139     /**
140      * Returns the bundle name (the class short name).
141      *
142      * @return string The Bundle name
143      */
144     final public function getName()
145     {
146         if (null === $this->name) {
147             $this->parseClassName();
148         }
149
150         return $this->name;
151     }
152
153     /**
154      * Finds and registers Commands.
155      *
156      * Override this method if your bundle commands do not follow the conventions:
157      *
158      * * Commands are in the 'Command' sub-directory
159      * * Commands extend Symfony\Component\Console\Command\Command
160      */
161     public function registerCommands(Application $application)
162     {
163         if (!is_dir($dir = $this->getPath().'/Command')) {
164             return;
165         }
166
167         if (!class_exists('Symfony\Component\Finder\Finder')) {
168             throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
169         }
170
171         $finder = new Finder();
172         $finder->files()->name('*Command.php')->in($dir);
173
174         $prefix = $this->getNamespace().'\\Command';
175         foreach ($finder as $file) {
176             $ns = $prefix;
177             if ($relativePath = $file->getRelativePath()) {
178                 $ns .= '\\'.str_replace('/', '\\', $relativePath);
179             }
180             $class = $ns.'\\'.$file->getBasename('.php');
181             if ($this->container) {
182                 $commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : array();
183                 $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
184                 if (isset($commandIds[$alias]) || $this->container->has($alias)) {
185                     continue;
186                 }
187             }
188             $r = new \ReflectionClass($class);
189             if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
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);
191
192                 $application->add($r->newInstance());
193             }
194         }
195     }
196
197     /**
198      * Returns the bundle's container extension class.
199      *
200      * @return string
201      */
202     protected function getContainerExtensionClass()
203     {
204         $basename = preg_replace('/Bundle$/', '', $this->getName());
205
206         return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
207     }
208
209     /**
210      * Creates the bundle's container extension.
211      *
212      * @return ExtensionInterface|null
213      */
214     protected function createContainerExtension()
215     {
216         if (class_exists($class = $this->getContainerExtensionClass())) {
217             return new $class();
218         }
219     }
220
221     private function parseClassName()
222     {
223         $pos = strrpos(static::class, '\\');
224         $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
225         if (null === $this->name) {
226             $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
227         }
228     }
229 }