From a2e154db30138143c24600fc6164000157d7e336 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 3 Jul 2015 18:45:43 +0200 Subject: [PATCH] [HttpKernel] make RequestStack parameter required for classes that need it --- .../LazyLoadingFragmentHandler.php | 14 +---- .../EventListener/LocaleListener.php | 33 +----------- .../EventListener/ProfilerListener.php | 11 +--- .../EventListener/RouterListener.php | 53 ++++--------------- .../HttpKernel/Fragment/FragmentHandler.php | 37 ++----------- 5 files changed, 15 insertions(+), 133 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/LazyLoadingFragmentHandler.php b/src/Symfony/Component/HttpKernel/DependencyInjection/LazyLoadingFragmentHandler.php index 50dde02d830b..789956237666 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/LazyLoadingFragmentHandler.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/LazyLoadingFragmentHandler.php @@ -28,26 +28,14 @@ class LazyLoadingFragmentHandler extends FragmentHandler /** * Constructor. * - * RequestStack will become required in 3.0. - * * @param ContainerInterface $container A container * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests * @param bool $debug Whether the debug mode is enabled or not */ - public function __construct(ContainerInterface $container, $requestStack = null, $debug = false) + public function __construct(ContainerInterface $container, RequestStack $requestStack, $debug = false) { $this->container = $container; - if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $debug instanceof RequestStack) { - $tmp = $debug; - $debug = $requestStack; - $requestStack = func_num_args() < 3 ? null : $tmp; - - @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); - } elseif (!$requestStack instanceof RequestStack) { - @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); - } - parent::__construct($requestStack, array(), $debug); } diff --git a/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php b/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php index 895d39aa3dd9..99fc78679390 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php @@ -22,11 +22,6 @@ /** * Initializes the locale based on the current request. * - * This listener works in 2 modes: - * - * * 2.3 compatibility mode where you must call setRequest whenever the Request changes. - * * 2.4+ mode where you must pass a RequestStack instance in the constructor. - * * @author Fabien Potencier */ class LocaleListener implements EventSubscriberInterface @@ -38,34 +33,12 @@ class LocaleListener implements EventSubscriberInterface /** * Constructor. * - * RequestStack will become required in 3.0. - * * @param RequestStack $requestStack A RequestStack instance * @param string $defaultLocale The default locale * @param RequestContextAwareInterface|null $router The router - * - * @throws \InvalidArgumentException */ - public function __construct($requestStack = null, $defaultLocale = 'en', $router = null) + public function __construct(RequestStack $requestStack, $defaultLocale = 'en', RequestContextAwareInterface $router = null) { - if ((null !== $requestStack && !$requestStack instanceof RequestStack) || $defaultLocale instanceof RequestContextAwareInterface || $router instanceof RequestStack) { - $tmp = $router; - $router = func_num_args() < 2 ? null : $defaultLocale; - $defaultLocale = $requestStack; - $requestStack = func_num_args() < 3 ? null : $tmp; - - @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); - } elseif (!$requestStack instanceof RequestStack) { - @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); - } - - if (null !== $requestStack && !$requestStack instanceof RequestStack) { - throw new \InvalidArgumentException('RequestStack instance expected.'); - } - if (null !== $router && !$router instanceof RequestContextAwareInterface) { - throw new \InvalidArgumentException('Router must implement RequestContextAwareInterface.'); - } - $this->defaultLocale = $defaultLocale; $this->requestStack = $requestStack; $this->router = $router; @@ -82,10 +55,6 @@ public function onKernelRequest(GetResponseEvent $event) public function onKernelFinishRequest(FinishRequestEvent $event) { - if (null === $this->requestStack) { - return; // removed when requestStack is required - } - if (null !== $parentRequest = $this->requestStack->getParentRequest()) { $this->setRouterContext($parentRequest); } diff --git a/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php b/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php index 5ce717291db3..c3772b688e54 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php @@ -32,7 +32,6 @@ class ProfilerListener implements EventSubscriberInterface protected $onlyException; protected $onlyMasterRequests; protected $exception; - protected $requests = array(); protected $profiles; protected $requestStack; protected $parents; @@ -101,14 +100,7 @@ public function onKernelResponse(FilterResponseEvent $event) $this->profiles[$request] = $profile; - if (null !== $this->requestStack) { - $this->parents[$request] = $this->requestStack->getParentRequest(); - } elseif (!$master) { - // to be removed when requestStack is required - array_pop($this->requests); - - $this->parents[$request] = end($this->requests); - } + $this->parents[$request] = $this->requestStack->getParentRequest(); } public function onKernelTerminate(PostResponseEvent $event) @@ -130,7 +122,6 @@ public function onKernelTerminate(PostResponseEvent $event) $this->profiles = new \SplObjectStorage(); $this->parents = new \SplObjectStorage(); - $this->requests = array(); } public static function getSubscribedEvents() diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php index 725a219c60f1..01a51b13a189 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php @@ -30,11 +30,6 @@ /** * Initializes the context from the request and sets request attributes based on a matching route. * - * This listener works in 2 modes: - * - * * 2.3 compatibility mode where you must call setRequest whenever the Request changes. - * * 2.4+ mode where you must pass a RequestStack instance in the constructor. - * * @author Fabien Potencier */ class RouterListener implements EventSubscriberInterface @@ -42,14 +37,11 @@ class RouterListener implements EventSubscriberInterface private $matcher; private $context; private $logger; - private $request; private $requestStack; /** * Constructor. * - * RequestStack will become required in 3.0. - * * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher * @param RequestStack $requestStack A RequestStack instance * @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface) @@ -57,29 +49,8 @@ class RouterListener implements EventSubscriberInterface * * @throws \InvalidArgumentException */ - public function __construct($matcher, $requestStack = null, $context = null, $logger = null) + public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null) { - if ($requestStack instanceof RequestContext || $context instanceof LoggerInterface || $logger instanceof RequestStack) { - $tmp = $requestStack; - $requestStack = $logger; - $logger = $context; - $context = $tmp; - - @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); - } elseif (!$requestStack instanceof RequestStack) { - @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); - } - - if (null !== $requestStack && !$requestStack instanceof RequestStack) { - throw new \InvalidArgumentException('RequestStack instance expected.'); - } - if (null !== $context && !$context instanceof RequestContext) { - throw new \InvalidArgumentException('RequestContext instance expected.'); - } - if (null !== $logger && !$logger instanceof LoggerInterface) { - throw new \InvalidArgumentException('Logger must implement LoggerInterface.'); - } - if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) { throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.'); } @@ -96,19 +67,19 @@ public function __construct($matcher, $requestStack = null, $context = null, $lo private function setCurrentRequest(Request $request = null) { - if (null !== $request && $this->request !== $request) { + if (null !== $request) { $this->context->fromRequest($request); } - - $this->request = $request; } + /** + * After a sub-request is done, we need to reset the routing context to the parent request so that the URL generator + * operates on the correct context again. + * + * @param FinishRequestEvent $event + */ public function onKernelFinishRequest(FinishRequestEvent $event) { - if (null === $this->requestStack) { - return; // removed when requestStack is required - } - $this->setCurrentRequest($this->requestStack->getParentRequest()); } @@ -116,13 +87,7 @@ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); - // initialize the context that is also used by the generator (assuming matcher and generator share the same context instance) - // we call setCurrentRequest even if most of the time, it has already been done to keep compatibility - // with frameworks which do not use the Symfony service container - // when we have a RequestStack, no need to do it - if (null !== $this->requestStack) { - $this->setCurrentRequest($request); - } + $this->setCurrentRequest($request); if ($request->attributes->has('_controller')) { // routing is already done diff --git a/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php b/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php index 66a912d8b52d..8bc74a2af5fa 100644 --- a/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php +++ b/src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php @@ -23,11 +23,6 @@ * This class handles the rendering of resource fragments that are included into * a main resource. The handling of the rendering is managed by specialized renderers. * - * This listener works in 2 modes: - * - * * 2.3 compatibility mode where you must call setRequest whenever the Request changes. - * * 2.4+ mode where you must pass a RequestStack instance in the constructor. - * * @author Fabien Potencier * * @see FragmentRendererInterface @@ -36,38 +31,17 @@ class FragmentHandler { private $debug; private $renderers = array(); - private $request; private $requestStack; /** * Constructor. * - * RequestStack will become required in 3.0. - * * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances * @param bool $debug Whether the debug mode is enabled or not */ - public function __construct($requestStack = null, $renderers = array(), $debug = false) + public function __construct(RequestStack $requestStack, array $renderers = array(), $debug = false) { - if (is_array($requestStack)) { - $tmp = $debug; - $debug = func_num_args() < 2 ? false : $renderers; - $renderers = $requestStack; - $requestStack = func_num_args() < 3 ? null : $tmp; - - @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); - } elseif (!$requestStack instanceof RequestStack) { - @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); - } - - if (null !== $requestStack && !$requestStack instanceof RequestStack) { - throw new \InvalidArgumentException('RequestStack instance expected.'); - } - if (!is_array($renderers)) { - throw new \InvalidArgumentException('Renderers must be an array.'); - } - $this->requestStack = $requestStack; foreach ($renderers as $renderer) { $this->addRenderer($renderer); @@ -111,7 +85,7 @@ public function render($uri, $renderer = 'inline', array $options = array()) throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer)); } - if (!$request = $this->getRequest()) { + if (!$request = $this->requestStack->getCurrentRequest()) { throw new \LogicException('Rendering a fragment can only be done when handling a Request.'); } @@ -133,7 +107,7 @@ public function render($uri, $renderer = 'inline', array $options = array()) protected function deliver(Response $response) { if (!$response->isSuccessful()) { - throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode())); + throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode())); } if (!$response instanceof StreamedResponse) { @@ -142,9 +116,4 @@ protected function deliver(Response $response) $response->sendContent(); } - - private function getRequest() - { - return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request; - } }