8000 [DependencyInjection] Fix dumping/loading errored definitions in XML/… · symfony/symfony@66a5f12 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66a5f12

Browse files
[DependencyInjection] Fix dumping/loading errored definitions in XML/Yaml
1 parent 033dace commit 66a5f12

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

src/Symfony/Component/DependencyInjection/Compiler/DefinitionErrorExceptionPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DefinitionErrorExceptionPass extends AbstractRecursivePass
2525
{
2626
protected function processValue(mixed $value, bool $isRoot = false): mixed
2727
{
28-
if (!$value instanceof Definition || !$value->hasErrors()) {
28+
if (!$value instanceof Definition || !$value->hasErrors() || $value->hasTag('container.error')) {
2929
return parent::processValue($value, $isRoot);
3030
}
3131

src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
129129
}
130130
}
131131

132-
foreach ($definition->getTags() as $name => $tags) {
132+
$tags = $definition->getTags();
133+
$tags['container.error'] = array_map(fn ($e) => ['message' => $e], $definition->getErrors());
134+
foreach ($tags as $name => $tags) {
133135
foreach ($tags as $attributes) {
134136
$tag = $this->document->createElement('tag');
135137
if (!\array_key_exists('name', $attributes)) {

src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ private function addService(string $id, Definition $definition): string
6969
}
7070

7171
$tagsCode = '';
72-
foreach ($definition->getTags() as $name => $tags) {
72+
$tags = $definition->getTags();
73+
$tags['container.error'] = array_map(fn ($e) => ['message' => $e], $definition->getErrors());
74+
foreach ($tags as $name => $tags) {
7375
foreach ($tags as $attributes) {
7476
$att = [];
7577
foreach ($attributes as $key => $value) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ protected function setDefinition(string $id, Definition $definition)
214214
{
215215
$this->container->removeBindings($id);
216216

217+
foreach ($definition->getTag('container.error') as $error) {
218+
$definition->addError($error['message']);
219+
}
220+
217221
if ($this->isLoadingInstanceof) {
218222
if (!$definition instanceof ChildDefinition) {
219223
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition)));

src/Symfony/Component/DependencyInjection/Tests/Compiler/DefinitionErrorExceptionPassTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ public function testNoExceptionThrown()
4949
$pass->process($container);
5050
$this->assertSame($def, $container->getDefinition('foo_service_id')->getArgument(0));
5151
}
52+
53+
public function testSkipErrorFromTag()
54+
{
55+
$container = new ContainerBuilder();
56+
$def = new Definition();
57+
$def->addError('Things went wrong!');
58+
$def->addTag('container.error');
59+
$container->register('foo_service_id')
60+
->setArguments([$def]);
61+
62+
$pass = new DefinitionErrorExceptionPass();
63+
$pass->process($container);
64+
$this->assertSame($def, $container->getDefinition('foo_service_id')->getArgument(0));
65+
}
5266
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@
145145
<service id="runtime_error" class="stdClass" public="true">
146146
<argument type="service" id="errored_definition"/>
147147
</service>
148-
<service id="errored_definition" class="stdClass"/>
148+
<service id="errored_definition" class="stdClass">
149+
<tag name="container.error" message="Service &quot;errored_definition&quot; is broken."/>
150+
</service>
149151
<service id="preload_sidekick" class="stdClass" public="true">
150152
<tag name="container.preload" class="Some\Sidekick1"/>
151153
<tag name="container.preload" class="Some\Sidekick2"/>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ services:
173173
public: true
174174
errored_definition:
175175
class: stdClass
176+
tags:
177+
- container.error: { message: 'Service "errored_definition" is broken.' }
176178
preload_sidekick:
177179
class: stdClass
178180
tags:

0 commit comments

Comments
 (0)
0