-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] fix using bindings with locators of service subscribers #31541
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,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]); | ||
|
||
$k = (string) $v; | ||
} | ||
$arguments[0][$k] = new ServiceClosureArgument($v); | ||
} | ||
ksort($arguments[0]); | ||
|
@@ -91,7 +97,11 @@ public static function register(ContainerBuilder $container, array $refMap, $cal | |
->setPublic(false) | ||
->addTag('container.service_locator'); | ||
|
||
if (!$container->has($id = 'service_locator.'.ContainerBuilder::hash($locator))) { | ||
if (null !== $callerId && $container->hasDefinition($callerId)) { | ||
$locator->setBindings($container->getDefinition($callerId)->getBindings()); | ||
} | ||
|
||
if (!$container->hasDefinition($id = 'service_locator.'.ContainerBuilder::hash($locator))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are bindings taken into account by the hashing ? If no, that would cause issues. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correct, hash() uses serialize() so all the state is hashed |
||
$container->setDefinition($id, $locator); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?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\Argument\BoundArgument; | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test class borrowed from 4.2. |
||
{ | ||
/** | ||
* @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([[ | ||
'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([[ | ||
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([[ | ||
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([[ | ||
'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'))); | ||
} | ||
|
||
public function testBindingsAreCopied() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new test case for this fix. |
||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('foo') | ||
->setBindings(['foo' => 'foo']); | ||
|
||
$locator = ServiceLocatorTagPass::register($container, ['foo' => new Reference('foo')], 'foo'); | ||
$locator = $container->getDefinition($locator); | ||
$locator = $container->getDefinition($locator->getFactory()[0]); | ||
|
||
$this->assertSame(['foo'], array_keys($locator->getBindings())); | ||
$this->assertInstanceOf(BoundArgument::class, $locator->getBindings()['foo']); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
class TestDefinition2 extends Definition | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix is borrowed from 4.2.