8000 feature #15196 [HttpKernel] make RequestStack parameter required (Tob… · symfony/symfony@ffddafc · GitHub
[go: up one dir, main page]

Skip to content

Commit ffddafc

Browse files
committed
feature #15196 [HttpKernel] make RequestStack parameter required (Tobion)
This PR was merged into the 3.0-dev branch. Discussion ---------- [HttpKernel] make RequestStack parameter required | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Continuation of #14634, #8904 Commits ------- a2e154d [HttpKernel] make RequestStack parameter required for classes that need it
2 parents aff5af6 + a2e154d commit ffddafc

File tree

5 files changed

+15
-133
lines changed

5 files changed

+15
-133
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,14 @@ class LazyLoadingFragmentHandler extends FragmentHandler
2828
/**
2929
* Constructor.
3030
*
31-
* RequestStack will become required in 3.0.
32-
*
3331
* @param ContainerInterface $container A container
3432
* @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
3533
* @param bool $debug Whether the debug mode is enabled or not
3634
*/
37-
public function __construct(ContainerInterface $container, $requestStack = null, $debug = false)
35+
public function __construct(ContainerInterface $container, RequestStack $requestStack, $debug = false)
3836
{
3937
$this->container = $container;
4038

41-
if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $debug instanceof RequestStack) {
42-
$tmp = $debug;
43-
$debug = $requestStack;
44-
$requestStack = func_num_args() < 3 ? null : $tmp;
45-
46-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as second argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
47-
} elseif (!$requestStack instanceof RequestStack) {
48-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
49-
}
50-
5139
parent::__construct($requestStack, array(), $debug);
5240
}
5341

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

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
/**
2323
* Initializes the locale based on the current request.
2424
*
25-
* This listener works in 2 modes:
26-
*
27-
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
28-
* * 2.4+ mode where you must pass a RequestStack instance in the constructor.
29-
*
3025
* @author Fabien Potencier <fabien@symfony.com>
3126
*/
3227
class LocaleListener implements EventSubscriberInterface
@@ -38,34 +33,12 @@ class LocaleListener implements EventSubscriberInterface
3833
/**
3934
* Constructor.
4035
*
41-
* RequestStack will become required in 3.0.
42-
*
4336
* @param RequestStack $requestStack A RequestStack instance
4437
* @param string $defaultLocale The default locale
4538
* @param RequestContextAwareInterface|null $router The router
46-
*
47-
* @throws \InvalidArgumentException
4839
*/
49-
public function __construct($requestStack = null, $defaultLocale = 'en', $router = null)
40+
public function __construct(RequestStack $requestStack, $defaultLocale = 'en', RequestContextAwareInterface $router = null)
5041
{
51-
if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $defaultLocale instanceof RequestContextAwareInterface || $router instanceof RequestStack) {
52-
$tmp = $router;
53-
$router = func_num_args() < 2 ? null : $defaultLocale;
54-
$defaultLocale = $requestStack;
55-
$requestStack = func_num_args() < 3 ? null : $tmp;
56-
57-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as first argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
58-
} elseif (!$requestStack instanceof RequestStack) {
59-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
60-
}
61-
62-
if (null !== $requestStack && !$requestStack instanceof RequestStack) {
63-
throw new \InvalidArgumentException('RequestStack instance expected.');
64-
}
65-
if (null !== $router && !$router instanceof RequestContextAwareInterface) {
66-
throw new \InvalidArgumentException('Router must implement RequestContextAwareInterface.');
67-
}
68-
6942
$this->defaultLocale = $defaultLocale;
7043
$this->requestStack = $requestStack;
7144
$this->router = $router;
@@ -82,10 +55,6 @@ public function onKernelRequest(GetResponseEvent $event)
8255

8356
public function onKernelFinishRequest(FinishRequestEvent $event)
8457
{
85-
if (null === $this->requestStack) {
86-
return; // removed when requestStack is required
87-
}
88-
8958
if (null !== $parentRequest = $this->requestStack->getParentRequest()) {
9059
$this->setRouterContext($parentRequest);
9160
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class ProfilerListener implements EventSubscriberInterface
3232
protected $onlyException;
3333
protected $onlyMasterRequests;
3434
protected $exception;
35-
protected $requests = array();
3635
protected $profiles;
3736
protected $requestStack;
3837
protected $parents;
@@ -101,14 +100,7 @@ public function onKernelResponse(FilterResponseEvent $event)
101100

102101
$this->profiles[$request] = $profile;
103102

104-
if (null !== $this->requestStack) {
105-
$this->parents[$request] = $this->requestStack->getParentRequest();
106-
} elseif (!$master) {
107-
// to be removed when requestStack is required
108-
array_pop($this->requests);
109-
110-
$this->parents[$request] = end($this->requests);
111-
}
103+
$this->parents[$request] = $this->requestStack->getParentRequest();
112104
}
113105

114106
public function onKernelTerminate(PostResponseEvent $event)
@@ -130,7 +122,6 @@ public function onKernelTerminate(PostResponseEvent $event)
130122

131123
$this->profiles = new \SplObjectStorage();
132124
$this->parents = new \SplObjectStorage();
133-
$this->requests = array();
134125
}
135126

