Skip to content

Commit 25907f7

Browse files
authored
Made CriteriaConverter handlers priority-aware
1 parent a09c0a5 commit 25907f7

File tree

3 files changed

+83
-96
lines changed

3 files changed

+83
-96
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9306,18 +9306,6 @@ parameters:
93069306
count: 1
93079307
path: src/lib/Base/Container/Compiler/Search/FieldRegistryPass.php
93089308

9309-
-
9310-
message: '#^Method Ibexa\\Core\\Base\\Container\\Compiler\\Search\\Legacy\\CriteriaConverterPass\:\:addHandlers\(\) has no return type specified\.$#'
9311-
identifier: missingType.return
9312-
count: 1
9313-
path: src/lib/Base/Container/Compiler/Search/Legacy/CriteriaConverterPass.php
9314-
9315-
-
9316-
message: '#^Method Ibexa\\Core\\Base\\Container\\Compiler\\Search\\Legacy\\CriteriaConverterPass\:\:addHandlers\(\) has parameter \$handlers with no type specified\.$#'
9317-
identifier: missingType.parameter
9318-
count: 1
9319-
path: src/lib/Base/Container/Compiler/Search/Legacy/CriteriaConverterPass.php
9320-
93219309
-
93229310
message: '#^Method Ibexa\\Core\\Base\\Container\\Compiler\\Search\\Legacy\\CriteriaConverterPass\:\:process\(\) has no return type specified\.$#'
93239311
identifier: missingType.return
@@ -49152,18 +49140,6 @@ parameters:
4915249140
count: 1
4915349141
path: tests/lib/Base/Container/Compiler/Search/FieldTypeRegistryPassTest.php
4915449142

49155-
-
49156-
message: '#^Method Ibexa\\Tests\\Core\\Base\\Container\\Compiler\\Search\\Legacy\\CriteriaConverterPassTest\:\:testAddContentHandlers\(\) has no return type specified\.$#'
49157-
identifier: missingType.return
49158-
count: 1
49159-
path: tests/lib/Base/Container/Compiler/Search/Legacy/CriteriaConverterPassTest.php
49160-
49161-
-
49162-
message: '#^Method Ibexa\\Tests\\Core\\Base\\Container\\Compiler\\Search\\Legacy\\CriteriaConverterPassTest\:\:testAddLocationHandlers\(\) has no return type specified\.$#'
49163-
identifier: missingType.return
49164-
count: 1
49165-
path: tests/lib/Base/Container/Compiler/Search/Legacy/CriteriaConverterPassTest.php
49166-
4916749143
-
4916849144
message: '#^Method Ibexa\\Tests\\Core\\Base\\Container\\Compiler\\Search\\Legacy\\CriterionFieldValueHandlerRegistryPassTest\:\:testRegisterValueHandler\(\) has no return type specified\.$#'
4916949145
identifier: missingType.return

src/lib/Base/Container/Compiler/Search/Legacy/CriteriaConverterPass.php

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,56 @@
88

99
use Ibexa\Core\Persistence\Legacy\URL\Query\CriteriaConverter;
1010
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1112
use Symfony\Component\DependencyInjection\ContainerBuilder;
12-
use Symfony\Component\DependencyInjection\Definition;
13-
use Symfony\Component\DependencyInjection\Reference;
1413

