8000 [HttpKernel] Make sure that decorated service works with kernel.reset tag by rmikalkenas · Pull Request #43972 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Make sure that decorated service works with kernel.reset tag #43972

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -89,8 +89,9 @@ public function process(ContainerBuilder $container)
$decoratingTags = $decoratingDefinition->getTags();
< 8000 span class=pl-s1>$resetTags = [];

// container.service_locator and container.service_subscriber have special logic and they must not be transferred out to decorators
foreach (['container.service_locator', 'container.service_subscriber'] as $containerTag) {
// container.service_locator, container.service_subscriber and kernel.reset
// have special logic and they must not be transferred out to decorators
foreach (['container.service_locator', 'container.service_subscriber', 'kernel.reset'] as $containerTag) {
if (isset($decoratingTags[$containerTag])) {
$resetTags[$containerTag] = $decoratingTags[$containerTag];
unset($decoratingTags[$containerTag]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,25 @@ public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
}

public function testProcessLeavesKernelResetTagOnOriginalDefinition()
{
$container = new ContainerBuilder();
$container
->register('foo')
->setTags(['kernel.reset' => [], 'bar' => ['attr' => 'baz']])
;
$container
->register('baz')
->setTags(['foobar' => ['attr' => 'bar']])
->setDecoratedService('foo')
;

$this->process($container);

$this->assertEquals(['kernel.reset' => []], $container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar']], $container->getDefinition('baz')->getTags());
}

public function testCannotDecorateSyntheticService()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableIn 8000 terface;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableServiceDecorator;
use Symfony\Component\HttpKernel\Tests\Fixtures\MultiResettableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

Expand Down Expand Up @@ -81,4 +84,102 @@ public function testCompilerPassWithoutResetters()

$this->assertFalse($container->has('services_resetter'));
}

public function testDecoratedLastResettableService()
{
$container = new ContainerBuilder();
$container->register('services_resetter', ServicesResetter::class)
->setPublic(true)
->setArguments([null, []]);
$container->addCompilerPass(new ResettableServicePass());

$container->register('clearable_service', ClearableService::class)
->setFactory([ClearableService::class, 'create'])
->addTag('kernel.reset', ['method' => 'reset']);

$container->setAlias(ClearableInterface::class, new Alias('clearable_service', true));

$container->register('clearable_service_decorator', ClearableServiceDecorator::class)
->setDecoratedService(ClearableInterface::class)
->setArgument(0, new Reference('.inner'));

$container->register('clearable_service_decorator_2', ClearableServiceDecorator::class)
->setDecoratedService(ClearableInterface::class)
->setArgument(0, new Reference('.inner'));

$container->compile();

$container->get(ClearableInterface::class)->clear();

self::assertSame(1, ClearableService::$counter);
self::assertSame(2, ClearableServiceDecorator::$counter);

$container->get('services_resetter')->reset();
self::assertSame(0, ClearableService::$counter);
self::assertSame(2, ClearableServiceDecorator::$counter);
}

public function testDecoratedMiddleResettableService()
{
$container = new ContainerBuilder();
$container->register('services_resetter', ServicesResetter::class)
->setPublic(true)
->setArguments([null, []]);
$container->addCompilerPass(new ResettableServicePass());

$container->register('clearable_service', ClearableService::class)
->setFactory([ClearableService::class, 'create']);

$container->setAlias(ClearableInterface::class, new Alias('clearable_service', true));

$container->register('clearable_service_decorator', ClearableServiceDecorator::class)
->setDecoratedService(ClearableInterface::class)
->setArgument(0, new Reference('.inner'))
->addTag('kernel.reset', ['method' => 'reset']);

$container->register('clearable_service_decorator_2', ClearableServiceDecorator::class)
->setDecoratedService(ClearableInterface::class)
->setArgument(0, new Reference('.inner'));

$container->compile();

$container->get(ClearableInterface::class)->clear();

self::assertSame(1, ClearableService::$counter);
self::assertSame(2, ClearableServiceDecorator::$counter);

$container->get('services_resetter')->reset();
self::assertSame(1, ClearableService::$counter);
self::assertSame(0, ClearableServiceDecorator::$counter);
}

public function testDecoratedFirstResettableService()
{
$container = new ContainerBuilder();
$container->register('services_resetter', ServicesResetter::class)
->setPublic(true)
->setArguments([null, []]);
$container->addCompilerPass(new ResettableServicePass());

$container->register('clearable_service', ClearableService::class)
->setFactory([ClearableService::class, 'create']);

$container->setAlias(ClearableInterface::class, new Alias('clearable_service', true));

$container->register('clearable_service_decorator', ClearableServiceDecorator::class)
->setDecoratedService(ClearableInterface::class)
->setArgument(0, new Reference('.inner'))
->addTag('kernel.reset', ['method' => 'reset']);

$container->compile();

$container->get(ClearableInterface::class)->clear();

self::assertSame(1, ClearableService::$counter);
self::assertSame(1, ClearableServiceDecorator::$counter);

$container->get('services_resetter')->reset();
self::assertSame(1, ClearableService::$counter);
self::assertSame(0, ClearableServiceDecorator::$counter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

interface ClearableInterface
{
public function clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

class ClearableService
use Symfony\Contracts\Service\ResetInterface;

class ClearableService implements ClearableInterface, ResetInterface
{
public static $counter = 0;

public function clear()
{
++self::$counter;
}

public static function create()
{
return new self();
}

public function reset()
{
self::$counter = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Component< 7457 /span>\HttpKernel\Tests\Fixtures;

use Symfony\Contracts\Service\ResetInterface;

class ClearableServiceDecorator implements ClearableInterface, ResetInterface
{
public static $counter = 0;

private $clearableService;

public function __construct(ClearableInterface $clearableService)
{
$this->clearableService = $clearableService;
}

public function clear()
{
++self::$counter;

$this->clearableService->clear();
}

public function reset()
{
self::$counter = 0;
}
}
0