[HttpKernel] tweaked the code
This commit is contained in:
parent
f9b10ba1d5
commit
018b71936f
|
@ -37,9 +37,9 @@ class ContainerAwareHttpKernel extends HttpKernel
|
|||
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
|
||||
* @param ContainerInterface $container A ContainerInterface instance
|
||||
* @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
|
||||
* @param RequestStack $requestStack A stack for master/sub requests
|
||||
* @param RequestStack $requestStack A stack for master/sub requests
|
||||
*/
|
||||
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack)
|
||||
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack = null)
|
||||
{
|
||||
parent::__construct($dispatcher, $controllerResolver, $requestStack);
|
||||
|
||||
|
|
|
@ -37,6 +37,27 @@ class LocaleListener implements EventSubscriberInterface
|
|||
$this->router = $router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current Request.
|
||||
*
|
||||
* This method was used to synchronize the Request, but as the HttpKernel
|
||||
* is doing that automatically now, you should never be called it directly.
|
||||
* It is kept public for BC with the 2.3 version.
|
||||
*
|
||||
* @param Request|null $request A Request instance
|
||||
*
|
||||
* @deprecated Deprecated since version 2.4, to be removed in 3.0.
|
||||
*/
|
||||
public function setRequest(Request $request = null)
|
||||
{
|
||||
if (null === $request) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setLocale($request);
|
||||
$this->setRouterContext($request);
|
||||
}
|
||||
|
||||
public function onKernelRequest(GetResponseEvent $event)
|
||||
{
|
||||
$request = $event->getRequest();
|
||||
|
@ -48,22 +69,9 @@ class LocaleListener implements EventSubscriberInterface
|
|||
|
||||
public function onKernelFinishRequest(FinishRequestEvent $event)
|
||||
{
|
||||
$this->resetRouterContext();
|
||||
}
|
||||
|
||||
private function resetRouterContext()
|
||||
{
|
||||
if ($this->requestContext === null) {
|
||||
return;
|
||||
if (null !== $parentRequest = $this->requestContext->getParentRequest()) {
|
||||
$this->setRouterContext($parentRequest);
|
||||
}
|
||||
|
||||
$parentRequest = $this->requestContext->getParentRequest();
|
||||
|
||||
if ($parentRequest === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->setRouterContext($parentRequest);
|
||||
}
|
||||
|
||||
private function setLocale(Request $request)
|
||||
|
|
|
@ -68,14 +68,15 @@ class RouterListener implements EventSubscriberInterface
|
|||
/**
|
||||
* Sets the current Request.
|
||||
*
|
||||
* The application should call this method whenever the Request
|
||||
* object changes (entering a Request scope for instance, but
|
||||
* also when leaving a Request scope -- especially when they are
|
||||
* nested).
|
||||
* This method was used to synchronize the Request, but as the HttpKernel
|
||||
* is doing that automatically now, you should never be called it directly.
|
||||
* It is kept public for BC with the 2.3 version.
|
||||
*
|
||||
* @param Request|null $request A Request instance
|
||||
*
|
||||
* @deprecated Deprecated since version 2.4, to be moved to a private function in 3.0.
|
||||
*/
|
||||
private function populateRoutingContext(Request $request = null)
|
||||
public function setRequest(Request $request = null)
|
||||
{
|
||||
if (null !== $request && $this->request !== $request) {
|
||||
$this->context->fromRequest($request);
|
||||
|
@ -83,10 +84,10 @@ class RouterListener implements EventSubscriberInterface
|
|||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function onKernelFinishRequest(FinishRequestEvent $event)
|
||||
{
|
||||
$this->populateRoutingContext($this->kernelContext->getParentRequest());
|
||||
}
|
||||
public function onKernelFinishRequest(FinishRequestEvent $event)
|
||||
{
|
||||
$this->setRequest($this->kernelContext->getParentRequest());
|
||||
}
|
||||
|
||||
public function onKernelRequest(GetResponseEvent $event)
|
||||
{
|
||||
|
@ -95,7 +96,7 @@ class RouterListener implements EventSubscriberInterface
|
|||
// initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
|
||||
// we call setRequest even if most of the time, it has already been done to keep compatibility
|
||||
// with frameworks which do not use the Symfony service container
|
||||
$this->populateRoutingContext($request);
|
||||
$this->setRequest($request);
|
||||
|
||||
if ($request->attributes->has('_controller')) {
|
||||
// routing is already done
|
||||
|
|
|
@ -85,6 +85,10 @@ class FragmentHandler
|
|||
throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
|
||||
}
|
||||
|
||||
if (null === $this->context->getCurrentRequest()) {
|
||||
throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
|
||||
}
|
||||
|
||||
return $this->deliver($this->renderers[$renderer]->render($uri, $this->context->getCurrentRequest(), $options));
|
||||
}
|
||||
|
||||
|
@ -103,8 +107,7 @@ class FragmentHandler
|
|||
protected function deliver(Response $response)
|
||||
{
|
||||
if (!$response->isSuccessful()) {
|
||||
$request = $this->context->getCurrentRequest();
|
||||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
|
||||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->context->getCurrentRequest()->getUri(), $response->getStatusCode()));
|
||||
}
|
||||
|
||||
if (!$response instanceof StreamedResponse) {
|
||||
|
|
|
@ -64,9 +64,9 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
|||
try {
|
||||
return $this->handleRaw($request, $type);
|
||||
} catch (\Exception $e) {
|
||||
$this->finishRequest($request, $type);
|
||||
|
||||
if (false === $catch) {
|
||||
$this->finishRequest($request, $type);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
|
@ -170,16 +170,14 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Publish event finished event, then pop the request from the stack.
|
||||
* Publishes the finish request event, then pop the request from the stack.
|
||||
*
|
||||
* Note: Order of the operations is important here, otherwise operations
|
||||
* such as {@link RequestStack::getParentRequest()} can lead to weird
|
||||
* results.
|
||||
* Note that the order of the operations is important here, otherwise
|
||||
* operations such as {@link RequestStack::getParentRequest()} can lead to
|
||||
* weird results.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param int $type
|
||||
*
|
||||
* @return void
|
||||
* @param int $type
|
||||
*/
|
||||
private function finishRequest(Request $request, $type)
|
||||
{
|
||||
|
@ -207,6 +205,8 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
|
|||
$e = $event->getException();
|
||||
|
||||
if (!$event->hasResponse()) {
|
||||
$this->finishRequest($request, $type);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ class RequestContext
|
|||
}
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
* @return Request|null
|
||||
*/
|
||||
public function getCurrentRequest()
|
||||
{
|
||||
|
@ -38,7 +38,7 @@ class RequestContext
|
|||
}
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
* @return Request|null
|
||||
*/
|
||||
public function getMasterRequest()
|
||||
{
|
||||
|
|
|
@ -16,8 +16,6 @@ use Symfony\Component\HttpFoundation\Request;
|
|||
/**
|
||||
* Request stack that controls the lifecycle of requests.
|
||||
*
|
||||
* Notifies services of changes in the stack.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
*/
|
||||
class RequestStack
|
||||
|
@ -33,7 +31,7 @@ class RequestStack
|
|||
}
|
||||
|
||||
/**
|
||||
* Pop the current request from the stack.
|
||||
* Pops the current request from the stack.
|
||||
*
|
||||
* This operation lets the current request go out of scope.
|
||||
*
|
||||
|
@ -41,6 +39,10 @@ class RequestStack
|
|||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -65,11 +67,11 @@ class RequestStack
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the parent request of the current.
|
||||
* Returns the parent request of the current.
|
||||
*
|
||||
* If current Request is the master request, method returns null.
|
||||
* If current Request is the master request, it returns null.
|
||||
*
|
||||
* @return Request
|
||||
* @return Request|null
|
||||
*/
|
||||
public function getParentRequest()
|
||||
{
|
||||
|
|
Reference in New Issue