8000 [HttpKernel] Make sure that decorated service works with kernel.reset… · symfony/symfony@fbdade2 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbdade2

Browse files
author
Rokas Mikalkėnas
committed
[HttpKernel] Make sure that decorated service works with kernel.reset tag
1 parent 8e59e85 commit fbdade2

File tree

6 files changed

+173
-3
lines changed

6 files changed

+173
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ public function process(ContainerBuilder $container)
8989
$decoratingTags = $decoratingDefinition->getTags();
9090
$resetTags = [];
9191

92-
// container.service_locator and container.service_subscriber have special logic and they must not be transferred out to decorators
93-
foreach (['container.service_locator', 'container.service_subscriber'] as $containerTag) {
92+
// container.service_locator, container.service_subscriber and kernel.reset
93+
// have special logic and they must not be transferred out to decorators
94+
foreach (['container.service_locator', 'container.service_subscriber', 'kernel.reset'] as $containerTag) {
9495
if (isset($decoratingTags[$containerTag])) {
9596
$resetTags[$containerTag] = $decoratingTags[$containerTag];
9697
unset($decoratingTags[$containerTag]);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,25 @@ public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()
263263
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
264264
}
265265

266+
public function testProcessLeavesKernelResetTagOnOriginalDefinition()
267+
{
268+
$container = new ContainerBuilder();
269+
$container
270+
->register('foo')
271+
->setTags(['kernel.reset' => [], 'bar' => ['attr' => 'baz']])
272+
;
273+
$container
274+
->register('baz')
275+
->setTags(['foobar' => ['attr' => 'bar']])
276+
->setDecoratedService('foo')
277+
;
278+
279+
$this->process($container);
280+
281+
$this->assertEquals(['kernel.reset' => []], $container->getDefinition('baz.inner')->getTags());
282+
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
283+
}
284+
266285
public function testCannotDecorateSyntheticService()
267286
{
268287
$container = new ContainerBuilder();

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ResettableServicePassTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Alias;
67
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\DependencyInjection\ContainerInterface;
910
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1011
use Symfony\Component\DependencyInjection\Reference;
1112
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
1213
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
14+
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableInterface;
1315
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
16+
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableServiceDecorator;
1417
use Symfony\Component\HttpKernel\Tests\Fixtures\MultiResettableService;
1518
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
1619

@@ -81,4 +84,102 @@ public function testCompilerPassWithoutResetters()
8184

8285
$this->assertFalse($container->has('services_resetter'));
8386
}
87+
88+
public function testDecoratedLastResettableService()
89+
{
90+
$container = new ContainerBuilder();
91+
$container->register('services_resetter', ServicesResetter::class)
92+
->setPublic(true)
93+
->setArguments([null, []]);
94+
$container->addCompilerPass(new ResettableServicePass());
95+
96+
$container->register('clearable_service', ClearableService::class)
97+
->setFactory([ClearableService::class, 'create'])
98+
->addTag('kernel.reset', ['method' => 'reset']);
99+
100+
$container->setAlias(ClearableInterface::class, new Alias('clearable_service', true));
101+
102+
$container->register('clearable_service_decorator', ClearableServiceDecorator::class)
103+
->setDecoratedService(ClearableInterface::class)
104+
->setArgument(0, new Reference('.inner'));
105+
106+
$container->register('clearable_service_decorator_2', ClearableServiceDecorator::class)
107+
->setDecoratedService(ClearableInterface::class)
108+
->setArgument(0, new Reference('.inner'));
109+
110+
$container->compile();
111+
112+
$container->get(ClearableInterface::class)->clear();
113+
114+
self::assertSame(1, ClearableService::$counter);
115+
self::assertSame(2, ClearableServiceDecorator::$counter);
116+
117+
$container->get('services_resetter')->reset();
118+
self::assertSame(0, ClearableService::$counter);
119+
self::assertSame(2, ClearableServiceDecorator::$counter);
120+
}
121+
122+
public function testDecoratedMiddleResettableService()
123+
{
124+
$container = new ContainerBuilder();
125+
$container->register('services_resetter', ServicesResetter::class)
126+
->setPublic(true)
127+
->setArguments([null, []]);
128+
$container->addCompilerPass(new ResettableServicePass());
129+
130+
$container->register('clearable_service', ClearableService::class)
131+
->setFactory([ClearableService::class, 'create']);
132+
133+
$container->setAlias(ClearableInterface::class, new Alias('clearable_service', true));
134+
135+
$container->register('clearable_service_decorator', ClearableServiceDecorator::class)
136+
->setDecoratedService(ClearableInterface::class)
137+
->setArgument(0, new Reference('.inner'))
138+
->addTag('kernel.reset', ['method' => 'reset']);
139+
140+
$container->register('clearable_service_decorator_2', ClearableServiceDecorator::class)
141+
->setDecoratedService(ClearableInterface::class)
142+
->setArgument(0, new Reference('.inner'));
143+
144+
$container->compile();
145+
146+
$container->get(ClearableInterface::class)->clear();
147+
148+
self::assertSame(1, ClearableService::$counter);
149+
self::assertSame(2, ClearableServiceDecorator::$counter);
150+
151+
$container->get('services_resetter')->reset();
152+
self::assertSame(1, ClearableService::$counter);
153+
self::assertSame(0, ClearableServiceDecorator::$counter);
154+
}
155+
156+
public function testDecoratedFirstResettableService()
157+
{
158+
$container = new ContainerBuilder();
159+
$container->register('services_resetter', ServicesResetter::class)
160+
->setPublic(true)
161+
->setArguments([null, []]);
162+
$container->addCompilerPass(new ResettableServicePass());
163+
164+
$container->register('clearable_service', ClearableService::class)
165+
->setFactory([ClearableService::class, 'create']);
166+
167+
$container->setAlias(ClearableInterface::class, new Alias('clearable_service', true));
168+
169+
$container->register('clearable_service_decorator', ClearableServiceDecorator::class)
170+
->setDecoratedService(ClearableInterface::class)
171+
->setArgument(0, new Reference('.inner'))
172+
->addTag('kernel.reset', ['method' => 'reset']);
173+
174+
$container->compile();
175+
176+
$container->get(ClearableInterface::class)->clear();
177+
178+
self::assertSame(1, ClearableService::$counter);
179+
self::assertSame(1, ClearableServiceDecorator::$counter);
180+
181+
$container->get('services_resetter')->reset();
182+
self::assertSame(1, ClearableService::$counter);
183+
self::assertSame(0, ClearableServiceDecorator::$counter);
184+
}
84185
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
interface ClearableInterface
6+
{
7+
public function clear();
8+
}

src/Symfony/Component/HttpKernel/Tests/Fixtures/ClearableService.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
44

5-
class ClearableService
5+
use Symfony\Contracts\Service\ResetInterface;
6+
7+
class ClearableService implements ClearableInterface, ResetInterface
68
{
79
public static $counter = 0;
810

911
public function clear()
1012
{
1113
++self::$counter;
1214
}
15+
16+
public static function create()
17+
{
18+
return new self();
19+
}
20+
21+
public function reset()
22+
{
23+
self::$counter = 0;
24+
}
1325
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
use Symfony\Contracts\Service\ResetInterface;
6+
7+
class ClearableServiceDecorator implements ClearableInterface, ResetInterface
8+
{
9+
public static $counter = 0;
10+
11+
private $clearableService;
12+
13+
public function __construct(ClearableInterface $clearableService)
14+
{
15+
$this->clearableService = $clearableService;
16+
}
17+
18+
public function clear()
19+
{
20+
++self::$counter;
21+
22+
$this->clearableService->clear();
23+
}
24+
25+
public function reset()
26+
{
27+
self::$counter = 0;
28+
}
29+
}

0 commit comments

Comments
 (0)
0