Skip to content

[DependencyInjection] Improve ServiceLocatorTagPass service matching #28571

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

Merged
merged 1 commit into from
Oct 3, 2018
Merged
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 @@ -52,6 +52,12 @@ protected function processValue($value, $isRoot = false)
if (!$v instanceof Reference) {
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, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k));
}

if (\is_int($k)) {
unset($arguments[0][$k]);
Copy link
Member

@nicolas-grekas nicolas-grekas Sep 24, 2018

Choose a reason for hiding this comment

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

I'd suggest moving "unset" outside of the "if": that will allow preserving the order in the list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree but right after the for loop it does a ksort so I presume that's unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

I forgot about it, no need then. Thanks.


$k = (string) $v;
Copy link
Member

Choose a reason for hiding this comment

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

should we check whether this key already exists in the configured arguments ?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure: if the key exists after the numbered one, it will override it - and if it came before, then the numbered one should win to me. works for you also?

Copy link
Member

Choose a reason for hiding this comment

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

nope, it will always override it here. If it comes after the numbered one, processing it will see the value set by the processing of the numbered one, not the original one, as it would have been replaced

Copy link
Member

Choose a reason for hiding this comment

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

Really? foreach operates on a copy of array so that it will yield the original value at this iteration, and overwrite the value set by the numbered step to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some basic tests that should confirm @nicolas-grekas description. I do like the idea of preventing unexepected behavior for the user but it works either way for me.

}
$arguments[0][$k] = new ServiceClosureArgument($v);
}
ksort($arguments[0]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
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;

require_once __DIR__.'/../Fixtures/includes/classes.php';

class ServiceLocatorTagPassTest extends TestCase
{
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set.
*/
public function testNoServices()
{
$container = new ContainerBuilder();

$container->register('foo', ServiceLocator::class)
->addTag('container.service_locator')
;

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

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set, "string" found for key "0".
*/
public function testInvalidServices()
{
$container = new ContainerBuilder();

$container->register('foo', ServiceLocator::class)
->setArguments(array(array(
'dummy',
)))
->addTag('container.service_locator')
;

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

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

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

$container->register('foo', ServiceLocator::class)
->setArguments(array(array(
new Reference('bar'),
new Reference('baz'),
'some.service' => new Reference('bar'),
)))
->addTag('container.service_locator')
;

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

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

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

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

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

$container->register('foo', ServiceLocator::class)
->setArguments(array(array(
new Reference('bar'),
'bar' => new Reference('baz'),
)))
->addTag('container.service_locator')
;

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

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

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

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

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

$container->register('foo', ServiceLocator::class)
->setArguments(array(array(
'bar' => new Reference('baz'),
new Reference('bar'),
)))
->addTag('container.service_locator')
;

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

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

$this->assertSame(TestDefinition1::class, \get_class($locator('bar')));
}
}