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