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

Skip to content

[HttpKernel] make RequestStack parameter required #15196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
class LocaleListener implements EventSubscriberInterface
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ProfilerListener implements EventSubscriberInterface
protected $onlyException;
protected $onlyMasterRequests;
protected $exception;
protected $requests = array();
protected $profiles;
protected $requestStack;
protected $parents;
Expand Down Expand Up @@ -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)
Expand All @@ -130,7 +122,6 @@ public function onKernelTerminate(PostResponseEvent $event)

$this->profiles = new \SplObjectStorage();
$this->parents = new \SplObjectStorage();
$this->requests = array();
}

public static function getSubscribedEvents()
Expand Down
53 changes: 9 additions & 44 deletions src/Symfony/Component/HttpKernel/EventListener/RouterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,27 @@
/**
* 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 <[email protected]>
*/
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)
* @param LoggerInterface|null $logger The logger
*
* @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.');
}
Expand All @@ -96,33 +67,27 @@ 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());
}

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
Expand Down
37 changes: 3 additions & 34 deletions src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
* @see FragmentRendererInterface
Expand All @@ -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);
Expand Down Expand Up @@ -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.');
}

Expand All @@ -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) {
Expand All @@ -142,9 +116,4 @@ protected function deliver(Response $response)

$response->sendContent();
}

private function getRequest()
{
return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
}
}