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

Skip to content

[HttpFoundation][HttpKernel] Move ServiceResetListener logic to RequestStack #24689

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 @@ -74,11 +74,5 @@
<service id="Symfony\Component\Config\Resource\SelfCheckingResourceChecker">
<tag name="config_cache.resource_checker" priority="-990" />
</service>

<service id="Symfony\Component\HttpKernel\EventListener\ServiceResetListener">
<argument /> <!-- ResettableServicePass will inject an iterator of initialized services here ($serviceId => $serviceInstance) -->
<argument type="collection" /> <!-- ResettableServicePass will inject an array of reset methods here ($serviceId => $method) -->
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
use Symfony\Component\HttpKernel\ResettingRequestStack;

/**
* @author Alexander M. Turek <[email protected]>
Expand All @@ -39,7 +39,7 @@ public function __construct($tagName = 'kernel.reset')
*/
public function process(ContainerBuilder $container)
{
if (!$container->has(ServiceResetListener::class)) {
if (!$container->has('request_stack')) {
return;
}

Expand All @@ -56,14 +56,9 @@ public function process(ContainerBuilder $container)
$methods[$id] = $attributes['method'];
}

if (empty($services)) {
$container->removeDefinition(ServiceResetListener::class);

return;
if (!empty($services)) {
$container->register('request_stack', ResettingRequestStack::class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we decorate the existing request stack rather than replacing it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to achieve what?

Copy link
Contributor

@ogizanagi ogizanagi Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone might already have replaced the request stack by his own, or decorated it for his own needs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, indeed. So I'm going to add a runtime decorating proxy.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait no! that's not how decoration works! So there is no issue with it here.
We don't guarantee BC for ppl that just replace services (otherwise we would be in serious trouble moving forward).
So as is is all good, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A request_stack decorator would just end up with a ResettingRequestStack instance as inner since this pass runs before decoration happens. Seems fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

->setArguments(array(new IteratorArgument($services), $methods));
}

$container->findDefinition(ServiceResetListener::class)
->replaceArgument(0, new IteratorArgument($services))
->replaceArgument(1, $methods);
}
}

This file was deleted.

49 changes: 49 additions & 0 deletions src/Symfony/Component/HttpKernel/ResettingRequestStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Resets services on second master requests.
*
* @author Nicolas Grekas <[email protected]>
*/
class ResettingRequestStack extends RequestStack
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add an interface on the RequestStack and make this extension deprecated in 4.1 to promote composition over inheritance. Should probably also get @final

{
private $resetNeeded = false;
private $resettableServices;
private $resetMethods;

public function __construct(\Traversable $resettableServices, array $resetMethods)
Copy link
Contributor

@dmaicher dmaicher Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously the services were instantiated lazily together with the listener on dispatch of kernel.terminate, right? Seems now the services would be instantiated before the first master request is handled already, even though they are not necessarily all used during the request.

Means now its affecting performance maybe a bit more than before? I mean before at least it was done after the response was streamed back to the client. Now it will delay handling the first request in some cases maybe.

Maybe its possible to do it completely lazy only if they are actually reset (in case there is a second master request)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iterator yields only instantiated services, so all is fine.

{
$this->resettableServices = $resettableServices;
$this->resetMethods = $resetMethods;
}

/**
* {@inheritdoc}
*/
public function push(Request $request)
{
if ($this->resetNeeded && !$this->getCurrentRequest()) {
foreach ($this->resettableServices as $id => $service) {
$service->{$this->resetMethods[$id]}();
}
} else {
$this->resetNeeded = true;
}

parent::push($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
use Symfony\Component\HttpKernel\EventListener\ServiceResetListener;
use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

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

$container->register(ServiceResetListener::class)
->setPublic(true)
->setArguments(array(null, array()));
$container->register('request_stack', RequestStack::class)
->setPublic(true);
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));

$container->compile();

$definition = $container->getDefinition(ServiceResetListener::class);
$definition = $container->getDefinition('request_stack');

$this->assertEquals(
array(
Expand All @@ -57,32 +56,10 @@ public function testMissingMethod()
$container = new ContainerBuilder();
$container->register(ResettableService::class)
->addTag('kernel.reset');
$container->register(ServiceResetListener::class)
$container->register('request_stack', RequestStack::class)
->setArguments(array(null, array()));
$container->addCompilerPass(new ResettableServicePass('kernel.reset'));

$container->compile();
}

public function testCompilerPassWithoutResetters()
{
$container = new ContainerBuilder();
$container->register(ServiceResetListener::class)
->setArguments(array(null, array()));
$container->addCompilerPass(new ResettableServicePass());

$container->compile();

$this->assertFalse($container->has(ServiceResetListener::class));
}

public function testCompilerPassWithoutListener()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new ResettableServicePass());

$container->compile();

$this->assertFalse($container->has(ServiceResetListener::class));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\ResettingRequestStack;
use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;

class ResettingRequestStackTest extends TestCase
{
protected function setUp()
{
ResettableService::$counter = 0;
}

public function testResetServicesNoOp()
{
$requestStack = new ResettingRequestStack(new \ArrayIterator(array(new ResettableService())), array('reset'));

$masterRequest = Request::create('/foo');

$requestStack->push($masterRequest);

$this->assertEquals(0, ResettableService::$counter);
}

public function testResetServices()
{
$requestStack = new ResettingRequestStack(new \ArrayIterator(array(new ResettableService())), array('reset'));

$masterRequest = Request::create('/foo');

$requestStack->push($masterRequest);
$this->assertEquals(0, ResettableService::$counter);

$requestStack->pop();
$this->assertEquals(0, ResettableService::$counter);

$requestStack->push($masterRequest);
$this->assertEquals(1, ResettableService::$counter);

$requestStack->push($masterRequest);
$this->assertEquals(1, ResettableService::$counter);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^5.5.9|>=7.0.8",
"symfony/event-dispatcher": "~2.8|~3.0|~4.0",
"symfony/http-foundation": "^3.3.11|~4.0",
"symfony/http-foundation": "^3.4|~4.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transitively, this is already the lowest version

"symfony/debug": "~2.8|~3.0|~4.0",
"psr/log": "~1.0"
},
Expand Down