136127
public static function getSubscribedEvents()

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

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,27 @@
3030
/**
3131
* Initializes the context from the request and sets request attributes based on a matching route.
3232
*
33-
* This listener works in 2 modes:
34-
*
35-
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
36-
* * 2.4+ mode where you must pass a RequestStack instance in the constructor.
37-
*
3833
* @author Fabien Potencier <fabien@symfony.com>
3934
*/
4035
class RouterListener implements EventSubscriberInterface
4136
{
4237
private $matcher;
4338
private $context;
4439
private $logger;
45-
private $request;
4640
private $requestStack;
4741

4842
/**
4943
* Constructor.
5044
*
51-
* RequestStack will become required in 3.0.
52-
*
5345
* @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
5446
* @param RequestStack $requestStack A RequestStack instance
5547
* @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
5648
* @param LoggerInterface|null $logger The logger
5749
*
5850
* @throws \InvalidArgumentException
5951
*/
60-
public function __construct($matcher, $requestStack = null, $context = null, $logger = null)
52+
public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null)
6153
{
62-
if ($requestStack instanceof RequestContext || $context instanceof LoggerInterface || $logger instanceof RequestStack) {
63-
$tmp = $requestStack;
64-
$requestStack = $logger;
65-
$logger = $context;
66-
$context = $tmp;
67-
68-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as second argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
69-
} elseif (!$requestStack instanceof RequestStack) {
70-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
71-
}
72-
73-
if (null !== $requestStack && !$requestStack instanceof RequestStack) {
74-
throw new \InvalidArgumentException('RequestStack instance expected.');
75-
}
76-
if (null !== $context && !$context instanceof RequestContext) {
77-
throw new \InvalidArgumentException('RequestContext instance expected.');
78-
}
79-
if (null !== $logger && !$logger instanceof LoggerInterface) {
80-
throw new \InvalidArgumentException('Logger must implement LoggerInterface.');
81-
}
82-
8354
if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
8455
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
8556
}
@@ -96,33 +67,27 @@ public function __construct($matcher, $requestStack = null, $context = null, $lo
9667

9768
private function setCurrentRequest(Request $request = null)
9869
{
99-
if (null !== $request && $this->request !== $request) {
70+
if (null !== $request) {
10071
$this->context->fromRequest($request);
10172
}
102-
103-
$this->request = $request;
10473
}
10574

75+
/**
76+
* After a sub-request is done, we need to reset the routing context to the parent request so that the URL generator
77+
* operates on the correct context again.
78+
*
79+
* @param FinishRequestEvent $event
80+
*/
10681
public function onKernelFinishRequest(FinishRequestEvent $event)
10782
{
108-
if (null === $this->requestStack) {
109-
return; // removed when requestStack is required
110-
}
111-
11283
$this->setCurrentRequest($this->requestStack->getParentRequest());
11384
}
11485

11586
public function onKernelRequest(GetResponseEvent $event)
11687
{
11788
$request = $event->getRequest();
11889

119-
// initialize the context that is also used by the generator (assuming matcher and generator share the same context instance)
120-
// we call setCurrentRequest even if most of the time, it has already been done to keep compatibility
121-
// with frameworks which do not use the Symfony service container
122-
// when we have a RequestStack, no need to do it
123-
if (null !== $this->requestStack) {
124-
$this->setCurrentRequest($request);
125-
}
90+
$this->setCurrentRequest($request);
12691

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

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

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
* This class handles the rendering of resource fragments that are included into
2424
* a main resource. The handling of the rendering is managed by specialized renderers.
2525
*
26-
* This listener works in 2 modes:
27-
*
28-
* * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
29-
* * 2.4+ mode where you must pass a RequestStack instance in the constructor.
30-
*
3126
* @author Fabien Potencier <fabien@symfony.com>
3227
*
3328
* @see FragmentRendererInterface
@@ -36,38 +31,17 @@ class FragmentHandler
3631
{
3732
private $debug;
3833
private $renderers = array();
39-
private $request;
4034
private $requestStack;
4135

4236
/**
4337
* Constructor.
4438
*
45-
* RequestStack will become required in 3.0.
46-
*
4739
* @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
4840
* @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
4941
* @param bool $debug Whether the debug mode is enabled or not
5042
*/
51-
public function __construct($requestStack = null, $renderers = array(), $debug = false)
43+
public function __construct(RequestStack $requestStack, array $renderers = array(), $debug = false)
5244
{
53-
if (is_array($requestStack)) {
54-
$tmp = $debug;
55-
$debug = func_num_args() < 2 ? false : $renderers;
56-
$renderers = $requestStack;
57-
$requestStack = func_num_args() < 3 ? null : $tmp;
58-
59-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as first argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
60-
} elseif (!$requestStack instanceof RequestStack) {
61-
@trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
62-
}
63-
64-
if (null !== $requestStack && !$requestStack instanceof RequestStack) {
65-
throw new \InvalidArgumentException('RequestStack instance expected.');
66-
}
67-
if (!is_array($renderers)) {
68-
throw new \InvalidArgumentException('Renderers must be an array.');
69-
}
70-
7145
$this->requestStack = $requestStack;
7246
foreach ($renderers as $renderer) {
7347
$this->addRenderer($renderer);
@@ -111,7 +85,7 @@ public function render($uri, $renderer = 'inline', array $options = array())
11185
throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
11286
}
11387

114-
if (!$request = $this->getRequest()) {
88+
if (!$request = $this->requestStack->getCurrentRequest()) {
11589
throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
11690
}
11791

@@ -133,7 +107,7 @@ public function render($uri, $renderer = 'inline', array $options = array())
133107
protected function deliver(Response $response)
134108
{
135109
if (!$response->isSuccessful()) {
136-
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode()));
110+
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
137111
}
138112

139113
if (!$response instanceof StreamedResponse) {
@@ -142,9 +116,4 @@ protected function deliver(Response $response)
142116

143117
$response->sendContent();
144118
}
145-
146-
private function getRequest()
147-
{
148-
return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
149-
}
150119
}

0 commit comments

Comments
 (0)
0