AbstractSurrogateFragmentRenderer.php
1 year ago
EsiFragmentRenderer.php
2 years ago
FragmentHandler.php
1 year ago
FragmentRendererInterface.php
2 years ago
FragmentUriGenerator.php
1 year ago
FragmentUriGeneratorInterface.php
1 year ago
HIncludeFragmentRenderer.php
1 year ago
InlineFragmentRenderer.php
1 year ago
RoutableFragmentRenderer.php
1 year ago
SsiFragmentRenderer.php
2 years ago
FragmentHandler.php
102 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel\Fragment; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\RequestStack; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Response; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\StreamedResponse; |
| 16 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ControllerReference; |
| 17 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Exception\HttpException; |
| 18 | /** |
| 19 | * Renders a URI that represents a resource fragment. |
| 20 | * |
| 21 | * This class handles the rendering of resource fragments that are included into |
| 22 | * a main resource. The handling of the rendering is managed by specialized renderers. |
| 23 | * |
| 24 | * @author Fabien Potencier <fabien@symfony.com> |
| 25 | * |
| 26 | * @see FragmentRendererInterface |
| 27 | */ |
| 28 | class FragmentHandler |
| 29 | { |
| 30 | private $debug; |
| 31 | private $renderers = []; |
| 32 | private $requestStack; |
| 33 | /** |
| 34 | * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances |
| 35 | * @param bool $debug Whether the debug mode is enabled or not |
| 36 | */ |
| 37 | public function __construct(RequestStack $requestStack, array $renderers = [], bool $debug = \false) |
| 38 | { |
| 39 | $this->requestStack = $requestStack; |
| 40 | foreach ($renderers as $renderer) { |
| 41 | $this->addRenderer($renderer); |
| 42 | } |
| 43 | $this->debug = $debug; |
| 44 | } |
| 45 | /** |
| 46 | * Adds a renderer. |
| 47 | */ |
| 48 | public function addRenderer(FragmentRendererInterface $renderer) |
| 49 | { |
| 50 | $this->renderers[$renderer->getName()] = $renderer; |
| 51 | } |
| 52 | /** |
| 53 | * Renders a URI and returns the Response content. |
| 54 | * |
| 55 | * Available options: |
| 56 | * |
| 57 | * * ignore_errors: true to return an empty string in case of an error |
| 58 | * |
| 59 | * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance |
| 60 | * |
| 61 | * @return string|null |
| 62 | * |
| 63 | * @throws \InvalidArgumentException when the renderer does not exist |
| 64 | * @throws \LogicException when no main request is being handled |
| 65 | */ |
| 66 | public function render($uri, string $renderer = 'inline', array $options = []) |
| 67 | { |
| 68 | if (!isset($options['ignore_errors'])) { |
| 69 | $options['ignore_errors'] = !$this->debug; |
| 70 | } |
| 71 | if (!isset($this->renderers[$renderer])) { |
| 72 | throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer)); |
| 73 | } |
| 74 | if (!($request = $this->requestStack->getCurrentRequest())) { |
| 75 | throw new \LogicException('Rendering a fragment can only be done when handling a Request.'); |
| 76 | } |
| 77 | return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options)); |
| 78 | } |
| 79 | /** |
| 80 | * Delivers the Response as a string. |
| 81 | * |
| 82 | * When the Response is a StreamedResponse, the content is streamed immediately |
| 83 | * instead of being returned. |
| 84 | * |
| 85 | * @return string|null The Response content or null when the Response is streamed |
| 86 | * |
| 87 | * @throws \RuntimeException when the Response is not successful |
| 88 | */ |
| 89 | protected function deliver(Response $response) |
| 90 | { |
| 91 | if (!$response->isSuccessful()) { |
| 92 | $responseStatusCode = $response->getStatusCode(); |
| 93 | throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $this->requestStack->getCurrentRequest()->getUri(), $responseStatusCode), 0, new HttpException($responseStatusCode)); |
| 94 | } |
| 95 | if (!$response instanceof StreamedResponse) { |
| 96 | return $response->getContent(); |
| 97 | } |
| 98 | $response->sendContent(); |
| 99 | return null; |
| 100 | } |
| 101 | } |
| 102 |