Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fdbd4c1

Browse files
minor #49788 [DependencyInjection] Add container.excluded tag on classes autodiscovered but excluded (Tiriel)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Add container.excluded tag on classes autodiscovered but excluded Add container.excluded tag on classes autodiscovered but excluded by `#[Exclude]` or `#[When]` attributes | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #49764 | License | MIT | Doc PR | - As per ticket, adding `container.excluded` tag on classes autodiscovered but excluded by one of the two attributes. Commits ------- c1fa81d [DependencyInjection] Add container.excluded tag on classes autodiscovered but excluded
2 parents b6e9b28 + c1fa81d commit fdbd4c1

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function registerClasses(Definition $prototype, string $namespace, string
126126
if (null === $errorMessage && $autoconfigureAttributes) {
127127
$r = $this->container->getReflectionClass($class);
128128
if ($r->getAttributes(Exclude::class)[0] ?? null) {
129+
$this->addContainerExcludedTag($class, $source);
129130
continue;
130131
}
131132
if ($this->env) {
@@ -137,6 +138,7 @@ public function registerClasses(Definition $prototype, string $namespace, string
137138
}
138139
}
139140
if (null !== $attribute) {
141+
$this->addContainerExcludedTag($class, $source);
140142
continue;
141143
}
142144
}
@@ -291,18 +293,29 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
291293
}
292294

293295
if (null !== $prefixLen) {
294-
$attributes = null !== $source ? ['source' => sprintf('in "%s/%s"', basename(\dirname($source)), basename($source))] : [];
295-
296296
foreach ($excludePaths as $path => $_) {
297297
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, str_ends_with($path, '.php') ? -4 : null)), '\\');
298-
if (!$this->container->has($class)) {
299-
$this->container->register($class)
300-
->setAbstract(true)
301-
->addTag('container.excluded', $attributes);
302-
}
298+
$this->addContainerExcludedTag($class, $source);
303299
}
304300
}
305301

306302
return $classes;
307303
}
304+
305+
private function addContainerExcludedTag(string $class, ?string $source): void
306+
{
307+
if ($this->container->has($class)) {
308+
return;
309+
}
310+
311+
static $attributes = [];
312+
313+
if (null !== $source && !isset($attributes[$source])) {
314+
$attributes[$source] = ['source' => sprintf('in "%s/%s"', basename(\dirname($source)), basename($source))];
315+
}
316+
317+
$this->container->register($class)
318+
->setAbstract(true)
319+
->addTag('container.excluded', null !== $source ? $attributes[$source] : []);
320+
}
308321
}

src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function testRegisterClassesWithExcludeAttribute(bool $autoconfigure)
161161
'Utils/*',
162162
);
163163

164-
$this->assertSame(!$autoconfigure, $container->hasDefinition(NotAService::class));
164+
$this->assertSame($autoconfigure, $container->getDefinition(NotAService::class)->hasTag('container.excluded'));
165165
}
166166

167167
public function testRegisterClassesWithExcludeAsArray()
@@ -284,10 +284,10 @@ public static function excludeTrailingSlashConsistencyProvider(): iterable
284284
}
285285

286286
/**
287-
* @testWith ["prod", true]
288-
* ["dev", true]
289-
* ["bar", false]
290-
* [null, true]
287+
* @testWith ["prod", false]
288+
* ["dev", false]
289+
* ["bar", true]
290+
* [null, false]
291291
*/
292292
public function testRegisterClassesWithWhenEnv(?string $env, bool $expected)
293293
{
@@ -299,7 +299,7 @@ public function testRegisterClassesWithWhenEnv(?string $env, bool $expected)
299299
'Prototype/{Foo.php}'
300300
);
301301

302-
$this->assertSame($expected, $container->has(Foo::class));
302+
$this->assertSame($expected, $container->getDefinition(Foo::class)->hasTag('container.excluded'));
303303
}
304304

305305
/**

0 commit comments

Comments
 (0)