8000 [DependencyInjection] Fix dumping/loading errored definitions in XML/Yaml by nicolas-grekas · Pull Request #50260 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Fix dumping/loading errored definitions in XML/Yaml #50260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ private function getContainerBuilder(): ContainerBuilder
return $this->buildContainer();
}, $kernel, $kernel::class);
$container = $buildContainer();

$skippedIds = [];
} else {
if (!$kernelContainer instanceof Container) {
throw new RuntimeException(sprintf('This command does not support the application container: "%s" does not extend "%s".', get_debug_type($kernelContainer), Container::class));
Expand All @@ -100,13 +98,6 @@ private function getContainerBuilder(): ContainerBuilder
$refl = new \ReflectionProperty($parameterBag, 'resolved');
$refl->setValue($parameterBag, true);

$skippedIds = [];
foreach ($container->getServiceIds() as $serviceId) {
if (str_starts_with($serviceId, '.errored.')) {
$skippedIds[$serviceId] = true;
}
}

$container->getCompilerPassConfig()->setBeforeOptimizationPasses([]);
$container->getCompilerPassConfig()->setOptimizationPasses([]);
$container->getCompilerPassConfig()->setBeforeRemovingPasses([]);
Expand All @@ -115,7 +106,7 @@ private function getContainerBuilder(): ContainerBuilder
$container->setParameter('container.build_hash', 'lint_container');
$container->setParameter('container.build_id', 'lint_container');

$container->addCompilerPass(new CheckTypeDeclarationsPass(true, $skippedIds), PassConfig::TYPE_AFTER_REMOVING, -100);
$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);

return $this->containerBuilder = $container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,12 @@ public function load(array $configs, ContainerBuilder $container)

$this->registerSerializerConfiguration($config['serializer'], $container, $loader);
} else {
$container->register('.argument_resolver.request_payload.no_seria 10000 lizer', Serializer::class)
$container->getDefinition('argument_resolver.request_payload')
->setArguments([])
->addError('You can neither use "#[MapRequestPayload]" nor "#[MapQueryString]" since the Serializer component is not '
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
);

$container->getDefinition('argument_resolver.request_payload')
->replaceArgument(0, new Reference('.argument_resolver.request_payload.no_serializer', ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE))
)
->addTag('container.error')
->clearTag('kernel.event_subscriber');

$container->removeDefinition('console.command.serializer_debug');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ext-xml": "*",
"symfony/cache": "^5.4|^6.0",
"symfony/config": "^6.1",
"symfony/dependency-injection": "^6.2.8",
"symfony/dependency-injection": "^6.3",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/error-handler": "^6.1",
"symfony/event-dispatcher": "^5.4|^6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefinitionErrorExceptionPass extends AbstractRecursivePass
{
protected function processValue(mixed $value, bool $isRoot = false): mixed
{
if (!$value instanceof Definition || !$value->hasErrors()) {
if (!$value instanceof Definition || !$value->hasErrors() || $value->hasTag('container.error')) {
return parent::processValue($value, $isRoot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
}
}

foreach ($definition->getTags() as $name => $tags) {
$tags = $definition->getTags();
$tags['container.error'] = array_map(fn ($e) => ['message' => $e], $definition->getErrors());
foreach ($tags as $name => $tags) {
foreach ($tags as $attributes) {
$tag = $this->document->createElement('tag');
if (!\array_key_exists('name', $attributes)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ private function addService(string $id, Definition $definition): string
}

$tagsCode = '';
foreach ($definition->getTags() as $name => $tags) {
$tags = $definition->getTags();
$tags['container.error'] = array_map(fn ($e) => ['message' => $e], $definition->getErrors());
foreach ($tags as $name => $tags) {
foreach ($tags as $attributes) {
$att = [];
foreach ($attributes as $key => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ public function testNoExceptionThrown()
$pass->process($container);
$this->assertSame($def, $container->getDefinition('foo_service_id')->getArgument(0));
}

public function testSkipErrorFromTag()
{
$container = new ContainerBuilder();
$def = new Definition();
$def->addError('Things went wrong!');
$def->addTag('container.error');
$container->register('foo_service_id')
->setArguments([$def]);

$pass = new DefinitionErrorExceptionPass();
$pass->process($container);
$this->assertSame($def, $container->getDefinition('foo_service_id')->getArgument(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@
<service id="runtime_error" class="stdClass" public="true">
<argument type="service" id="errored_definition"/>
</service>
<service id="errored_definition" class="stdClass"/>
<service id="errored_definition" class="stdClass">
<tag name="container.error" message="Service &quot;errored_definition&quot; is broken."/>
</service>
<service id="preload_sidekick" class="stdClass" public="true">
<tag name="container.preload" class="Some\Sidekick1"/>
<tag name="container.preload" class="Some\Sidekick2"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ services:
public: true
errored_definition:
class: stdClass
tags:
- container.error: { message: 'Service "errored_definition" is broken.' }
preload_sidekick:
class: stdClass
tags:
Expand Down
0