4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Fragment;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpFoundation\StreamedResponse;
17 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\HttpKernel\Controller\ControllerReference;
21 * Renders a URI that represents a resource fragment.
23 * This class handles the rendering of resource fragments that are included into
24 * a main resource. The handling of the rendering is managed by specialized renderers.
26 * This listener works in 2 modes:
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.
31 * @author Fabien Potencier <fabien@symfony.com>
33 * @see FragmentRendererInterface
38 private $renderers = array();
40 private $requestStack;
45 * RequestStack will become required in 3.0.
47 * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
48 * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
49 * @param bool $debug Whether the debug mode is enabled or not
51 public function __construct($requestStack = null, $renderers = array(), $debug = false)
53 if (is_array($requestStack)) {
55 $debug = func_num_args() < 2 ? false : $renderers;
56 $renderers = $requestStack;
57 $requestStack = func_num_args() < 3 ? null : $tmp;
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);
64 if (null !== $requestStack && !$requestStack instanceof RequestStack) {
65 throw new \InvalidArgumentException('RequestStack instance expected.');
67 if (!is_array($renderers)) {
68 throw new \InvalidArgumentException('Renderers must be an array.');
71 $this->requestStack = $requestStack;
72 foreach ($renderers as $renderer) {
73 $this->addRenderer($renderer);
75 $this->debug = $debug;
81 * @param FragmentRendererInterface $renderer A FragmentRendererInterface instance
83 public function addRenderer(FragmentRendererInterface $renderer)
85 $this->renderers[$renderer->getName()] = $renderer;
89 * Sets the current Request.
91 * This method was used to synchronize the Request, but as the HttpKernel
92 * is doing that automatically now, you should never call it directly.
93 * It is kept public for BC with the 2.3 version.
95 * @param Request|null $request A Request instance
97 * @deprecated since version 2.4, to be removed in 3.0.
99 public function setRequest(Request $request = null)
101 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
103 $this->request = $request;
107 * Renders a URI and returns the Response content.
111 * * ignore_errors: true to return an empty string in case of an error
113 * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
114 * @param string $renderer The renderer name
115 * @param array $options An array of options
117 * @return string|null The Response content or null when the Response is streamed
119 * @throws \InvalidArgumentException when the renderer does not exist
120 * @throws \LogicException when no master request is being handled
122 public function render($uri, $renderer = 'inline', array $options = array())
124 if (!isset($options['ignore_errors'])) {
125 $options['ignore_errors'] = !$this->debug;
128 if (!isset($this->renderers[$renderer])) {
129 throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
132 if (!$request = $this->getRequest()) {
133 throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
136 return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
140 * Delivers the Response as a string.
142 * When the Response is a StreamedResponse, the content is streamed immediately
143 * instead of being returned.
145 * @param Response $response A Response instance
147 * @return string|null The Response content or null when the Response is streamed
149 * @throws \RuntimeException when the Response is not successful
151 protected function deliver(Response $response)
153 if (!$response->isSuccessful()) {
154 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode()));
157 if (!$response instanceof StreamedResponse) {
158 return $response->getContent();
161 $response->sendContent();
164 private function getRequest()
166 return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;