| 148 |
lars |
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 |
|
|
|
20 |
/**
|
|
|
21 |
* An implementation of BundleInterface that adds a few conventions for DependencyInjection extensions.
|
|
|
22 |
*
|
|
|
23 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
24 |
*/
|
|
|
25 |
abstract class Bundle implements BundleInterface
|
|
|
26 |
{
|
|
|
27 |
use ContainerAwareTrait;
|
|
|
28 |
|
|
|
29 |
protected $name;
|
|
|
30 |
protected $extension;
|
|
|
31 |
protected $path;
|
|
|
32 |
private string $namespace;
|
|
|
33 |
|
|
|
34 |
public function boot()
|
|
|
35 |
{
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function shutdown()
|
|
|
39 |
{
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* This method can be overridden to register compilation passes,
|
|
|
44 |
* other extensions, ...
|
|
|
45 |
*/
|
|
|
46 |
public function build(ContainerBuilder $container)
|
|
|
47 |
{
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Returns the bundle's container extension.
|
|
|
52 |
*
|
|
|
53 |
* @throws \LogicException
|
|
|
54 |
*/
|
|
|
55 |
public function getContainerExtension(): ?ExtensionInterface
|
|
|
56 |
{
|
|
|
57 |
if (null === $this->extension) {
|
|
|
58 |
$extension = $this->createContainerExtension();
|
|
|
59 |
|
|
|
60 |
if (null !== $extension) {
|
|
|
61 |
if (!$extension instanceof ExtensionInterface) {
|
|
|
62 |
throw new \LogicException(sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_debug_type($extension)));
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// check naming convention
|
|
|
66 |
$basename = preg_replace('/Bundle$/', '', $this->getName());
|
|
|
67 |
$expectedAlias = Container::underscore($basename);
|
|
|
68 |
|
|
|
69 |
if ($expectedAlias != $extension->getAlias()) {
|
|
|
70 |
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()));
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$this->extension = $extension;
|
|
|
74 |
} else {
|
|
|
75 |
$this->extension = false;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $this->extension ?: null;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function getNamespace(): string
|
|
|
83 |
{
|
|
|
84 |
if (!isset($this->namespace)) {
|
|
|
85 |
$this->parseClassName();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return $this->namespace;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function getPath(): string
|
|
|
92 |
{
|
|
|
93 |
if (null === $this->path) {
|
|
|
94 |
$reflected = new \ReflectionObject($this);
|
|
|
95 |
$this->path = \dirname($reflected->getFileName());
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return $this->path;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Returns the bundle name (the class short name).
|
|
|
103 |
*/
|
|
|
104 |
final public function getName(): string
|
|
|
105 |
{
|
|
|
106 |
if (null === $this->name) {
|
|
|
107 |
$this->parseClassName();
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
return $this->name;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
public function registerCommands(Application $application)
|
|
|
114 |
{
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Returns the bundle's container extension class.
|
|
|
119 |
*/
|
|
|
120 |
protected function getContainerExtensionClass(): string
|
|
|
121 |
{
|
|
|
122 |
$basename = preg_replace('/Bundle$/', '', $this->getName());
|
|
|
123 |
|
|
|
124 |
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Creates the bundle's container extension.
|
|
|
129 |
*/
|
|
|
130 |
protected function createContainerExtension(): ?ExtensionInterface
|
|
|
131 |
{
|
|
|
132 |
return class_exists($class = $this->getContainerExtensionClass()) ? new $class() : null;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
private function parseClassName()
|
|
|
136 |
{
|
|
|
137 |
$pos = strrpos(static::class, '\\');
|
|
|
138 |
$this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
|
|
|
139 |
$this->name ??= false === $pos ? static::class : substr(static::class, $pos + 1);
|
|
|
140 |
}
|
|
|
141 |
}
|