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

Skip to content

Commit 4501a36

Browse files
[HttpKernel] Move services reset to Kernel
1 parent fdac9e3 commit 4501a36

File tree

9 files changed

+164
-170
lines changed

9 files changed

+164
-170
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@
7575
<tag name="config_cache.resource_checker" priority="-990" />
7676
</service>
7777

78-
<service id="Symfony\Component\HttpKernel\EventListener\ServiceResetListener">
79-
<argument /> <!-- ResettableServicePass will inject an iterator of initialized services here ($serviceId => $serviceInstance) -->
80-
<argument type="collection" /> <!-- ResettableServicePass will inject an array of reset methods here ($serviceId => $method) -->
81-
<tag name="kernel.event_subscriber" />
82-
</service>
78+
<service id="services_resetter" class="Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter" public="true" />
8379
</services>
8480
</container>

src/Symfony/Component/HttpKernel/DependencyInjection/ResettableServicePass.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1919
use Symfony\Component\DependencyInjection\Reference;
20-
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
2120

2221
/**
2322
* @author Alexander M. Turek <[email protected]>
@@ -26,9 +25,6 @@ class ResettableServicePass implements CompilerPassInterface
2625
{
2726
private $tagName;
2827

29-
/**
30-
* @param string $tagName
31-
*/
3228
public function __construct($tagName = 'kernel.reset')
3329
{
3430
$this->tagName = $tagName;
@@ -39,7 +35,7 @@ public function __construct($tagName = 'kernel.reset')
3935
*/
4036
public function process(ContainerBuilder $container)
4137
{
42-
if (!$container->has(ServiceResetListener::class)) {
38+
if (!$container->has('services_resetter')) {
4339
return;
4440
}
4541

@@ -57,13 +53,13 @@ public function process(ContainerBuilder $container)
5753
}
5854

5955
if (empty($services)) {
60-
$container->removeDefinition(ServiceResetListener::class);
56+
$container->removeDefinition('services_resetter');
6157

6258
return;
6359
}
6460

65-
$container->findDefinition(ServiceResetListener::class)
66-
->replaceArgument(0, new IteratorArgument($services))
67-
->replaceArgument(1, $methods);
61+
$container->findDefinition('services_resetter')
62+
->setArgument(0, new IteratorArgument($services))
63+
->setArgument(1, $methods);
6864
}
6965
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/**
15+
* Resets provided services.
16+
*
17+
* @author Alexander M. Turek <[email protected]>
18+
* @author Nicolas Grekas <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
class ServicesResetter
23+
{
24+
private $resettableServices;
25+
private $resetMethods;
26+
27+
public function __construct(\Traversable $resettableServices, array $resetMethods)
28+
{
29+
$this->resettableServices = $resettableServices;
30+
$this->resetMethods = $resetMethods;
31+
}
32+
33+
public function reset()
34+
{
35+
foreach ($this->resettableServices as $id => $service) {
36+
$service->{$this->resetMethods[$id]}();
37+
}
38+
}
39+
}

src/Symfony/Component/HttpKernel/EventListener/ServiceResetListener.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
6464

6565
private $projectDir;
6666
private $warmupDir;
67+
private $requestStackSize = 0;
68+
private $resetServices = false;
6769

6870
const VERSION = '3.4.0-DEV';
6971
const VERSION_ID = 30400;
@@ -99,6 +101,8 @@ public function __clone()
99101

100102
$this->booted = false;
101103
$this->container = null;
104+
$this->requestStackSize = 0;
105+
$this->resetServices = false;
102106
}
103107

104108
/**
@@ -107,8 +111,20 @@ public function __clone()
107111
public function boot()
108112
{
109113
if (true === $this->booted) {
114+
if (!$this->requestStackSize && $this->resetServices) {
115+
if ($this->container->has('services_resetter')) {
116+
$this->container->get('services_resetter')->reset();
117+
}
118+
$this->resetServices = false;
119+
}
120+
110121
return;
111122
}
123+
if ($this->debug && !isset($_SERVER['SHELL_VERBOSITY'])) {
124+
putenv('SHELL_VERBOSITY=3');
125+
$_ENV['SHELL_VERBOSITY'] = 3;
126+
$_SERVER['SHELL_VERBOSITY'] = 3;
127+
}
112128

113129
if ($this->loadClassCache) {
114130
$this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]);
@@ -169,24 +185,24 @@ public function shutdown()
169185
}
170186

171187
$this->container = null;
188+
$this->requestStackSize = 0;
189+
$this->resetServices = false;
172190
}
173191

174192
/**
175193
* {@inheritdoc}
176194
*/
177195
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
178196
{
179-
if (false === $this->booted) {
180-
if ($this->debug && !isset($_SERVER['SHELL_VERBOSITY'])) {
181-
putenv('SHELL_VERBOSITY=3');
182-
$_ENV['SHELL_VERBOSITY'] = 3;
183-
$_SERVER['SHELL_VERBOSITY'] = 3;
184-
}
197+
$this->boot();
198+
++$this->requestStackSize;
199+
$this->resetServices = true;
185200

186-
$this->boot();
201+
try {
202+
return $this->getHttpKernel()->handle($request, $type, $catch);
203+
} finally {
204+
--$this->requestStackSize;
187205
}
188-
189-
return $this->getHttpKernel()->handle($request, $type, $catch);
190206
}
191207

192208
/**

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ResettableServicePassTest.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Symfony\Component\DependencyInjection\ContainerInterface;
99
use Symfony\Component\DependencyInjection\Reference;
1010
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
11-
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
11+
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
1212
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
1313
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
1414

@@ -24,14 +24,14 @@ public function testCompilerPass()
2424
->setPublic(true)
2525
->addTag('kernel.reset', array('method' => 'clear'));
2626

27-
$container->register(ServiceResetListener::class)
27+
$container->register('services_resetter', ServicesResetter::class)
2828
->setPublic(true)
2929
->setArguments(array(null, array()));
30-
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));
30+
$container->addCompilerPass(new ResettableServicePass());
3131

3232
$container->compile();
3333

34-
$definition = $container->getDefinition(ServiceResetListener::class);
34+
$definition = $container->getDefinition('services_resetter');
3535

3636
$this->assertEquals(
3737
array(
@@ -57,32 +57,22 @@ public function testMissingMethod()
5757
$container = new ContainerBuilder();
5858
$container->register(ResettableService::class)
5959
->addTag('kernel.reset');
60-
$container->register(ServiceResetListener::class)
60+
$container->register('services_resetter', ServicesResetter::class)
6161
->setArguments(array(null, array()));
62-
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));
62+
$container->addCompilerPass(new ResettableServicePass());
6363

6464
$container->compile();
6565
}
6666

6767
public function testCompilerPassWithoutResetters()
6868
{
6969
$container = new ContainerBuilder();
70-
$container->register(ServiceResetListener::class)
70+
$container->register('services_resetter', ServicesResetter::class)
7171
->setArguments(array(null, array()));
7272
$container->addCompilerPass(new ResettableServicePass());
7373

7474
$container->compile();
7575

76-
$this->assertFalse($container->has(ServiceResetListener::class));
77-
}
78-
79-
public function testCompilerPassWithoutListener()
80-
{
81-
$container = new ContainerBuilder();
82-
$container->addCompilerPass(new ResettableServicePass());
83-
84-
$container->compile();
85-
86-
$this->assertFalse($container->has(ServiceResetListener::class));
76+
$this->assertFalse($container->has('services_resetter'));
8777
}
8878
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
16+
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
17+
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
18+
19+
class ServicesResetterTest extends TestCase
20+
{
21+
protected function setUp()
22+
{
23+
ResettableService::$counter = 0;
24+
ClearableService::$counter = 0;
25+
}
26+
27+
public function testResetServices()
28+
{
29+
$resetter = new ServicesResetter(new \ArrayIterator(array(
30+
'id1' => new ResettableService(),
31+
'id2' => new ClearableService(),
32+
)), array(
33+
'id1' => 'reset',
34+
'id2' => 'clear',
35+
));
36+
37+
$resetter->reset();
38+
39+
$this->assertEquals(1, ResettableService::$counter);
40+
$this->assertEquals(1, ClearableService::$counter);
41+
}
42+
}

0 commit comments

Comments
 (0)