Skip to content

Commit

Permalink
Allow service locators to be ordered by priority.
Browse files Browse the repository at this point in the history
  • Loading branch information
longwave authored and nicolas-grekas committed Jun 28, 2024
1 parent 4e6e9fa commit 1a97831
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
1 change: 0 additions & 1 deletion Compiler/ServiceLocatorTagPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public static function map(array $services): array

$services[$k] = new ServiceClosureArgument($v);
}
ksort($services);

return $services;
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Compiler/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public function testTaggedLocatorWithDefaultPriorityMethodConfiguredViaAttribute
// We need to check priority of instances in the factories
$factories = (new \ReflectionClass($locator))->getProperty('factories');

self::assertSame([BarTagClass::class, FooTagClass::class], array_keys($factories->getValue($locator)));
self::assertSame([FooTagClass::class, BarTagClass::class], array_keys($factories->getValue($locator)));
}

public function testTaggedLocatorWithDefaultIndexMethodAndWithDefaultPriorityMethodConfiguredViaAttribute()
Expand Down Expand Up @@ -652,7 +652,7 @@ public function testTaggedLocatorWithDefaultIndexMethodAndWithDefaultPriorityMet
// We need to check priority of instances in the factories
$factories = (new \ReflectionClass($locator))->getProperty('factories');

self::assertSame(['bar_tag_class', 'foo_tag_class'], array_keys($factories->getValue($locator)));
self::assertSame(['foo_tag_class', 'bar_tag_class'], array_keys($factories->getValue($locator)));
self::assertSame($container->get(BarTagClass::class), $locator->get('bar_tag_class'));
self::assertSame($container->get(FooTagClass::class), $locator->get('foo_tag_class'));
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Compiler/RegisterServiceSubscribersPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public static function getSubscribedServices(): array
'autowired' => new ServiceClosureArgument(new TypedReference('service.id', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired', [new Autowire(service: 'service.id')])),
'autowired.nullable' => new ServiceClosureArgument(new TypedReference('service.id', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'autowired.nullable', [new Autowire(service: 'service.id')])),
'autowired.parameter' => new ServiceClosureArgument('foobar'),
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.oO4rxCy.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.0tSxobl.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'target', [new Target('someTarget')])),
];
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
Expand Down
2 changes: 1 addition & 1 deletion Tests/Compiler/ServiceLocatorTagPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function testDefinitionOrderIsTheSame()
$locator = $container->getDefinition($locator);
$factories = $locator->getArguments()[0];

static::assertSame(['service-1', 'service-2'], array_keys($factories));
static::assertSame(['service-2', 'service-1'], array_keys($factories));
}

public function testBindingsAreProcessed()
Expand Down
10 changes: 5 additions & 5 deletions Tests/Fixtures/php/services_subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function isCompiled(): bool
public function getRemovedIds(): array
{
return [
'.service_locator.2hyyc9y' => true,
'.service_locator.KGUGnmw' => true,
'.service_locator.KGUGnmw.foo_service' => true,
'.service_locator.0H1ht0q' => true,
'.service_locator.0H1ht0q.foo_service' => true,
'.service_locator.tfSHZa1' => true,
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
];
}
Expand All @@ -68,14 +68,14 @@ protected static function getTestServiceSubscriberService($container)
protected static function getFooServiceService($container)
{
return $container->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($container->getService ??= $container->getService(...), [
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
'bar' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
'baz' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
'late_alias' => ['services', 'late_alias', 'getLateAliasService', false],
], [
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber',
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'bar' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'baz' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'late_alias' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1',
Expand Down

0 comments on commit 1a97831

Please sign in to comment.