8000 bug #42347 Fix ServiceLocator indexing when service is decorated (shyim) · symfony/symfony@a206e77 · GitHub
[go: up one dir, main page]

Skip to content

Commit a206e77

Browse files
committed
bug #42347 Fix ServiceLocator indexing when service is decorated (shyim)
This PR was merged into the 5.3 branch. Discussion ---------- Fix ServiceLocator indexing when service is decorated | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | We save in our application the service id into the database to look up the service by a tagged locator (finding the service which handles the payment). With Symfony 5.3 the keys are differed when the service is decorated, this breaks our application logic. The problem has been introduced with the addition of autowired arguments. The Pass order has been changed (91fbc90#diff-b75bcd1c8a876c4e6f189c298e7c7133d1d3bc87e2a2876c40ab92a51df1093aR68) Now the `ServiceLocatorTagPass` runs after the decoration magic has been done. This PR moves the DecoratorServicePass back to the place where it was on 5.2 and previous versions Commits ------- 499df3d Fix ServiceLocator indexing when service is decorated
2 parents 36ebebb + 499df3d commit a206e77

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public function __construct()
6262
new AutowireRequiredMethodsPass(),
6363
new AutowireRequiredPropertiesPass(),
6464
new ResolveBindingsPass(),
65-
new DecoratorServicePass(),
6665
new CheckDefinitionValidityPass(),
6766
new AutowirePass(false),
6867
new ServiceLocatorTagPass(),
68+
new DecoratorServicePass(),
6969
new ResolveTaggedIteratorArgumentPass(),
7070
new ResolveServiceSubscribersPass(),
7171
new ResolveReferencesToAliasesPass(),

src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
16+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
17+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1618
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
1719
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Definition;
1821
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1922
use Symfony\Component\DependencyInjection\Reference;
2023
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -143,4 +146,56 @@ public function testBindingsAreCopied()
143146
$this->assertSame(['foo'], array_keys($locator->getBindings()));
144147
$this->assertInstanceOf(BoundArgument::class, $locator->getBindings()['foo']);
145148
}
149+
150+
public function testIndexedByServiceIdWithDecoration()
151+
{
152+
$container = new ContainerBuilder();
153+
154+
$locator = new Definition(Locator::class);
155+
$locator->setPublic(true);
156+
$locator->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('test_tag', null, null, true)));
157+
158+
$container->setDefinition(Locator::class, $locator);
159+
160+
$service = new Definition(Service::class);
161+
$service->setPublic(true);
162+
$service->addTag('test_tag');
163+
164+
$container->setDefinition(Service::class, $service);
165+
166+
$decorated = new Definition(Decorated::class);
167+
$decorated->setPublic(true);
168+
$decorated->setDecoratedService(Service::class);
169+
170+
$container->setDefinition(Decorated::class, $decorated);
171+
172+
$container->compile();
173+
174+
/** @var ServiceLocator $locator */
175+
$locator = $container->get(Locator::class)->locator;
176+
static::assertTrue($locator->has(Service::class));
177+
static::assertFalse($locator->has(Decorated::class));
178+
static::assertInstanceOf(Decorated::class, $locator->get(Service::class));
179+
}
180+
}
181+
182+
class Locator
183+
{
184+
/**
185+
* @var ServiceLocator
186+
*/
187+
public $locator;
188+
189+
public function __construct(ServiceLocator $locator)
190+
{
191+
$this->locator = $locator;
192+
}
193+
}
194+
195+
class Service
196+
{
197+
}
198+
199+
class DecoratedService
200+
{
146201
}

0 commit comments

Comments
 (0)
0