comparison vendor/symfony/http-kernel/Fragment/FragmentHandler.php @ 0:4c8ae668cc8c

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