annotate vendor/symfony/http-kernel/Fragment/AbstractSurrogateFragmentRenderer.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
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\Request;
Chris@0 15 use Symfony\Component\HttpFoundation\Response;
Chris@0 16 use Symfony\Component\HttpKernel\Controller\ControllerReference;
Chris@0 17 use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
Chris@0 18 use Symfony\Component\HttpKernel\UriSigner;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Implements Surrogate rendering strategy.
Chris@0 22 *
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 */
Chris@0 25 abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRenderer
Chris@0 26 {
Chris@0 27 private $surrogate;
Chris@0 28 private $inlineStrategy;
Chris@0 29 private $signer;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The "fallback" strategy when surrogate is not available should always be an
Chris@0 33 * instance of InlineFragmentRenderer.
Chris@0 34 *
Chris@0 35 * @param SurrogateInterface $surrogate An Surrogate instance
Chris@0 36 * @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported
Chris@0 37 * @param UriSigner $signer
Chris@0 38 */
Chris@0 39 public function __construct(SurrogateInterface $surrogate = null, FragmentRendererInterface $inlineStrategy, UriSigner $signer = null)
Chris@0 40 {
Chris@0 41 $this->surrogate = $surrogate;
Chris@0 42 $this->inlineStrategy = $inlineStrategy;
Chris@0 43 $this->signer = $signer;
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 *
Chris@0 49 * Note that if the current Request has no surrogate capability, this method
Chris@0 50 * falls back to use the inline rendering strategy.
Chris@0 51 *
Chris@0 52 * Additional available options:
Chris@0 53 *
Chris@0 54 * * alt: an alternative URI to render in case of an error
Chris@0 55 * * comment: a comment to add when returning the surrogate tag
Chris@0 56 *
Chris@0 57 * Note, that not all surrogate strategies support all options. For now
Chris@0 58 * 'alt' and 'comment' are only supported by ESI.
Chris@0 59 *
Chris@0 60 * @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
Chris@0 61 */
Chris@17 62 public function render($uri, Request $request, array $options = [])
Chris@0 63 {
Chris@0 64 if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
Chris@0 65 if ($uri instanceof ControllerReference && $this->containsNonScalars($uri->attributes)) {
Chris@14 66 @trigger_error('Passing non-scalar values as part of URI attributes to the ESI and SSI rendering strategies is deprecated since Symfony 3.1, and will be removed in 4.0. Use a different rendering strategy or pass scalar values.', E_USER_DEPRECATED);
Chris@0 67 }
Chris@0 68
Chris@0 69 return $this->inlineStrategy->render($uri, $request, $options);
Chris@0 70 }
Chris@0 71
Chris@0 72 if ($uri instanceof ControllerReference) {
Chris@0 73 $uri = $this->generateSignedFragmentUri($uri, $request);
Chris@0 74 }
Chris@0 75
Chris@0 76 $alt = isset($options['alt']) ? $options['alt'] : null;
Chris@0 77 if ($alt instanceof ControllerReference) {
Chris@0 78 $alt = $this->generateSignedFragmentUri($alt, $request);
Chris@0 79 }
Chris@0 80
Chris@0 81 $tag = $this->surrogate->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
Chris@0 82
Chris@0 83 return new Response($tag);
Chris@0 84 }
Chris@0 85
Chris@0 86 private function generateSignedFragmentUri($uri, Request $request)
Chris@0 87 {
Chris@0 88 if (null === $this->signer) {
Chris@0 89 throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
Chris@0 90 }
Chris@0 91
Chris@0 92 // we need to sign the absolute URI, but want to return the path only.
Chris@0 93 $fragmentUri = $this->signer->sign($this->generateFragmentUri($uri, $request, true));
Chris@0 94
Chris@17 95 return substr($fragmentUri, \strlen($request->getSchemeAndHttpHost()));
Chris@0 96 }
Chris@0 97
Chris@0 98 private function containsNonScalars(array $values)
Chris@0 99 {
Chris@0 100 foreach ($values as $value) {
Chris@17 101 if (\is_array($value)) {
Chris@14 102 return $this->containsNonScalars($value);
Chris@0 103 } elseif (!is_scalar($value) && null !== $value) {
Chris@0 104 return true;
Chris@0 105 }
Chris@0 106 }
Chris@0 107
Chris@0 108 return false;
Chris@0 109 }
Chris@0 110 }