1514
/**
1615
* This compiler pass will register Legacy Search Engine criterion handlers.
1716
*/
1817
class CriteriaConverterPass implements CompilerPassInterface
1918
{
20-
/**
21-
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
22-
*/
19+
use PriorityTaggedServiceTrait;
20+
2321
public function process(ContainerBuilder $container)
2422
{
25-
if (
26-
!$container->hasDefinition('ibexa.search.legacy.gateway.criteria_converter.content') &&
27-
!$container->hasDefinition('ibexa.search.legacy.gateway.criteria_converter.location') &&
28-
!$container->hasDefinition('ibexa.core.trash.search.legacy.gateway.criteria_converter') &&
29-
!$container->hasDefinition(CriteriaConverter::class)
30-
) {
31-
return;
32-
}
33-
34-
if ($container->hasDefinition('ibexa.search.legacy.gateway.criteria_converter.content')) {
35-
$criteriaConverterContent = $container->getDefinition('ibexa.search.legacy.gateway.criteria_converter.content');
36-
37-
$contentHandlers = $container->findTaggedServiceIds('ibexa.search.legacy.gateway.criterion_handler.content');
38-
39-
$this->addHandlers($criteriaConverterContent, $contentHandlers);
40-
}
41-
42-
if ($container->hasDefinition('ibexa.search.legacy.gateway.criteria_converter.location')) {
43-
$criteriaConverterLocation = $container->getDefinition('ibexa.search.legacy.gateway.criteria_converter.location');
23+
$this->setHandlersForConverter(
24+
$container,
25+
'ibexa.search.legacy.gateway.criteria_converter.content',
26+
'ibexa.search.legacy.gateway.criterion_handler.content'
27+
);
4428

45-
$locationHandlers = $container->findTaggedServiceIds('ibexa.search.legacy.gateway.criterion_handler.location');
29+
$this->setHandlersForConverter(
30+
$container,
31+
'ibexa.search.legacy.gateway.criteria_converter.location',
32+
'ibexa.search.legacy.gateway.criterion_handler.location'
33+
);
4634

47-
$this->addHandlers($criteriaConverterLocation, $locationHandlers);
48-
}
49-
50-
if ($container->hasDefinition('ibexa.core.trash.search.legacy.gateway.criteria_converter')) {
51-
$trashCriteriaConverter = $container->getDefinition('ibexa.core.trash.search.legacy.gateway.criteria_converter');
52-
$trashCriteriaHandlers = $container->findTaggedServiceIds('ibexa.search.legacy.trash.gateway.criterion.handler');
53-
54-
$this->addHandlers($trashCriteriaConverter, $trashCriteriaHandlers);
55-
}
35+
$this->setHandlersForConverter(
36+
$container,
37+
'ibexa.core.trash.search.legacy.gateway.criteria_converter',
38+
'ibexa.search.legacy.trash.gateway.criterion.handler'
39+
);
5640

57-
if ($container->hasDefinition(CriteriaConverter::class)) {
58-
$urlCriteriaConverter = $container->getDefinition(CriteriaConverter::class);
59-
$urlCriteriaHandlers = $container->findTaggedServiceIds('ibexa.storage.legacy.url.criterion.handler');
41+
$this->setHandlersForConverter(
42+
$container,
43+
CriteriaConverter::class,
44+
'ibexa.storage.legacy.url.criterion.handler'
45+
);
46+
}
6047

61-
$this->addHandlers($urlCriteriaConverter, $urlCriteriaHandlers);
48+
private function setHandlersForConverter(
49+
ContainerBuilder $container,
50+
string $serviceId,
51+
string $handlersTag
52+
): void {
53+
if (!$container->hasDefinition($serviceId)) {
54+
return;
6255
}
63-
}
6456

65-
protected function addHandlers(Definition $definition, $handlers)
66-
{
67-
foreach ($handlers as $id => $attributes) {
68-
$definition->addMethodCall('addHandler', [new Reference($id)]);
57+
$service = $container->getDefinition($serviceId);
58+
$handlers = $this->findAndSortTaggedServices($handlersTag, $container);
59+
foreach ($handlers as $handler) {
60+
$service->addMethodCall('addHandler', [$handler]);
6961
}
7062
}
7163
}

tests/lib/Base/Container/Compiler/Search/Legacy/CriteriaConverterPassTest.php

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Ibexa\Tests\Core\Base\Container\Compiler\Search\Legacy;
88

99
use Ibexa\Core\Base\Container\Compiler\Search\Legacy\CriteriaConverterPass;
10+
use Ibexa\Core\Persistence\Legacy\URL\Query\CriteriaConverter;
1011
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
1112
use Symfony\Component\DependencyInjection\ContainerBuilder;
1213
use Symfony\Component\DependencyInjection\Definition;
@@ -25,67 +26,85 @@ protected function registerCompilerPass(ContainerBuilder $container): void
2526
$container->addCompilerPass(new CriteriaConverterPass());
2627
}
2728

28-
public function testAddContentHandlers()
29+
/**
30+
* @dataProvider provideDescribedServiceToTagName
31+
*/
32+
public function testAddHandlers(string $serviceId, string $tag): void
2933
{
3034
$this->setDefinition(
31-
'ibexa.search.legacy.gateway.criteria_converter.content',
35+
$serviceId,
3236
new Definition()
3337
);
3438

35-
$serviceId = 'service_id';
3639
$def = new Definition();
37-
$def->addTag('ibexa.search.legacy.gateway.criterion_handler.content');
38-
$this->setDefinition($serviceId, $def);
40+
$def->addTag($tag);
41+
$this->setDefinition('service_id', $def);
3942

4043
$this->compile();
4144

4245
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
43-
'ibexa.search.legacy.gateway.criteria_converter.content',
46+
$serviceId,
4447
'addHandler',
45-
[new Reference($serviceId)]
48+
[new Reference('service_id')]
4649
);
4750
}
4851

