minor #32659 [ErrorRenderer] Fixed the priority order of the error renderers registration (yceruto)

This PR was merged into the 4.4 branch.

Discussion
----------

[ErrorRenderer] Fixed the priority order of the error renderers registration

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Spotted in https://github.com/symfony/symfony/pull/31398 and https://github.com/symfony/symfony/pull/32504

Commits
-------

710b51dcb1 Fixed the priority order of the error renderers registration
This commit is contained in:
Tobias Schultze 2019-07-22 23:36:35 +02:00
commit 759f91c565
2 changed files with 20 additions and 5 deletions

View File

@ -40,23 +40,22 @@ class ErrorRendererPass implements CompilerPassInterface
return;
}
$renderers = $registered = [];
$renderers = [];
foreach ($container->findTaggedServiceIds($this->rendererTag, true) as $serviceId => $tags) {
/** @var ErrorRendererInterface $class */
$class = $container->getDefinition($serviceId)->getClass();
foreach ($tags as $tag) {
$format = $tag['format'] ?? $class::getFormat();
if (!isset($registered[$format])) {
$priority = $tag['priority'] ?? 0;
$priority = $tag['priority'] ?? 0;
if (!isset($renderers[$priority][$format])) {
$renderers[$priority][$format] = new Reference($serviceId);
$registered[$format] = true;
}
}
}
if ($renderers) {
krsort($renderers);
ksort($renderers);
$renderers = array_merge(...$renderers);
}

View File

@ -48,4 +48,20 @@ class ErrorRendererPassTest extends TestCase
];
$this->assertEquals($expected, $serviceLocatorDefinition->getArgument(0));
}
public function testServicesAreOrderedAccordingToPriority()
{
$container = new ContainerBuilder();
$definition = $container->register('error_renderer')->setArguments([null]);
$container->register('r2')->addTag('error_renderer.renderer', ['format' => 'json', 'priority' => 100]);
$container->register('r1')->addTag('error_renderer.renderer', ['format' => 'json', 'priority' => 200]);
$container->register('r3')->addTag('error_renderer.renderer', ['format' => 'json']);
(new ErrorRendererPass())->process($container);
$expected = [
'json' => new ServiceClosureArgument(new Reference('r1')),
];
$serviceLocatorDefinition = $container->getDefinition((string) $definition->getArgument(0));
$this->assertEquals($expected, $serviceLocatorDefinition->getArgument(0));
}
}