Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\Fragment;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\StreamedResponse;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\RequestStack;
|
Chris@0
|
17 use Symfony\Component\HttpKernel\Controller\ControllerReference;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Renders a URI that represents a resource fragment.
|
Chris@0
|
21 *
|
Chris@0
|
22 * This class handles the rendering of resource fragments that are included into
|
Chris@0
|
23 * a main resource. The handling of the rendering is managed by specialized renderers.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
26 *
|
Chris@0
|
27 * @see FragmentRendererInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 class FragmentHandler
|
Chris@0
|
30 {
|
Chris@0
|
31 private $debug;
|
Chris@0
|
32 private $renderers = array();
|
Chris@0
|
33 private $requestStack;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Constructor.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
|
Chris@0
|
39 * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
|
Chris@0
|
40 * @param bool $debug Whether the debug mode is enabled or not
|
Chris@0
|
41 */
|
Chris@0
|
42 public function __construct(RequestStack $requestStack, array $renderers = array(), $debug = false)
|
Chris@0
|
43 {
|
Chris@0
|
44 $this->requestStack = $requestStack;
|
Chris@0
|
45 foreach ($renderers as $renderer) {
|
Chris@0
|
46 $this->addRenderer($renderer);
|
Chris@0
|
47 }
|
Chris@0
|
48 $this->debug = $debug;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Adds a renderer.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @param FragmentRendererInterface $renderer A FragmentRendererInterface instance
|
Chris@0
|
55 */
|
Chris@0
|
56 public function addRenderer(FragmentRendererInterface $renderer)
|
Chris@0
|
57 {
|
Chris@0
|
58 $this->renderers[$renderer->getName()] = $renderer;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Renders a URI and returns the Response content.
|
Chris@0
|
63 *
|
Chris@0
|
64 * Available options:
|
Chris@0
|
65 *
|
Chris@0
|
66 * * ignore_errors: true to return an empty string in case of an error
|
Chris@0
|
67 *
|
Chris@0
|
68 * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
|
Chris@0
|
69 * @param string $renderer The renderer name
|
Chris@0
|
70 * @param array $options An array of options
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return string|null The Response content or null when the Response is streamed
|
Chris@0
|
73 *
|
Chris@0
|
74 * @throws \InvalidArgumentException when the renderer does not exist
|
Chris@0
|
75 * @throws \LogicException when no master request is being handled
|
Chris@0
|
76 */
|
Chris@0
|
77 public function render($uri, $renderer = 'inline', array $options = array())
|
Chris@0
|
78 {
|
Chris@0
|
79 if (!isset($options['ignore_errors'])) {
|
Chris@0
|
80 $options['ignore_errors'] = !$this->debug;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 if (!isset($this->renderers[$renderer])) {
|
Chris@0
|
84 throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 if (!$request = $this->requestStack->getCurrentRequest()) {
|
Chris@0
|
88 throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Delivers the Response as a string.
|
Chris@0
|
96 *
|
Chris@0
|
97 * When the Response is a StreamedResponse, the content is streamed immediately
|
Chris@0
|
98 * instead of being returned.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @param Response $response A Response instance
|
Chris@0
|
101 *
|
Chris@0
|
102 * @return string|null The Response content or null when the Response is streamed
|
Chris@0
|
103 *
|
Chris@0
|
104 * @throws \RuntimeException when the Response is not successful
|
Chris@0
|
105 */
|
Chris@0
|
106 protected function deliver(Response $response)
|
Chris@0
|
107 {
|
Chris@0
|
108 if (!$response->isSuccessful()) {
|
Chris@0
|
109 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 if (!$response instanceof StreamedResponse) {
|
Chris@0
|
113 return $response->getContent();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 $response->sendContent();
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|