[HttpKernel] Ability to define multiple kernel.reset tags

This commit is contained in:
Rokas Mikalkėnas 2019-11-25 19:31:29 +02:00 committed by Nicolas Grekas
parent ec781c927a
commit 6f8dbf1a5f
5 changed files with 54 additions and 14 deletions

View File

@ -43,16 +43,21 @@ class ResettableServicePass implements CompilerPassInterface
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');

View File

@ -35,7 +35,9 @@ class ServicesResetter implements ResetInterface
public function reset()
{
foreach ($this->resettableServices as $id => $service) {
$service->{$this->resetMethods[$id]}();
foreach ((array) $this->resetMethods[$id] as $resetMethod) {
$service->$resetMethod();
}
}
}
}

View File

@ -10,6 +10,7 @@ use Symfony\Component\DependencyInjection\Reference;
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
@ -23,6 +24,10 @@ class ResettableServicePassTest extends TestCase
$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)
@ -38,10 +43,12 @@ class ResettableServicePassTest extends TestCase
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()
@ -51,7 +58,7 @@ class ResettableServicePassTest extends TestCase
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');

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
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
@ -22,6 +23,8 @@ class ServicesResetterTest extends TestCase
{
ResettableService::$counter = 0;
ClearableService::$counter = 0;
MultiResettableService::$resetFirstCounter = 0;
MultiResettableService::$resetSecondCounter = 0;
}
public function testResetServices()
@ -29,14 +32,18 @@ class ServicesResetterTest extends TestCase
$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);
}
}

View File

@ -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;
}
}