Skip to content

Commit

Permalink
bug #34607 [HttpKernel] Ability to define multiple kernel.reset tags …
Browse files Browse the repository at this point in the history
…(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
  • Loading branch information
nicolas-grekas committed Nov 26, 2019
2 parents 2dd32b0 + 6f8dbf1 commit 127ebee
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
Expand Up @@ -43,16 +43,21 @@ public function process(ContainerBuilder $container)

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

if (!isset($attributes['method'])) {
throw new RuntimeException(sprintf('Tag %s requires the "method" attribute to be set.', $this->tagName));
}
foreach ($tags as $attributes) {
if (!isset($attributes['method'])) {
throw new RuntimeException(sprintf('Tag "%s" requires the "method" attribute to be set.', $this->tagName));
}

if (!isset($methods[$id])) {
$methods[$id] = [];
}

$methods[$id] = $attributes['method'];
$methods[$id][] = $attributes['method'];
}
}

if (empty($services)) {
if (!$services) {
$container->removeAlias('services_resetter');
$container->removeDefinition('services_resetter');

Expand Down
Expand Up @@ -35,7 +35,9 @@ public function __construct(\Traversable $resettableServices, array $resetMethod
public function reset()
{
foreach ($this->resettableServices as $id => $service) {
$service->{$this->resetMethods[$id]}();
foreach ((array) $this->resetMethods[$id] as $resetMethod) {
$service->$resetMethod();
}
}
}
}
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\MultiResettableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

class ResettableServicePassTest extends TestCase
Expand All @@ -23,6 +24,10 @@ public function testCompilerPass()
$container->register('two', ClearableService::class)
->setPublic(true)
->addTag('kernel.reset', ['method' => 'clear']);
$container->register('three', MultiResettableService::class)
->setPublic(true)
->addTag('kernel.reset', ['method' => 'resetFirst'])
->addTag('kernel.reset', ['method' => 'resetSecond']);

$container->register('services_resetter', ServicesResetter::class)
->setPublic(true)
Expand All @@ -38,10 +43,12 @@ public function testCompilerPass()
new IteratorArgument([
'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
'three' => new Reference('three', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
]),
[
'one' => 'reset',
'two' => 'clear',
'one' => ['reset'],
'two' => ['clear'],
'three' => ['resetFirst', 'resetSecond'],
],
],
$definition->getArguments()
Expand All @@ -51,7 +58,7 @@ public function testCompilerPass()
public function testMissingMethod()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
$this->expectExceptionMessage('Tag kernel.reset requires the "method" attribute to be set.');
$this->expectExceptionMessage('Tag "kernel.reset" requires the "method" attribute to be set.');
$container = new ContainerBuilder();
$container->register(ResettableService::class)
->addTag('kernel.reset');
Expand Down
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\MultiResettableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

class ServicesResetterTest extends TestCase
Expand All @@ -22,21 +23,27 @@ protected function setUp(): void
{
ResettableService::$counter = 0;
ClearableService::$counter = 0;
MultiResettableService::$resetFirstCounter = 0;
MultiResettableService::$resetSecondCounter = 0;
}

public function testResetServices()
{
$resetter = new ServicesResetter(new \ArrayIterator([
'id1' => new ResettableService(),
'id2' => new ClearableService(),
'id3' => new MultiResettableService(),
]), [
'id1' => 'reset',
'id2' => 'clear',
'id1' => ['reset'],
'id2' => ['clear'],
'id3' => ['resetFirst', 'resetSecond'],
]);

$resetter->reset();

$this->assertEquals(1, ResettableService::$counter);
$this->assertEquals(1, ClearableService::$counter);
$this->assertSame(1, ResettableService::$counter);
$this->assertSame(1, ClearableService::$counter);
$this->assertSame(1, MultiResettableService::$resetFirstCounter);
$this->assertSame(1, MultiResettableService::$resetSecondCounter);
}
}
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

class MultiResettableService
{
public static $resetFirstCounter = 0;
public static $resetSecondCounter = 0;

public function resetFirst()
{
++self::$resetFirstCounter;
}

public function resetSecond()
{
++self::$resetSecondCounter;
}
}

0 comments on commit 127ebee

Please sign in to comment.