-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Bundle.php
157 lines (131 loc) · 4.32 KB
/
Bundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Bundle;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
/**
* An implementation of BundleInterface that adds a few conventions for DependencyInjection extensions.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Bundle implements BundleInterface
{
protected string $name;
protected ExtensionInterface|false|null $extension = null;
protected string $path;
protected ?ContainerInterface $container;
private string $namespace;
/**
* @return void
*/
public function boot()
{
}
/**
* @return void
*/
public function shutdown()
{
}
/**
* This method can be overridden to register compilation passes,
* other extensions, ...
*
* @return void
*/
public function build(ContainerBuilder $container)
{
}
/**
* Returns the bundle's container extension.
*
* @throws \LogicException
*/
public function getContainerExtension(): ?ExtensionInterface
{
if (!isset($this->extension)) {
$extension = $this->createContainerExtension();
if (null !== $extension) {
if (!$extension instanceof ExtensionInterface) {
throw new \LogicException(sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_debug_type($extension)));
}
// check naming convention
$basename = preg_replace('/Bundle$/', '', $this->getName());
$expectedAlias = Container::underscore($basename);
if ($expectedAlias != $extension->getAlias()) {
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()));
}
$this->extension = $extension;
} else {
$this->extension = false;
}
}
return $this->extension ?: null;
}
public function getNamespace(): string
{
if (!isset($this->namespace)) {
$this->parseClassName();
}
return $this->namespace;
}
public function getPath(): string
{
if (!isset($this->path)) {
$reflected = new \ReflectionObject($this);
$this->path = \dirname($reflected->getFileName());
}
return $this->path;
}
/**
* Returns the bundle name (the class short name).
*/
final public function getName(): string
{
if (!isset($this->name)) {
$this->parseClassName();
}
return $this->name;
}
/**
* @return void
*/
public function registerCommands(Application $application)
{
}
/**
* Returns the bundle's container extension class.
*/
protected function getContainerExtensionClass(): string
{
$basename = preg_replace('/Bundle$/', '', $this->getName());
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
}
/**
* Creates the bundle's container extension.
*/
protected function createContainerExtension(): ?ExtensionInterface
{
return class_exists($class = $this->getContainerExtensionClass()) ? new $class() : null;
}
private function parseClassName(): void
{
$pos = strrpos(static::class, '\\');
$this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
$this->name ??= false === $pos ? static::class : substr(static::class, $pos + 1);
}
public function setContainer(?ContainerInterface $container): void
{
$this->container = $container;
}
}