Skip to content

Commit a09a0a4

Browse files
Merge branch '7.3' into 7.4
* 7.3: [DependencyInjection] Fix dealing with errored service definitions
2 parents 60dc806 + 6d83d99 commit a09a0a4

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

Compiler/CheckDefinitionValidityPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class CheckDefinitionValidityPass implements CompilerPassInterface
3838
public function process(ContainerBuilder $container): void
3939
{
4040
foreach ($container->getDefinitions() as $id => $definition) {
41+
if ($definition->hasErrors()) {
42+
continue;
43+
}
44+
4145
// synthetic service is public
4246
if ($definition->isSynthetic() && !$definition->isPublic()) {
4347
throw new RuntimeException(\sprintf('A synthetic service ("%s") must be public.', $id));

Compiler/ResolveClassPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function process(ContainerBuilder $container): void
2424
{
2525
foreach ($container->getDefinitions() as $id => $definition) {
2626
if ($definition->isSynthetic()
27+
|| $definition->hasErrors()
2728
|| null !== $definition->getClass()
2829
|| !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)
2930
) {

Loader/FileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ public function registerClasses(Definition $prototype, string $namespace, string
192192

193193
$abstract = $r?->isAbstract() || $r?->isInterface() ? '.abstract.' : '';
194194
$this->setDefinition($abstract.$class, $definition = $getPrototype());
195+
$definition->setClass($class);
195196
if (null !== $errorMessage) {
196197
$definition->addError($errorMessage);
197198

198199
continue;
199200
}
200-
$definition->setClass($class);
201201

202202
if ($abstract) {
203203
if ($r->isInterface()) {

Tests/Compiler/CheckDefinitionValidityPassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ public function testDynamicPrivateName()
156156
$this->addToAssertionCount(1);
157157
}
158158

159+
public function testProcessSkipsDefinitionsWithErrors()
160+
{
161+
$container = new ContainerBuilder();
162+
$definition = $container->register('a')->setSynthetic(true)->setPublic(false);
163+
$definition->addError('Some error message');
164+
165+
// This should not throw an exception because the definition has errors
166+
$this->process($container);
167+
168+
$this->addToAssertionCount(1);
169+
}
170+
159171
protected function process(ContainerBuilder $container)
160172
{
161173
$pass = new CheckDefinitionValidityPass();

Tests/Compiler/ResolveClassPassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public function testAmbiguousChildDefinition()
9595
(new ResolveClassPass())->process($container);
9696
}
9797

98+
public function testSkipsDefinitionsWithErrors()
99+
{
100+
$container = new ContainerBuilder();
101+
$def = $container->register('App\SomeClass');
102+
$def->addError('Some error message');
103+
104+
(new ResolveClassPass())->process($container);
105+
106+
// The class should not be set because the definition has errors
107+
$this->assertNull($def->getClass());
108+
}
109+
98110
#[IgnoreDeprecations]
99111
#[Group('legacy')]
100112
public function testInvalidClassNameDefinition()

Tests/Loader/FileLoaderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ public function testMissingParentClass()
226226
);
227227
}
228228

229+
public function testClassIsSetEvenWhenDefinitionHasErrors()
230+
{
231+
$container = new ContainerBuilder();
232+
$container->setParameter('bad_classes_dir', 'BadClasses');
233+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), 'test');
234+
235+
$loader->registerClasses(
236+
(new Definition())->setAutoconfigured(true),
237+
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\',
238+
'Prototype/%bad_classes_dir%/*'
239+
);
240+
241+
$definition = $container->getDefinition(MissingParent::class);
242+
243+
// The class should be set even when the definition has errors
244+
$this->assertSame(MissingParent::class, $definition->getClass());
245+
$this->assertNotEmpty($definition->getErrors());
246+
}
247+
229248
public function testRegisterClassesWithBadPrefix()
230249
{
231250
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)