Skip to content

Commit 127ebee

Browse files
bug #34607 [HttpKernel] Ability to define multiple kernel.reset tags (rmikalkenas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpKernel] Ability to define multiple kernel.reset tags | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34606 | License | MIT | Doc PR | All info #34606 Commits ------- 6f8dbf1 [HttpKernel] Ability to define multiple kernel.reset tags
2 parents 2dd32b0 + 6f8dbf1 commit 127ebee

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

src/Symfony/Component/HttpKernel/DependencyInjection/ResettableServicePass.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,21 @@ public function process(ContainerBuilder $container)
4343

4444
foreach ($container->findTaggedServiceIds($this->tagName, true) as $id => $tags) {
4545
$services[$id] = new Reference($id, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE);
46-
$attributes = $tags[0];
4746

48-
if (!isset($attributes['method'])) {
49-
throw new RuntimeException(sprintf('Tag %s requires the "method" attribute to be set.', $this->tagName));
50-
}
47+
foreach ($tags as $attributes) {
48+
if (!isset($attributes['method'])) {
49+
throw new RuntimeException(sprintf('Tag "%s" requires the "method" attribute to be set.', $this->tagName));
50+
}
51+
52+
if (!isset($methods[$id])) {
53+
$methods[$id] = [];
54+
}
5155

52-
$methods[$id] = $attributes['method'];
56+
$methods[$id][] = $attributes['method'];
57+
}
5358
}
5459

55-
if (empty($services)) {
60+
if (!$services) {
5661
$container->removeAlias('services_resetter');
5762
$container->removeDefinition('services_resetter');
5863

src/Symfony/Component/HttpKernel/DependencyInjection/ServicesResetter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public function __construct(\Traversable $resettableServices, array $resetMethod
3535
public function reset()
3636
{
3737
foreach ($this->resettableServices as $id => $service) {
38-
$service->{$this->resetMethods[$id]}();
38+
foreach ((array) $this->resetMethods[$id] as $resetMethod) {
39+
$service->$resetMethod();
40+
}
3941
}
4042
}
4143
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
1111
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
1212
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
13+
use Symfony\Component\HttpKernel\Tests\Fixtures\MultiResettableService;
1314
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
1415

1516
class ResettableServicePassTest extends TestCase
@@ -23,6 +24,10 @@ public function testCompilerPass()
2324
$container->register('two', ClearableService::class)
2425
->setPublic(true)
2526
->addTag('kernel.reset', ['method' => 'clear']);
27+
$container->register('three', MultiResettableService::class)
28+
->setPublic(true)
29+
->addTag('kernel.reset', ['method' => 'resetFirst'])
30+
->addTag('kernel.reset', ['method' => 'resetSecond']);
2631

2732
$container->register('services_resetter', ServicesResetter::class)
2833
->setPublic(true)
@@ -38,10 +43,12 @@ public function testCompilerPass()
3843
new IteratorArgument([
3944
'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
4045
'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
46+
'three' => new Reference('three', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
4147
]),
4248
[
43-
'one' => 'reset',
44-
'two' => 'clear',
49+
'one' => ['reset'],
50+
'two' => ['clear'],
51+
'three' => ['resetFirst', 'resetSecond'],
4552
],
4653
],
4754
$definition->getArguments()
@@ -51,7 +58,7 @@ public function testCompilerPass()
5158
public function testMissingMethod()
5259
{
5360
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
54-
$this->expectExceptionMessage('Tag kernel.reset requires the "method" attribute to be set.');
61+
$this->expectExceptionMessage('Tag "kernel.reset" requires the "method" attribute to be set.');
5562
$container = new ContainerBuilder();
5663
$container->register(ResettableService::class)
5764
->addTag('kernel.reset');

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
1616
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
17+
use Symfony\Component\HttpKernel\Tests\Fixtures\MultiResettableService;
1718
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
1819

1920
class ServicesResetterTest extends TestCase
@@ -22,21 +23,27 @@ protected function setUp(): void
2223
{
2324
ResettableService::$counter = 0;
2425
ClearableService::$counter = 0;
26+
MultiResettableService::$resetFirstCounter = 0;
27+
MultiResettableService::$resetSecondCounter = 0;
2528
}
2629

2730
public function testResetServices()
2831
{
2932
$resetter = new ServicesResetter(new \ArrayIterator([
3033
'id1' => new ResettableService(),
3134
'id2' => new ClearableService(),
35+
'id3' => new MultiResettableService(),
3236
]), [
33-
'id1' => 'reset',
34-
'id2' => 'clear',
37+
'id1' => ['reset'],
38+
'id2' => ['clear'],
39+
'id3' => ['resetFirst', 'resetSecond'],
3540
]);
3641

3742
$resetter->reset();
3843

39-
$this->assertEquals(1, ResettableService::$counter);
40-
$this->assertEquals(1, ClearableService::$counter);
44+
$this->assertSame(1, ResettableService::$counter);
45+
$this->assertSame(1, ClearableService::$counter);
46+
$this->assertSame(1, MultiResettableService::$resetFirstCounter);
47+
$this->assertSame(1, MultiResettableService::$resetSecondCounter);
4148
}
4249
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
class MultiResettableService
6+
{
7+
public static $resetFirstCounter = 0;
8+
public static $resetSecondCounter = 0;
9+
10+
public function resetFirst()
11+
{
12+
++self::$resetFirstCounter;
13+
}
14+
15+
public function resetSecond()
16+
{
17+
++self::$resetSecondCounter;
18+
}
19+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy