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@17
|
14 use Symfony\Component\HttpFoundation\RequestStack;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
16 use Symfony\Component\HttpFoundation\StreamedResponse;
|
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@17
|
32 private $renderers = [];
|
Chris@0
|
33 private $requestStack;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
|
Chris@0
|
37 * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
|
Chris@0
|
38 * @param bool $debug Whether the debug mode is enabled or not
|
Chris@0
|
39 */
|
Chris@17
|
40 public function __construct(RequestStack $requestStack, array $renderers = [], $debug = false)
|
Chris@0
|
41 {
|
Chris@0
|
42 $this->requestStack = $requestStack;
|
Chris@0
|
43 foreach ($renderers as $renderer) {
|
Chris@0
|
44 $this->addRenderer($renderer);
|
Chris@0
|
45 }
|
Chris@0
|
46 $this->debug = $debug;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Adds a renderer.
|
Chris@0
|
51 */
|
Chris@0
|
52 public function addRenderer(FragmentRendererInterface $renderer)
|
Chris@0
|
53 {
|
Chris@0
|
54 $this->renderers[$renderer->getName()] = $renderer;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Renders a URI and returns the Response content.
|
Chris@0
|
59 *
|
Chris@0
|
60 * Available options:
|
Chris@0
|
61 *
|
Chris@0
|
62 * * ignore_errors: true to return an empty string in case of an error
|
Chris@0
|
63 *
|
Chris@0
|
64 * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
|
Chris@0
|
65 * @param string $renderer The renderer name
|
Chris@0
|
66 * @param array $options An array of options
|
Chris@0
|
67 *
|
Chris@0
|
68 * @return string|null The Response content or null when the Response is streamed
|
Chris@0
|
69 *
|
Chris@0
|
70 * @throws \InvalidArgumentException when the renderer does not exist
|
Chris@0
|
71 * @throws \LogicException when no master request is being handled
|
Chris@0
|
72 */
|
Chris@17
|
73 public function render($uri, $renderer = 'inline', array $options = [])
|
Chris@0
|
74 {
|
Chris@0
|
75 if (!isset($options['ignore_errors'])) {
|
Chris@0
|
76 $options['ignore_errors'] = !$this->debug;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 if (!isset($this->renderers[$renderer])) {
|
Chris@0
|
80 throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 if (!$request = $this->requestStack->getCurrentRequest()) {
|
Chris@0
|
84 throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Delivers the Response as a string.
|
Chris@0
|
92 *
|
Chris@0
|
93 * When the Response is a StreamedResponse, the content is streamed immediately
|
Chris@0
|
94 * instead of being returned.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return string|null The Response content or null when the Response is streamed
|
Chris@0
|
97 *
|
Chris@0
|
98 * @throws \RuntimeException when the Response is not successful
|
Chris@0
|
99 */
|
Chris@0
|
100 protected function deliver(Response $response)
|
Chris@0
|
101 {
|
Chris@0
|
102 if (!$response->isSuccessful()) {
|
Chris@0
|
103 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 if (!$response instanceof StreamedResponse) {
|
Chris@0
|
107 return $response->getContent();
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 $response->sendContent();
|
Chris@0
|
111 }
|
Chris@0
|
112 }
|