namespace Symfony\Component\HttpKernel\Fragment;
-use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\RequestStack;
* 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 <fabien@symfony.com>
*
* @see FragmentRendererInterface
{
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);
$this->renderers[$renderer->getName()] = $renderer;
}
- /**
- * Sets the current Request.
- *
- * This method was used to synchronize the Request, but as the HttpKernel
- * is doing that automatically now, you should never call it directly.
- * It is kept public for BC with the 2.3 version.
- *
- * @param Request|null $request A Request instance
- *
- * @deprecated since version 2.4, to be removed in 3.0.
- */
- public function setRequest(Request $request = null)
- {
- @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
-
- $this->request = $request;
- }
-
/**
* Renders a URI and returns the Response content.
*
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.');
}
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) {
$response->sendContent();
}
-
- private function getRequest()
- {
- return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
- }
}