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 * Constructor.
|
Chris@0
|
33 *
|
Chris@0
|
34 * The "fallback" strategy when surrogate is not available should always be an
|
Chris@0
|
35 * instance of InlineFragmentRenderer.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param SurrogateInterface $surrogate An Surrogate instance
|
Chris@0
|
38 * @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported
|
Chris@0
|
39 * @param UriSigner $signer
|
Chris@0
|
40 */
|
Chris@0
|
41 public function __construct(SurrogateInterface $surrogate = null, FragmentRendererInterface $inlineStrategy, UriSigner $signer = null)
|
Chris@0
|
42 {
|
Chris@0
|
43 $this->surrogate = $surrogate;
|
Chris@0
|
44 $this->inlineStrategy = $inlineStrategy;
|
Chris@0
|
45 $this->signer = $signer;
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * {@inheritdoc}
|
Chris@0
|
50 *
|
Chris@0
|
51 * Note that if the current Request has no surrogate capability, this method
|
Chris@0
|
52 * falls back to use the inline rendering strategy.
|
Chris@0
|
53 *
|
Chris@0
|
54 * Additional available options:
|
Chris@0
|
55 *
|
Chris@0
|
56 * * alt: an alternative URI to render in case of an error
|
Chris@0
|
57 * * comment: a comment to add when returning the surrogate tag
|
Chris@0
|
58 *
|
Chris@0
|
59 * Note, that not all surrogate strategies support all options. For now
|
Chris@0
|
60 * 'alt' and 'comment' are only supported by ESI.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
|
Chris@0
|
63 */
|
Chris@0
|
64 public function render($uri, Request $request, array $options = array())
|
Chris@0
|
65 {
|
Chris@0
|
66 if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
|
Chris@0
|
67 if ($uri instanceof ControllerReference && $this->containsNonScalars($uri->attributes)) {
|
Chris@0
|
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);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 return $this->inlineStrategy->render($uri, $request, $options);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 if ($uri instanceof ControllerReference) {
|
Chris@0
|
75 $uri = $this->generateSignedFragmentUri($uri, $request);
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 $alt = isset($options['alt']) ? $options['alt'] : null;
|
Chris@0
|
79 if ($alt instanceof ControllerReference) {
|
Chris@0
|
80 $alt = $this->generateSignedFragmentUri($alt, $request);
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 $tag = $this->surrogate->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
|
Chris@0
|
84
|
Chris@0
|
85 return new Response($tag);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 private function generateSignedFragmentUri($uri, Request $request)
|
Chris@0
|
89 {
|
Chris@0
|
90 if (null === $this->signer) {
|
Chris@0
|
91 throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 // we need to sign the absolute URI, but want to return the path only.
|
Chris@0
|
95 $fragmentUri = $this->signer->sign($this->generateFragmentUri($uri, $request, true));
|
Chris@0
|
96
|
Chris@0
|
97 return substr($fragmentUri, strlen($request->getSchemeAndHttpHost()));
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 private function containsNonScalars(array $values)
|
Chris@0
|
101 {
|
Chris@0
|
102 foreach ($values as $value) {
|
Chris@0
|
103 if (is_array($value) && $this->containsNonScalars($value)) {
|
Chris@0
|
104 return true;
|
Chris@0
|
105 } elseif (!is_scalar($value) && null !== $value) {
|
Chris@0
|
106 return true;
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 return false;
|
Chris@0
|
111 }
|
Chris@0
|
112 }
|