Skip to content

[HttpKernel] Ability to define multiple kernel.reset tags #34607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}