49-
public function testAddLocationHandlers()
52+
/**
53+
* @dataProvider provideDescribedServiceToTagName
54+
*/
55+
public function testAddContentHandlersWithPriority(string $serviceId, string $tag): void
5056
{
5157
$this->setDefinition(
52-
'ibexa.search.legacy.gateway.criteria_converter.location',
58+
$serviceId,
5359
new Definition()
5460
);
5561

56-
$serviceId = 'service_id';
5762
$def = new Definition();
58-
$def->addTag('ibexa.search.legacy.gateway.criterion_handler.location');
59-
$this->setDefinition($serviceId, $def);
63+
$def->addTag($tag, ['priority' => 0]);
64+
$this->setDefinition('service_1_id', $def);
65+
66+
$def = new Definition();
67+
$def->addTag($tag, ['priority' => 100]);
68+
$this->setDefinition('service_with_priority', $def);
6069

6170
$this->compile();
6271

6372
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
64-
'ibexa.search.legacy.gateway.criteria_converter.location',
73+
$serviceId,
74+
'addHandler',
75+
[new Reference('service_with_priority')],
76+
0,
77+
);
78+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
79+
$serviceId,
6580
'addHandler',
66-
[new Reference($serviceId)]
81+
[new Reference('service_1_id')],
82+
1,
6783
);
6884
}
6985

70-
public function testAddTrashHandlers(): void
86+
/**
87+
* @return iterable<string, string>
88+
*/
89+
public static function provideServiceToTagName(): iterable
7190
{
72-
$this->setDefinition(
73-
'ibexa.core.trash.search.legacy.gateway.criteria_converter',
74-
new Definition()
75-
);
91+
yield 'ibexa.search.legacy.gateway.criteria_converter.content' => 'ibexa.search.legacy.gateway.criterion_handler.content';
7692

77-
$serviceId = 'service_id';
78-
$def = new Definition();
79-
$def->addTag('ibexa.search.legacy.trash.gateway.criterion.handler');
80-
$this->setDefinition($serviceId, $def);
93+
yield 'ibexa.search.legacy.gateway.criteria_converter.location' => 'ibexa.search.legacy.gateway.criterion_handler.location';
8194

82-
$this->compile();
95+
yield 'ibexa.core.trash.search.legacy.gateway.criteria_converter' => 'ibexa.search.legacy.trash.gateway.criterion.handler';
8396

84-
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
85-
'ibexa.core.trash.search.legacy.gateway.criteria_converter',
86-
'addHandler',
87-
[new Reference($serviceId)]
88-
);
97+
yield CriteriaConverter::class => 'ibexa.storage.legacy.url.criterion.handler';
98+
}
99+
100+
/**
101+
* @return iterable<string, array{string, string}>
102+
*/
103+
public static function provideDescribedServiceToTagName(): iterable
104+
{
105+
foreach (self::provideServiceToTagName() as $serviceId => $tag) {
106+
yield sprintf('Service "%s" with tag "%s"', $serviceId, $tag) => [$serviceId, $tag];
107+
}
89108
}
90109

91110
public function testAddMultipleHandlers(): void

0 commit comments

Comments
 (0)