Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac7b2c6

Browse files
committed
A DI tag for resettable services.
1 parent b1b6860 commit ac7b2c6

File tree

9 files changed

+327
-0
lines changed

9 files changed

+327
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass;
3434
use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
3535
use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass;
36+
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
3637
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
3738
use Symfony\Component\Routing\DependencyInjection\RoutingResolverPass;
3839
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
@@ -117,6 +118,7 @@ public function build(ContainerBuilder $container)
117118
$container->addCompilerPass(new CachePoolPrunerPass(), PassConfig::TYPE_AFTER_REMOVING);
118119
$this->addCompilerPassIfExists($container, FormPass::class);
119120
$container->addCompilerPass(new WorkflowGuardListenerPass());
121+
$container->addCompilerPass(new ResettableServicePass('kernel.reset'), PassConfig::TYPE_AFTER_REMOVING);
120122

121123
if ($container->getParameter('kernel.debug')) {
122124
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@
7474
<service id="Symfony\Component\Config\Resource\SelfCheckingResourceChecker">
7575
<tag name="config_cache.resource_checker" priority="-990" />
7676
</service>
77+
78+
<service id="Symfony\Component\HttpKernel\EventListener\ServiceResetListener">
79+
<tag name="kernel.event_subscriber" />
80+
</service>
7781
</services>
7882
</container>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
20+
21+
/**
22+
* @author Alexander M. Turek <[email protected]>
23+
*/
24+
class ResettableServicePass implements CompilerPassInterface
25+
{
26+
const RESET_TAG = 'kernel.reset';
27+
28+
/**
29+
* @var string
30+
*/
31+
private $tagName;
32+
33+
/**
34+
* @param string $tagName
35+
*/
36+
public function __construct($tagName)
37+
{
38+
$this->tagName = $tagName;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function process(ContainerBuilder $container)
45+
{
46+
$services = $methods = array();
47+
48+
foreach ($container->findTaggedServiceIds($this->tagName, true) as $id => $tags) {
49+
$services[$id] = new Reference($id, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE);
50+
$methods[$id] = array();
51+
52+
foreach ($tags as $attributes) {
53+
if (!isset($attributes['method'])) {
54+
throw new \RuntimeException(
55+
sprintf('Tag %s requires the attribute method to be set.', $this->tagName)
56+
);
57+
}
58+
59+
$methods[$id][] = $attributes['method'];
60+
}
61+
}
62+
63+
$container->getDefinition(ServiceResetListener::class)
64+
->setArguments(array(
65+
new IteratorArgument($services),
66+
$methods,
67+
));
68+
}
69+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpKernel\KernelEvents;
16+
17+
/**
18+
* Clean up services between requests.
19+
*
20+
* @author Alexander M. Turek <[email protected]>
21+
*/
22+
class ServiceResetListener implements EventSubscriberInterface
23+
{
24+
/**
25+
* @var \Traversable
26+
*/
27+
private $services;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $resetMethods;
33+
34+
public function __construct(\Traversable $services, array $resetMethods)
35+
{
36+
$this->services = $services;
37+
$this->resetMethods = $resetMethods;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public static function getSubscribedEvents()
44+
{
45+
return array(
46+
KernelEvents::TERMINATE => array('onKernelTerminate', -2048),
47+
);
48+
}
49+
50+
public function onKernelTerminate()
51+
{
52+
foreach ($this->services as $id => $service) {
53+
foreach ($this->resetMethods[$id] as $method) {
54+
call_user_func(array($service, $method));
55+
}
56+
}
57+
}
58+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
use Symfony\Component\DependencyInjection\Reference;
10+
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
11+
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
12+
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
13+
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableClearableService;
14+
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
15+
16+
class ResettableServicePassTest extends TestCase
17+
{
18+
public function testCompilerPass()
19+
{
20+
$container = new ContainerBuilder();
21+
$container->register('one', ResettableService::class)
22+
->addTag('kernel.reset', array('method' => 'reset'));
23+
$container->register('two', ClearableService::class)
24+
->addTag('kernel.reset', array('method' => 'clear'));
25+
$container->register('three', ResettableClearableService::class)
26+
->addTag('kernel.reset', array('method' => 'reset'))
27+
->addTag('kernel.reset', array('method' => 'clear'));
28+
29+
$container->register(ServiceResetListener::class);
30+
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));
31+
32+
$container->compile();
33+
34+
$definition = $container->getDefinition(ServiceResetListener::class);
35+
36+
$this->assertEquals(
37+
array(
38+
new IteratorArgument(array(
39+
'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
40+
'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
41+
'three' => new Reference('three', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
42+
)),
43+
array(
44+
'one' => array('reset'),
45+
'two' => array('clear'),
46+
'three' => array('reset', 'clear'),
47+
),
48+
),
49+
$definition->getArguments()
50+
);
51+
}
52+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
use Symfony\Component\DependencyInjection\Reference;
10+
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
11+
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
12+
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableClearableService;
13+
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
14+
15+
class ServiceResetSubscriberTest extends TestCase
16+
{
17+
/**
18+
* @before
19+
*/
20+
public function resetCounters()
21+
{
22+
ResettableService::$counter = 0;
23+
ClearableService::$counter = 0;
24+
ResettableClearableService::$counter = 0;
25+
}
26+
27+
public function testResetServicesNoOp()
28+
{
29+
$container = $this->buildContainer();
30+
$container->get('reset_subscriber')->onKernelTerminate();
31+
32+
$this->assertEquals(0, ResettableService::$counter);
33+
$this->assertEquals(0, ClearableService::$counter);
34+
$this->assertEquals(0, ResettableClearableService::$counter);
35+
}
36+
37+
public function testResetServicesPartially()
38+
{
39+
$container = $this->buildContainer();
40+
$container->get('one');
41+
$container->get('reset_subscriber')->onKernelTerminate();
42+
43+
$this->assertEquals(1, ResettableService::$counter);
44+
$this->assertEquals(0, ClearableService::$counter);
45+
$this->assertEquals(0, ResettableClearableService::$counter);
46+
}
47+
48+
public function testResetServicesTwice()
49+
{
50+
$container = $this->buildContainer();
51+
$container->get('one');
52+
$container->get('reset_subscriber')->onKernelTerminate();
53+
$container->get('two');
54+
$container->get('reset_subscriber')->onKernelTerminate();
55+
56+
$this->assertEquals(2, ResettableService::$counter);
57+
$this->assertEquals(1, ClearableService::$counter);
58+
$this->assertEquals(0, ResettableClearableService::$counter);
59+
}
60+
61+
public function testMultipleResetMethods()
62+
{
63+
$container = $this->buildContainer();
64+
$container->get('three');
65+
$container->get('reset_subscriber')->onKernelTerminate();
66+
67+
$this->assertEquals(0, ResettableService::$counter);
68+
$this->assertEquals(0, ClearableService::$counter);
69+
$this->assertEquals(2, ResettableClearableService::$counter);
70+
}
71+
72+
/**
73+
* @return ContainerBuilder
74+
*/
75+
private function buildContainer()
76+
{
77+
$container = new ContainerBuilder();
78+
$container->register('one', ResettableService::class);
79+
$container->register('two', ClearableService::class);
80+
$container->register('three', ResettableClearableService::class);
81+
82+
$container->register('reset_subscriber', ServiceResetListener::class)
83+
->addArgument(new IteratorArgument(array(
84+
'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
85+
'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
86+
'three' => new Reference('three', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
87+
)))
88+
->addArgument(array(
89+
'one' => array('reset'),
90+
'two' => array('clear'),
91+
'three' => array('clear', 'reset'),
92+
));
93+
94+
$container->compile();
95+
96+
return $container;
97+
}
98+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
class ClearableService
6+
{
7+
public static $counter = 0;
8+
9+
public function clear()
10+
{
11+
++self::$counter;
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
class ResettableClearableService
6+
{
7+
public static $counter = 0;
8+
9+
public function reset()
10+
{
11+
++self::$counter;
12+
}
13+
14+
public function clear()
15+
{
16+
++self::$counter;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
4+
5+
class ResettableService
6+
{
7+
public static $counter = 0;
8+
9+
public function reset()
10+
{
11+
++self::$counter;
12+
}
13+
}

0 commit comments

Comments
 (0)