Skip to content

[DependencyInjection] Sort services in service locator according to priority #42532

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -102,7 +102,6 @@ public static function register(ContainerBuilder $container, array $refMap, stri
}
$refMap[$id] = new ServiceClosureArgument($ref);
}
ksort($refMap);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand sort here is for easy testing


$locator = (new Definition(ServiceLocator::class))
->addArgument($refMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ function tagged_iterator(string $tag, string $indexAttribute = null, string $def
/**
* Creates a service locator by tag name.
*/
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): ServiceLocatorArgument
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null): ServiceLocatorArgument
{
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true));
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ public function testIndexedByServiceIdWithDecoration()
static::assertFalse($locator->has(Decorated::class));
static::assertInstanceOf(Decorated::class, $locator->get(Service::class));
}

public function testDefinitionOrderIsTheSame()
{
$container = new ContainerBuilder();
$container->register('service-1');
$container->register('service-2');

$locator = ServiceLocatorTagPass::register($container, [
'service-2' => new Reference('service-2'),
'service-1' => new Reference('service-1'),
]);
$locator = $container->getDefinition($locator);
$factories = $locator->getArguments()[0];

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

class Locator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function isCompiled(): bool
public function getRemovedIds(): array
{
return [
'.service_locator.DlIAmAe' => true,
'.service_locator.DlIAmAe.foo_service' => true,
'.service_locator.t5IGRMW' => true,
'.service_locator.zFfA7ng' => true,
'.service_locator.zFfA7ng.foo_service' => true,
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function testNoExceptionOnNonExistentTypeHintOptionalArg()
$pass->process($container);

$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
$this->assertSame(['foo::barAction', 'foo::fooAction'], array_keys($locator));
$this->assertEqualsCanonicalizing(['foo::barAction', 'foo::fooAction'], array_keys($locator));
}

public function testArgumentWithNoTypeHintIsOk()
Expand Down Expand Up @@ -396,7 +396,10 @@ public function testAlias()
$pass->process($container);

$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
$this->assertSame([RegisterTestController::class.'::fooAction', 'foo::fooAction'], array_keys($locator));

$services = array_keys($locator);
sort($services);
$this->assertSame([RegisterTestController::class.'::fooAction', 'foo::fooAction'], $services);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testProcess()
'Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass: Removing method "setTestCase" of service "c2" from controller candidates: the method is called at instantiation, thus cannot be an action.',
];

$this->assertSame($expectedLog, $container->getCompiler()->getLog());
$this->assertEqualsCanonicalizing($expectedLog, $container->getCompiler()->getLog());
}

public function testInvoke()
Expand Down