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\RequestStack;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpFoundation\StreamedResponse;
17 use Symfony\Component\HttpKernel\Controller\ControllerReference;
20 * Renders a URI that represents a resource fragment.
22 * This class handles the rendering of resource fragments that are included into
23 * a main resource. The handling of the rendering is managed by specialized renderers.
25 * @author Fabien Potencier <fabien@symfony.com>
27 * @see FragmentRendererInterface
32 private $renderers = array();
33 private $requestStack;
36 * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
37 * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
38 * @param bool $debug Whether the debug mode is enabled or not
40 public function __construct(RequestStack $requestStack, array $renderers = array(), $debug = false)
42 $this->requestStack = $requestStack;
43 foreach ($renderers as $renderer) {
44 $this->addRenderer($renderer);
46 $this->debug = $debug;
52 public function addRenderer(FragmentRendererInterface $renderer)
54 $this->renderers[$renderer->getName()] = $renderer;
58 * Renders a URI and returns the Response content.
62 * * ignore_errors: true to return an empty string in case of an error
64 * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
65 * @param string $renderer The renderer name
66 * @param array $options An array of options
68 * @return string|null The Response content or null when the Response is streamed
70 * @throws \InvalidArgumentException when the renderer does not exist
71 * @throws \LogicException when no master request is being handled
73 public function render($uri, $renderer = 'inline', array $options = array())
75 if (!isset($options['ignore_errors'])) {
76 $options['ignore_errors'] = !$this->debug;
79 if (!isset($this->renderers[$renderer])) {
80 throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
83 if (!$request = $this->requestStack->getCurrentRequest()) {
84 throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
87 return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
91 * Delivers the Response as a string.
93 * When the Response is a StreamedResponse, the content is streamed immediately
94 * instead of being returned.
96 * @return string|null The Response content or null when the Response is streamed
98 * @throws \RuntimeException when the Response is not successful
100 protected function deliver(Response $response)
102 if (!$response->isSuccessful()) {
103 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
106 if (!$response instanceof StreamedResponse) {
107 return $response->getContent();
110 $response->sendContent();