Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Use ID of the original service when key not specified in service_locator config #48653

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 @@ -37,7 +37,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
$value->setValues($this->findAndSortTaggedServices($value->getTaggedIteratorArgument(), $this->container));
}

return self::register($this->container, $value->getValues());
return self::register($this->container, $this->replaceNumericServiceLocatorKeys($value->getValues()));
}

if ($value instanceof Definition) {
Expand All @@ -62,26 +62,15 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.', $this->currentId));
}

$i = 0;
$services = $this->replaceNumericServiceLocatorKeys($services);

foreach ($services as $k => $v) {
if ($v instanceof ServiceClosureArgument) {
continue;
}

if ($i === $k) {
if ($v instanceof Reference) {
unset($services[$k]);
$k = (string) $v;
}
++$i;
} elseif (\is_int($k)) {
$i = null;
}

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

$value->setArgument(0, $services);

Expand All @@ -100,6 +89,39 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
return new Reference($id);
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

In my view, this needs documentation to explain what it is doing and why. The method name certainly gives no indication as to what is going on here.

Suggested change
/**
/**
* Replaces numeric service IDs with service names.

The method name could probably be improved along these lines, also, e.g. replaceNumericServiceLocatorKeys.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion!

* Replaces numeric service IDs with service names.
*
* @param array<array-key, mixed> $services
*
* @return array<string, mixed>
*/
private function replaceNumericServiceLocatorKeys(array $services): array
{
$i = 0;

foreach ($services as $k => $v) {
if ($i === $k && $v instanceof Reference) {
unset($services[$k]);
$k = (string) $v;
++$i;
} elseif (\is_int($k)) {
/**
* Not consecutive numbers, so stop replacing names but continue the loop.
*
* @see ServiceLocatorTagPassTest::testInheritedKeyOverwritesPreviousServiceWithKey
*/
$i = null;
}

$services[$k] = $v;
}

ksort($services);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I knew... I extracted this code for reusing. The ksort() was removed from the register method on c67c2df. I have no idea why it still exists in processValue.


return $services;
}

public static function register(ContainerBuilder $container, array $map, string $callerId = null): Reference
{
foreach ($map as $k => $v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition2;

Expand Down Expand Up @@ -62,8 +61,8 @@ public function testProcessValue()
{
$container = new ContainerBuilder();

$container->register('bar', CustomDefinition::class);
$container->register('baz', CustomDefinition::class);
$container->register('bar', TestDefinition1::class);
$container->register('baz', TestDefinition2::class);

$container->register('foo', ServiceLocator::class)
->setArguments([[
Expand All @@ -79,9 +78,33 @@ public function testProcessValue()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(CustomDefinition::class, $locator('bar')::class);
$this->assertSame(CustomDefinition::class, $locator('baz')::class);
$this->assertSame(CustomDefinition::class, $locator('some.service')::class);
$this->assertInstanceOf(TestDefinition1::class, $locator('bar'));
$this->assertInstanceOf(TestDefinition2::class, $locator('baz'));
$this->assertInstanceOf(TestDefinition1::class, $locator('some.service'));
}

public function testProcessLocatorArgumentValue()
{
$container = new ContainerBuilder();

$container->register('bar', TestDefinition1::class);
$container->register('baz', TestDefinition2::class);

$container->register('foo', Locator::class)
->addArgument(new ServiceLocatorArgument([
new Reference('bar'),
'some.service' => new Reference('bar'),
new Reference('baz'),
]));

(new ServiceLocatorTagPass())->process($container);

/** @var ServiceLocator $locator */
$locator = $container->get('foo')->locator;

$this->assertInstanceOf(TestDefinition1::class, $locator('bar'));
$this->assertInstanceOf(TestDefinition1::class, $locator('some.service'));
$this->assertInstanceOf(TestDefinition2::class, $locator('baz'));
}

public function testServiceWithKeyOverwritesPreviousInheritedKey()
Expand All @@ -104,7 +127,7 @@ public function testServiceWithKeyOverwritesPreviousInheritedKey()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(TestDefinition2::class, $locator('bar')::class);
$this->assertInstanceOf(TestDefinition2::class, $locator('bar'));
}

public function testInheritedKeyOverwritesPreviousServiceWithKey()
Expand All @@ -128,8 +151,8 @@ public function testInheritedKeyOverwritesPreviousServiceWithKey()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(TestDefinition1::class, $locator('bar')::class);
$this->assertSame(TestDefinition2::class, $locator(16)::class);
$this->assertInstanceOf(TestDefinition1::class, $locator('bar'));
$this->assertInstanceOf(TestDefinition2::class, $locator(16));
}

public function testBindingsAreCopied()
Expand Down Expand Up @@ -164,8 +187,8 @@ public function testTaggedServices()
/** @var ServiceLocator $locator */
$locator = $container->get('foo');

$this->assertSame(TestDefinition1::class, $locator('bar')::class);
$this->assertSame(TestDefinition2::class, $locator('baz')::class);
$this->assertInstanceOf(TestDefinition1::class, $locator('bar'));
$this->assertInstanceOf(TestDefinition2::class, $locator('baz'));
}

public function testIndexedByServiceIdWithDecoration()
Expand All @@ -184,19 +207,19 @@ public function testIndexedByServiceIdWithDecoration()

$container->setDefinition(Service::class, $service);

$decorated = new Definition(Decorated::class);
$decorated = new Definition(DecoratedService::class);
$decorated->setPublic(true);
$decorated->setDecoratedService(Service::class);

$container->setDefinition(Decorated::class, $decorated);
$container->setDefinition(DecoratedService::class, $decorated);

$container->compile();

/** @var ServiceLocator $locator */
$locator = $container->get(Locator::class)->locator;
static::assertTrue($locator->has(Service::class));
static::assertFalse($locator->has(Decorated::class));
static::assertInstanceOf(Decorated::class, $locator->get(Service::class));
static::assertFalse($locator->has(DecoratedService::class));
static::assertInstanceOf(DecoratedService::class, $locator->get(Service::class));
}

public function testDefinitionOrderIsTheSame()
Expand Down