-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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) | ||
->setArguments(array(new IteratorArgument($services), $methods)); | ||
} | ||
|
||
$container->findDefinition(ServiceResetListener::class) | ||
->replaceArgument(0, new IteratorArgument($services)) | ||
->replaceArgument(1, $methods); | ||
} | ||
} |
This file was deleted.
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could add an interface on the |
||
{ | ||
private $resetNeeded = false; | ||
private $resettableServices; | ||
private $resetMethods; | ||
|
||
public function __construct(\Traversable $resettableServices, array $resetMethods) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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)? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
}, | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to achieve what?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 fineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright