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
FragmentUriGenerator.php
81 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\Request; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\RequestStack; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ControllerReference; |
| 16 | use Matomo\Dependencies\Symfony\Component\HttpKernel\UriSigner; |
| 17 | /** |
| 18 | * Generates a fragment URI. |
| 19 | * |
| 20 | * @author Kévin Dunglas <kevin@dunglas.fr> |
| 21 | * @author Fabien Potencier <fabien@symfony.com> |
| 22 | */ |
| 23 | final class FragmentUriGenerator implements FragmentUriGeneratorInterface |
| 24 | { |
| 25 | private $fragmentPath; |
| 26 | private $signer; |
| 27 | private $requestStack; |
| 28 | public function __construct(string $fragmentPath, ?UriSigner $signer = null, ?RequestStack $requestStack = null) |
| 29 | { |
| 30 | $this->fragmentPath = $fragmentPath; |
| 31 | $this->signer = $signer; |
| 32 | $this->requestStack = $requestStack; |
| 33 | } |
| 34 | /** |
| 35 | * {@inheritDoc} |
| 36 | */ |
| 37 | public function generate(ControllerReference $controller, ?Request $request = null, bool $absolute = \false, bool $strict = \true, bool $sign = \true) : string |
| 38 | { |
| 39 | if (null === $request && (null === $this->requestStack || null === ($request = $this->requestStack->getCurrentRequest()))) { |
| 40 | throw new \LogicException('Generating a fragment URL can only be done when handling a Request.'); |
| 41 | } |
| 42 | if ($sign && null === $this->signer) { |
| 43 | throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.'); |
| 44 | } |
| 45 | if ($strict) { |
| 46 | $this->checkNonScalar($controller->attributes); |
| 47 | } |
| 48 | // We need to forward the current _format and _locale values as we don't have |
| 49 | // a proper routing pattern to do the job for us. |
| 50 | // This makes things inconsistent if you switch from rendering a controller |
| 51 | // to rendering a route if the route pattern does not contain the special |
| 52 | // _format and _locale placeholders. |
| 53 | if (!isset($controller->attributes['_format'])) { |
| 54 | $controller->attributes['_format'] = $request->getRequestFormat(); |
| 55 | } |
| 56 | if (!isset($controller->attributes['_locale'])) { |
| 57 | $controller->attributes['_locale'] = $request->getLocale(); |
| 58 | } |
| 59 | $controller->attributes['_controller'] = $controller->controller; |
| 60 | $controller->query['_path'] = http_build_query($controller->attributes, '', '&'); |
| 61 | $path = $this->fragmentPath . '?' . http_build_query($controller->query, '', '&'); |
| 62 | // we need to sign the absolute URI, but want to return the path only. |
| 63 | $fragmentUri = $sign || $absolute ? $request->getUriForPath($path) : $request->getBaseUrl() . $path; |
| 64 | if (!$sign) { |
| 65 | return $fragmentUri; |
| 66 | } |
| 67 | $fragmentUri = $this->signer->sign($fragmentUri); |
| 68 | return $absolute ? $fragmentUri : substr($fragmentUri, \strlen($request->getSchemeAndHttpHost())); |
| 69 | } |
| 70 | private function checkNonScalar(array $values) : void |
| 71 | { |
| 72 | foreach ($values as $key => $value) { |
| 73 | if (\is_array($value)) { |
| 74 | $this->checkNonScalar($value); |
| 75 | } elseif (!\is_scalar($value) && null !== $value) { |
| 76 | throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key)); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |