Skip to content

Commit 72e81f3

Browse files
committed
Sort services in service locator according to priority
1 parent 0f8de5f commit 72e81f3

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

Compiler/ServiceLocatorTagPass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public static function register(ContainerBuilder $container, array $refMap, stri
102102
}
103103
$refMap[$id] = new ServiceClosureArgument($ref);
104104
}
105-
ksort($refMap);
106105

107106
$locator = (new Definition(ServiceLocator::class))
108107
->addArgument($refMap)

Loader/Configurator/ContainerConfigurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ function tagged_iterator(string $tag, string $indexAttribute = null, string $def
174174
/**
175175
* Creates a service locator by tag name.
176176
*/
177-
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): ServiceLocatorArgument
177+
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null): ServiceLocatorArgument
178178
{
179-
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true));
179+
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod));
180180
}
181181

182182
/**

Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ public function testIndexedByServiceIdWithDecoration()
177177
static::assertFalse($locator->has(Decorated::class));
178178
static::assertInstanceOf(Decorated::class, $locator->get(Service::class));
179179
}
180+
181+
public function testDefinitionOrderIsTheSame()
182+
{
183+
$container = new ContainerBuilder();
184+
$container->register('service-1');
185+
$container->register('service-2');
186+
187+
$locator = ServiceLocatorTagPass::register($container, [
188+
'service-2' => new Reference('service-2'),
189+
'service-1' => new Reference('service-1'),
190+
]);
191+
$locator = $container->getDefinition($locator);
192+
$factories = $locator->getArguments()[0];
193+
194+
static::assertSame(['service-2', 'service-1'], array_keys($factories));
195+
}
180196
}
181197

182198
class Locator

Tests/Fixtures/php/services_subscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function isCompiled(): bool
4444
public function getRemovedIds(): array
4545
{
4646
return [
47-
'.service_locator.DlIAmAe' => true,
48-
'.service_locator.DlIAmAe.foo_service' => true,
4947
'.service_locator.t5IGRMW' => true,
48+
'.service_locator.zFfA7ng' => true,
49+
'.service_locator.zFfA7ng.foo_service' => true,
5050
'Psr\\Container\\ContainerInterface' => true,
5151
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
5252
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,

0 commit comments

Comments
 (0)