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

Skip to content

Commit 018b719

Browse files
committed
[HttpKernel] tweaked the code
1 parent f9b10ba commit 018b719

File tree

7 files changed

+60
-46
lines changed

7 files changed

+60
-46
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class ContainerAwareHttpKernel extends HttpKernel
3737
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
3838
* @param ContainerInterface $container A ContainerInterface instance
3939
* @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
40-
* @param RequestStack $requestStack A stack for master/sub requests
40+
* @param RequestStack $requestStack A stack for master/sub requests
4141
*/
42-
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack)
42+
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack = null)
4343
{
4444
parent::__construct($dispatcher, $controllerResolver, $requestStack);
4545

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ public function __construct($defaultLocale = 'en', RequestContext $requestContex
3737
$this->router = $router;
3838
}
3939

40+
/**
41+
* Sets the current Request.
42+
*
43+
* This method was used to synchronize the Request, but as the HttpKernel
44+
* is doing that automatically now, you should never be called it directly.
45+
* It is kept public for BC with the 2.3 version.
46+
*
47+
* @param Request|null $request A Request instance
48+
*
49+
* @deprecated Deprecated since version 2.4, to be removed in 3.0.
50+
*/
51+
public function setRequest(Request $request = null)
52+
{
53+
if (null === $request) {
54+
return;
55+
}
56+
57+
$this->setLocale($request);
58+
$this->setRouterContext($request);
59+
}
60+
4061
public function onKernelRequest(GetResponseEvent $event)
4162
{
4263
$request = $event->getRequest();
@@ -48,22 +69,9 @@ public function onKernelRequest(GetResponseEvent $event)
4869

4970
public function onKernelFinishRequest(FinishRequestEvent $event)
5071
{
51-
$this->resetRouterContext();
52-
}
53-
54-
private function resetRouterContext()
55-
{
56-
if ($this->requestContext === null) {
57-
return;
58-
}
59-
60-
$parentRequest = $this->requestContext->getParentRequest();
61-
62-
if ($parentRequest === null) {
63-
return;
72+
if (null !== $parentRequest = $this->requestContext->getParentRequest()) {
73+
$this->setRouterContext($parentRequest);
6474
}
65-
66-
$this->setRouterContext($parentRequest);
6775
}
6876

6977
private function setLocale(Request $request)

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,26 @@ public function __construct($matcher, KernelRequestContext $kernelContext, Reque
6868
/**
6969
* Sets the current Request.
7070
*
71-
* The application should call this method whenever the Request
72-
* object changes (entering a Request scope for instance, but
73-
* also when leaving a Request scope -- especially when they are
74-
* nested).
71+
* This method was used to synchronize the Request, but as the HttpKernel
72+
* is doing that automatically now, you should never be called it directly.
73+
* It is kept public for BC with the 2.3 version.
7574
*
7675
* @param Request|null $request A Request instance
76+
*
77+
* @deprecated Deprecated since version 2.4, to be moved to a private function in 3.0.
7778
*/
78-
private function populateRoutingContext(Request $request = null)
79+
public function setRequest(Request $request = null)
7980
{
8081
if (null !== $request && $this->request !== $request) {
8182
$this->context->fromRequest($request);
8283
}
8384
$this->request = $request;
8485
}
8586

86-
public function onKernelFinishRequest(FinishRequestEvent $event)
87-
{
88-
$this->populateRoutingContext($this->kernelContext->getParentRequest());
89-
}
87+
public function onKernelFinishRequest(FinishRequestEvent $event)
88+
{
89+
$this->setRequest($this->kernelContext->getParentRequest());
90+
}
9091

9192
public function onKernelRequest(GetResponseEvent $event)
9293
{
@@ -95,7 +96,7 @@ public function onKernelRequest(GetResponseEvent $event)
9596
// initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
9697
// we call setRequest even if most of the time, it has already been done to keep compatibility
9798
// with frameworks which do not use the Symfony service container
98-
$this->populateRoutingContext($request);
99+
$this->setRequest($request);
99100

100101
if ($request->attributes->has('_controller')) {
101102
// routing is already done

src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public function render($uri, $renderer = 'inline', array $options = array())
8585
throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
8686
}
8787

88+
if (null === $this->context->getCurrentRequest()) {
89+
throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
90+
}
91+
8892
return $this->deliver($this->renderers[$renderer]->render($uri, $this->context->getCurrentRequest(), $options));
8993
}
9094

@@ -103,8 +107,7 @@ public function render($uri, $renderer = 'inline', array $options = array())
103107
protected function deliver(Response $response)
104108
{
105109
if (!$response->isSuccessful()) {
106-
$request = $this->context->getCurrentRequest();
107-
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
110+
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->context->getCurrentRequest()->getUri(), $response->getStatusCode()));
108111
}
109112

110113
if (!$response instanceof StreamedResponse) {

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
6464
try {
6565
return $this->handleRaw($request, $type);
6666
} catch (\Exception $e) {
67-
$this->finishRequest($request, $type);
68-
6967
if (false === $catch) {
68+
$this->finishRequest($request, $type);
69+
7070
throw $e;
7171
}
7272

@@ -170,16 +170,14 @@ private function filterResponse(Response $response, Request $request, $type)
170170
}
171171

172172
/**
173-
* Publish event finished event, then pop the request from the stack.
173+
* Publishes the finish request event, then pop the request from the stack.
174174
*
175-
* Note: Order of the operations is important here, otherwise operations
176-
* such as {@link RequestStack::getParentRequest()} can lead to weird
177-
* results.
175+
* Note that the order of the operations is important here, otherwise
176+
* operations such as {@link RequestStack::getParentRequest()} can lead to
177+
* weird results.
178178
*
179179
* @param Request $request
180-
* @param int $type
181-
*
182-
* @return void
180+
* @param int $type
183181
*/
184182
private function finishRequest(Request $request, $type)
185183
{
@@ -207,6 +205,8 @@ private function handleException(\Exception $e, $request, $type)
207205
$e = $event->getException();
208206

209207
if (!$event->hasResponse()) {
208+
$this->finishRequest($request, $type);
209+
210210
throw $e;
211211
}
212212

src/Symfony/Component/HttpKernel/RequestContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public function __construct(RequestStack $stack)
3030
}
3131

3232
/**
33-
* @return Request
33+
* @return Request|null
3434
*/
3535
public function getCurrentRequest()
3636
{
3737
return $this->stack->getCurrentRequest();
3838
}
3939

4040
/**
41-
* @return Request
41+
* @return Request|null
4242
*/
4343
public function getMasterRequest()
4444
{

src/Symfony/Component/HttpKernel/RequestStack.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
/**
1717
* Request stack that controls the lifecycle of requests.
1818
*
19-
* Notifies services of changes in the stack.
20-
*
2119
* @author Benjamin Eberlei <[email protected]>
2220
*/
2321
class RequestStack
@@ -33,14 +31,18 @@ public function push(Request $request)
3331
}
3432

3533
/**
36-
* Pop the current request from the stack.
34+
* Pops the current request from the stack.
3735
*
3836
* This operation lets the current request go out of scope.
3937
*
4038
* @return Request
4139
*/
4240
public function pop()
4341
{
42+
if (!$this->requests) {
43+
throw new \LogicException('Unable to pop a Request as the stack is already empty.');
44+
}
45+
4446
return array_pop($this->requests);
4547
}
4648

@@ -65,11 +67,11 @@ public function getMasterRequest()
6567
}
6668

6769
/**
68-
* Return the parent request of the current.
70+
* Returns the parent request of the current.
6971
*
70-
* If current Request is the master request, method returns null.
72+
* If current Request is the master request, it returns null.
7173
*
72-
* @return Request
74+
* @return Request|null
7375
*/
7476
public function getParentRequest()
7577
{

0 commit comments

Comments
 (0)