Skip to content

Commit

Permalink
[HttpKernel] Add a class to generate fragment URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Mar 24, 2021
1 parent 2dcf313 commit 0141b44
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php
Expand Up @@ -38,4 +38,6 @@ public static function controller(string $controller, array $attributes = [], ar
{
return new ControllerReference($controller, $attributes, $query);
}

public static function fragment()
}
Expand Up @@ -83,14 +83,7 @@ public function render($uri, Request $request, array $options = [])

private function generateSignedFragmentUri(ControllerReference $uri, Request $request): string
{
if (null === $this->signer) {
throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
}

// we need to sign the absolute URI, but want to return the path only.
$fragmentUri = $this->signer->sign($this->generateFragmentUri($uri, $request, true));

return substr($fragmentUri, \strlen($request->getSchemeAndHttpHost()));
return (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
}

private function containsNonScalars(array $values): bool
Expand Down
85 changes: 85 additions & 0 deletions src/Symfony/Component/HttpKernel/Fragment/FragmentUriGenerator.php
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Fragment;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\UriSigner;

/**
* Generates a fragment URI.
*
* @author Kévin Dunglas <kevin@dunglas.fr>
* @author Fabien Potencier <fabien@symfony.com>
*/
final class FragmentUriGenerator implements FragmentUriGeneratorInterface
{
private $fragmentPath;
private $signer;

public function __construct(string $fragmentPath, UriSigner $signer = null)
{
$this->fragmentPath = $fragmentPath;
$this->signer = $signer;
}

/**
* {@inheritDoc}
*/
public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string
{
if ($sign && null === $this->signer) {
throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
}

if ($strict) {
$this->checkNonScalar($controller->attributes);
}

// We need to forward the current _format and _locale values as we don't have
// a proper routing pattern to do the job for us.
// This makes things inconsistent if you switch from rendering a controller
// to rendering a route if the route pattern does not contain the special
// _format and _locale placeholders.
if (!isset($controller->attributes['_format'])) {
$controller->attributes['_format'] = $request->getRequestFormat();
}
if (!isset($controller->attributes['_locale'])) {
$controller->attributes['_locale'] = $request->getLocale();
}

$controller->attributes['_controller'] = $controller->controller;
$controller->query['_path'] = http_build_query($controller->attributes, '', '&');
$path = $this->fragmentPath.'?'.http_build_query($controller->query, '', '&');

// we need to sign the absolute URI, but want to return the path only.
$fragmentUri = $sign || $absolute ? $request->getUriForPath($path) : $request->getBaseUrl().$path;

if (!$sign) {
return $fragmentUri;
}

$fragmentUri = $this->signer->sign($fragmentUri);

return $absolute ? $fragmentUri : substr($fragmentUri, \strlen($request->getSchemeAndHttpHost()));
}

private function checkNonScalar(array $values): void
{
foreach ($values as $key => $value) {
if (\is_array($value)) {
$this->checkNonScalar($value);
} elseif (!is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key));
}
}
}
}
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Fragment;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerReference;

/**
* Interface implemented by rendering strategies able to generate an URL for a fragment.
*
* @author Kévin Dunglas <kevin@dunglas.fr>
*/
interface FragmentUriGeneratorInterface
{
/**
* Generates a fragment URI for a given controller.
*
* @param string|ControllerReference $controller The name of a controller as a string or a ControllerReference instance
* @param bool $absolute Whether to generate an absolute URL or not
* @param bool $strict Whether to allow non-scalar attributes or not
*
* @return string A fragment URI
*/
public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string;
}
Expand Up @@ -62,12 +62,7 @@ public function hasTemplating()
public function render($uri, Request $request, array $options = [])
{
if ($uri instanceof ControllerReference) {
if (null === $this->signer) {
throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
}

// we need to sign the absolute URI, but want to return the path only.
$uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), \strlen($request->getSchemeAndHttpHost()));
$uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
}

// We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
Expand Down
Expand Up @@ -22,7 +22,7 @@
*/
abstract class RoutableFragmentRenderer implements FragmentRendererInterface
{
private $fragmentPath = '/_fragment';
protected $fragmentPath = '/_fragment';

/**
* Sets the fragment path that triggers the fragment listener.
Expand All @@ -44,43 +44,6 @@ public function setFragmentPath(string $path)
*/
protected function generateFragmentUri(ControllerReference $reference, Request $request, bool $absolute = false, bool $strict = true)
{
if ($strict) {
$this->checkNonScalar($reference->attributes);
}

// We need to forward the current _format and _locale values as we don't have
// a proper routing pattern to do the job for us.
// This makes things inconsistent if you switch from rendering a controller
// to rendering a route if the route pattern does not contain the special
// _format and _locale placeholders.
if (!isset($reference->attributes['_format'])) {
$reference->attributes['_format'] = $request->getRequestFormat();
}
if (!isset($reference->attributes['_locale'])) {
$reference->attributes['_locale'] = $request->getLocale();
}

$reference->attributes['_controller'] = $reference->controller;

$reference->query['_path'] = http_build_query($reference->attributes, '', '&');

$path = $this->fragmentPath.'?'.http_build_query($reference->query, '', '&');

if ($absolute) {
return $request->getUriForPath($path);
}

return $request->getBaseUrl().$path;
}

private function checkNonScalar(array $values)
{
foreach ($values as $key => $value) {
if (\is_array($value)) {
$this->checkNonScalar($value);
} elseif (!is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key));
}
}
return (new FragmentUriGenerator($this->fragmentPath))->generate($reference, $request, $absolute, $strict, false);
}
}

0 comments on commit 0141b44

Please sign in to comment.