Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged branch fabpot/request-stack (PR #8904)
This PR was merged into the master branch. Discussion ---------- Synchronized Service alternative, backwards compatible This is a rebased version #7707. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7707 | License | MIT | Doc PR | symfony/symfony-docs#2956 Todo/Questions - [x] do we deprecate the synchronize feature (and removed it in 3.0)? - [x] deal with BC for listeners - [x] rename RequestContext as we already have a class with the same name in the Routing component? - [x] move RequestStack and RequestContext to HttpFoundation? - [x] update documentation Prototype for introducing a ``RequestContext`` in HttpKernel. This PR keeps the synchronized services feature, however introduces a ``RequestContext`` object additionally, that allows to avoid using synchronized service when injecting ``request_context`` instead of ``request`` into a service. The FrameworkBundle is modified such that it does not depend on synchronized services anymore. Users however can still depend on ``request``, activating the synchronized services feature. Features: * Introduced ``REQUEST_FINSHED`` (name is up for grabs) event to handle global state and environment cleanup that should not be done in ``RESPONSE``. Called in both exception or success case correctly * Changed listeners that were synchronized before to using ``onKernelRequestFinished`` and ``RequestContext`` to reset to the parent request (RouterListener + LocaleListener). * Changed ``FragmentHandler`` to use the RequestContext. Added some more tests for this class. * ``RequestStack`` is injected into the ``HttpKernel`` to handle the request finished event and push/pop the stack with requests. Todos: * RequestContext name clashes with Routing components RequestContext. Keep or make unique? * Name for Kernel Request Finished Event could be improved. Commits ------- 1b2ef74 [Security] made sure that the exception listener is always removed from the event dispatcher at the end of the request b1a062d moved RequestStack to HttpFoundation and removed RequestContext 93e60ea [HttpKernel] modified listeners to be BC with Symfony <2.4 018b719 [HttpKernel] tweaked the code f9b10ba [HttpKernel] renamed the kernel finished event a58a8a6 [HttpKernel] changed request_stack to a private service c55f1ea added a RequestStack class
- Loading branch information
Showing
24 changed files
with
530 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation; | ||
|
||
/** | ||
* Request stack that controls the lifecycle of requests. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
class RequestStack | ||
{ | ||
/** | ||
* @var Request[] | ||
*/ | ||
private $requests = array(); | ||
|
||
/** | ||
* Pushes a Request on the stack. | ||
* | ||
* This method should generally not be called directly as the stack | ||
* management should be taken care of by the application itself. | ||
*/ | ||
public function push(Request $request) | ||
{ | ||
$this->requests[] = $request; | ||
} | ||
|
||
/** | ||
* Pops the current request from the stack. | ||
* | ||
* This operation lets the current request go out of scope. | ||
* | ||
* This method should generally not be called directly as the stack | ||
* management should be taken care of by the application itself. | ||
* | ||
* @return Request | ||
*/ | ||
public function pop() | ||
{ | ||
if (!$this->requests) { | ||
throw new \LogicException('Unable to pop a Request as the stack is already empty.'); | ||
} | ||
|
||
return array_pop($this->requests); | ||
} | ||
|
||
/** | ||
* @return Request|null | ||
*/ | ||
public function getCurrentRequest() | ||
{ | ||
return end($this->requests) ?: null; | ||
} | ||
|
||
/** | ||
* Gets the master Request. | ||
* | ||
* Be warned that making your code aware of the master request | ||
* might make it un-compatible with other features of your framework | ||
* like ESI support. | ||
* | ||
* @return Request|null | ||
*/ | ||
public function getMasterRequest() | ||
{ | ||
if (!$this->requests) { | ||
return null; | ||
} | ||
|
||
return $this->requests[0]; | ||
} | ||
|
||
/** | ||
* Returns the parent request of the current. | ||
* | ||
* Be warned that making your code aware of the parent request | ||
* might make it un-compatible with other features of your framework | ||
* like ESI support. | ||
* | ||
* If current Request is the master request, it returns null. | ||
* | ||
* @return Request|null | ||
*/ | ||
public function getParentRequest() | ||
{ | ||
$pos = count($this->requests) - 2; | ||
|
||
if (!isset($this->requests[$pos])) { | ||
return null; | ||
} | ||
|
||
return $this->requests[$pos]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.4.0 | ||
----- | ||
|
||
* added the KernelEvents::FINISH_REQUEST event | ||
|
||
2.3.0 | ||
----- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/HttpKernel/Event/FinishRequestEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Event; | ||
|
||
/** | ||
* Triggered whenever a request is fully processed. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
class FinishRequestEvent extends KernelEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.