-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
FileLoader.php
347 lines (299 loc) · 14.8 KB
/
FileLoader.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?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\DependencyInjection\Loader;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\Exception\LoaderLoadException;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
/**
* FileLoader is the abstract class used by all built-in loaders that are file based.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class FileLoader extends BaseFileLoader
{
public const ANONYMOUS_ID_REGEXP = '/^\.\d+_[^~]*+~[._a-zA-Z\d]{7}$/';
protected $container;
protected $isLoadingInstanceof = false;
protected $instanceof = [];
protected $interfaces = [];
protected $singlyImplemented = [];
/** @var array<string, Alias> */
protected $aliases = [];
protected $autoRegisterAliasesForSinglyImplementedInterfaces = true;
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null)
{
$this->container = $container;
parent::__construct($locator, $env);
}
/**
* @param bool|string $ignoreErrors Whether errors should be ignored; pass "not_found" to ignore only when the loaded resource is not found
*/
public function import(mixed $resource, string $type = null, bool|string $ignoreErrors = false, string $sourceResource = null, $exclude = null): mixed
{
$args = \func_get_args();
if ($ignoreNotFound = 'not_found' === $ignoreErrors) {
$args[2] = false;
} elseif (!\is_bool($ignoreErrors)) {
throw new \TypeError(sprintf('Invalid argument $ignoreErrors provided to "%s::import()": boolean or "not_found" expected, "%s" given.', static::class, get_debug_type($ignoreErrors)));
}
try {
return parent::import(...$args);
} catch (LoaderLoadException $e) {
if (!$ignoreNotFound || !($prev = $e->getPrevious()) instanceof FileLocatorFileNotFoundException) {
throw $e;
}
foreach ($prev->getTrace() as $frame) {
if ('import' === ($frame['function'] ?? null) && is_a($frame['class'] ?? '', Loader::class, true)) {
break;
}
}
if (__FILE__ !== $frame['file']) {
throw $e;
}
}
return null;
}
/**
* Registers a set of classes as services using PSR-4 for discovery.
*
* @param Definition $prototype A definition to use as template
* @param string $namespace The namespace prefix of classes in the scanned directory
* @param string $resource The directory to look for classes, glob-patterns allowed
* @param string|string[]|null $exclude A globbed path of files to exclude or an array of globbed paths of files to exclude
* @param string|null $source The path to the file that defines the auto-discovery rule
*
* @return void
*/
public function registerClasses(Definition $prototype, string $namespace, string $resource, string|array $exclude = null/* , string $source = null */)
{
if (!str_ends_with($namespace, '\\')) {
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
}
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace));
}
// This can happen with YAML files
if (\is_array($exclude) && \in_array(null, $exclude, true)) {
throw new InvalidArgumentException('The exclude list must not contain a "null" value.');
}
// This can happen with XML files
if (\is_array($exclude) && \in_array('', $exclude, true)) {
throw new InvalidArgumentException('The exclude list must not contain an empty value.');
}
$source = \func_num_args() > 4 ? func_get_arg(4) : null;
$autoconfigureAttributes = new RegisterAutoconfigureAttributesPass();
$autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null;
$classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes, $source);
$getPrototype = static fn () => clone $prototype;
$serialized = serialize($prototype);
// avoid deep cloning if no definitions are nested
if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"', 55)
|| strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"', 55)
) {
// prepare for deep cloning
foreach (['Arguments', 'Properties', 'MethodCalls', 'Configurator', 'Factory', 'Bindings'] as $key) {
$serialized = serialize($prototype->{'get'.$key}());
if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"')
|| strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"')
) {
$getPrototype = static fn () => $getPrototype()->{'set'.$key}(unserialize($serialized));
}
}
}
unset($serialized);
foreach ($classes as $class => $errorMessage) {
if (null === $errorMessage && $autoconfigureAttributes) {
$r = $this->container->getReflectionClass($class);
if ($r->getAttributes(Exclude::class)[0] ?? null) {
$this->addContainerExcludedTag($class, $source);
continue;
}
if ($this->env) {
$attribute = null;
foreach ($r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
$attribute = null;
break;
}
}
if (null !== $attribute) {
$this->addContainerExcludedTag($class, $source);
continue;
}
}
}
if (interface_exists($class, false)) {
$this->interfaces[] = $class;
} else {
$this->setDefinition($class, $definition = $getPrototype());
if (null !== $errorMessage) {
$definition->addError($errorMessage);
continue;
}
$definition->setClass($class);
$interfaces = [];
foreach (class_implements($class, false) as $interface) {
$this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
$interfaces[] = $interface;
}
if (!$autoconfigureAttributes) {
continue;
}
$r = $this->container->getReflectionClass($class);
$defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
foreach ($r->getAttributes(AsAlias::class) as $attr) {
/** @var AsAlias $attribute */
$attribute = $attr->newInstance();
$alias = $attribute->id ?? $defaultAlias;
$public = $attribute->public;
if (null === $alias) {
throw new LogicException(sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class));
}
if (isset($this->aliases[$alias])) {
throw new LogicException(sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
}
$this->aliases[$alias] = new Alias($class, $public);
}
}
}
foreach ($this->aliases as $alias => $aliasDefinition) {
$this->container->setAlias($alias, $aliasDefinition);
}
if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
$this->registerAliasesForSinglyImplementedInterfaces();
}
}
/**
* @return void
*/
public function registerAliasesForSinglyImplementedInterfaces()
{
foreach ($this->interfaces as $interface) {
if (!empty($this->singlyImplemented[$interface]) && !isset($this->aliases[$interface]) && !$this->container->has($interface)) {
$this->container->setAlias($interface, $this->singlyImplemented[$interface]);
}
}
$this->interfaces = $this->singlyImplemented = $this->aliases = [];
}
/**
* Registers a definition in the container with its instanceof-conditionals.
*
* @return void
*/
protected function setDefinition(string $id, Definition $definition)
{
$this->container->removeBindings($id);
foreach ($definition->getTag('container.error') as $error) {
if (isset($error['message'])) {
$definition->addError($error['message']);
}
}
if ($this->isLoadingInstanceof) {
if (!$definition instanceof ChildDefinition) {
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition)));
}
$this->instanceof[$id] = $definition;
} else {
$this->container->setDefinition($id, $definition->setInstanceofConditionals($this->instanceof));
}
}
private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes, ?string $source): array
{
$parameterBag = $this->container->getParameterBag();
$excludePaths = [];
$excludePrefix = null;
$excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
foreach ($excludePatterns as $excludePattern) {
foreach ($this->glob($excludePattern, true, $resource, true, true) as $path => $info) {
$excludePrefix ??= $resource->getPrefix();
// normalize Windows slashes and remove trailing slashes
$excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
}
}
$pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
$classes = [];
$prefixLen = null;
foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) {
if (null === $prefixLen) {
$prefixLen = \strlen($resource->getPrefix());
if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) {
throw new InvalidArgumentException(sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s).', $namespace, $excludePattern, $pattern));
}
}
if (isset($excludePaths[str_replace('\\', '/', $path)])) {
continue;
}
if (!str_ends_with($path, '.php')) {
continue;
}
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -4)), '\\');
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
continue;
}
try {
$r = $this->container->getReflectionClass($class);
} catch (\ReflectionException $e) {
$classes[$class] = $e->getMessage();
continue;
}
// check to make sure the expected class exists
if (!$r) {
throw new InvalidArgumentException(sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern));
}
if ($r->isInstantiable() || $r->isInterface()) {
$classes[$class] = null;
}
if ($autoconfigureAttributes && !$r->isInstantiable()) {
$autoconfigureAttributes->processClass($this->container, $r);
}
}
// track only for new & removed files
if ($resource instanceof GlobResource) {
$this->container->addResource($resource);
} else {
foreach ($resource as $path) {
$this->container->fileExists($path, false);
}
}
if (null !== $prefixLen) {
foreach ($excludePaths as $path => $_) {
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, str_ends_with($path, '.php') ? -4 : null)), '\\');
$this->addContainerExcludedTag($class, $source);
}
}
return $classes;
}
private function addContainerExcludedTag(string $class, ?string $source): void
{
if ($this->container->has($class)) {
return;
}
static $attributes = [];
if (null !== $source && !isset($attributes[$source])) {
$attributes[$source] = ['source' => sprintf('in "%s/%s"', basename(\dirname($source)), basename($source))];
}
$this->container->register($class, $class)
->setAbstract(true)
->addTag('container.excluded', null !== $source ? $attributes[$source] : []);
}
}