package and depencies
This commit is contained in:
@@ -24,9 +24,9 @@ use Symfony\Component\HttpKernel\UriSigner;
|
||||
*/
|
||||
abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRenderer
|
||||
{
|
||||
private $surrogate;
|
||||
private $inlineStrategy;
|
||||
private $signer;
|
||||
private ?SurrogateInterface $surrogate;
|
||||
private FragmentRendererInterface $inlineStrategy;
|
||||
private ?UriSigner $signer;
|
||||
|
||||
/**
|
||||
* The "fallback" strategy when surrogate is not available should always be an
|
||||
@@ -42,8 +42,6 @@ abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRendere
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Note that if the current Request has no surrogate capability, this method
|
||||
* falls back to use the inline rendering strategy.
|
||||
*
|
||||
@@ -51,13 +49,14 @@ abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRendere
|
||||
*
|
||||
* * alt: an alternative URI to render in case of an error
|
||||
* * comment: a comment to add when returning the surrogate tag
|
||||
* * absolute_uri: whether to generate an absolute URI or not. Default is false
|
||||
*
|
||||
* Note, that not all surrogate strategies support all options. For now
|
||||
* 'alt' and 'comment' are only supported by ESI.
|
||||
*
|
||||
* @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
|
||||
*/
|
||||
public function render($uri, Request $request, array $options = [])
|
||||
public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
|
||||
{
|
||||
if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
|
||||
if ($uri instanceof ControllerReference && $this->containsNonScalars($uri->attributes)) {
|
||||
@@ -67,13 +66,15 @@ abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRendere
|
||||
return $this->inlineStrategy->render($uri, $request, $options);
|
||||
}
|
||||
|
||||
$absolute = $options['absolute_uri'] ?? false;
|
||||
|
||||
if ($uri instanceof ControllerReference) {
|
||||
$uri = $this->generateSignedFragmentUri($uri, $request);
|
||||
$uri = $this->generateSignedFragmentUri($uri, $request, $absolute);
|
||||
}
|
||||
|
||||
$alt = $options['alt'] ?? null;
|
||||
if ($alt instanceof ControllerReference) {
|
||||
$alt = $this->generateSignedFragmentUri($alt, $request);
|
||||
$alt = $this->generateSignedFragmentUri($alt, $request, $absolute);
|
||||
}
|
||||
|
||||
$tag = $this->surrogate->renderIncludeTag($uri, $alt, $options['ignore_errors'] ?? false, $options['comment'] ?? '');
|
||||
@@ -81,9 +82,9 @@ abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRendere
|
||||
return new Response($tag);
|
||||
}
|
||||
|
||||
private function generateSignedFragmentUri(ControllerReference $uri, Request $request): string
|
||||
private function generateSignedFragmentUri(ControllerReference $uri, Request $request, bool $absolute): string
|
||||
{
|
||||
return (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
|
||||
return (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request, $absolute);
|
||||
}
|
||||
|
||||
private function containsNonScalars(array $values): bool
|
||||
|
@@ -18,10 +18,7 @@ namespace Symfony\Component\HttpKernel\Fragment;
|
||||
*/
|
||||
class EsiFragmentRenderer extends AbstractSurrogateFragmentRenderer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return 'esi';
|
||||
}
|
||||
|
@@ -29,9 +29,9 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
*/
|
||||
class FragmentHandler
|
||||
{
|
||||
private $debug;
|
||||
private $renderers = [];
|
||||
private $requestStack;
|
||||
private bool $debug;
|
||||
private array $renderers = [];
|
||||
private RequestStack $requestStack;
|
||||
|
||||
/**
|
||||
* @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
|
||||
@@ -61,14 +61,10 @@ class FragmentHandler
|
||||
*
|
||||
* * ignore_errors: true to return an empty string in case of an error
|
||||
*
|
||||
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
|
||||
*
|
||||
* @return string|null
|
||||
*
|
||||
* @throws \InvalidArgumentException when the renderer does not exist
|
||||
* @throws \LogicException when no main request is being handled
|
||||
*/
|
||||
public function render($uri, string $renderer = 'inline', array $options = [])
|
||||
public function render(string|ControllerReference $uri, string $renderer = 'inline', array $options = []): ?string
|
||||
{
|
||||
if (!isset($options['ignore_errors'])) {
|
||||
$options['ignore_errors'] = !$this->debug;
|
||||
@@ -95,7 +91,7 @@ class FragmentHandler
|
||||
*
|
||||
* @throws \RuntimeException when the Response is not successful
|
||||
*/
|
||||
protected function deliver(Response $response)
|
||||
protected function deliver(Response $response): ?string
|
||||
{
|
||||
if (!$response->isSuccessful()) {
|
||||
$responseStatusCode = $response->getStatusCode();
|
||||
|
@@ -24,17 +24,11 @@ interface FragmentRendererInterface
|
||||
{
|
||||
/**
|
||||
* Renders a URI and returns the Response content.
|
||||
*
|
||||
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function render($uri, Request $request, array $options = []);
|
||||
public function render(string|ControllerReference $uri, Request $request, array $options = []): Response;
|
||||
|
||||
/**
|
||||
* Gets the name of the strategy.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
public function getName(): string;
|
||||
}
|
||||
|
@@ -24,9 +24,9 @@ use Symfony\Component\HttpKernel\UriSigner;
|
||||
*/
|
||||
final class FragmentUriGenerator implements FragmentUriGeneratorInterface
|
||||
{
|
||||
private $fragmentPath;
|
||||
private $signer;
|
||||
private $requestStack;
|
||||
private string $fragmentPath;
|
||||
private ?UriSigner $signer;
|
||||
private ?RequestStack $requestStack;
|
||||
|
||||
public function __construct(string $fragmentPath, UriSigner $signer = null, RequestStack $requestStack = null)
|
||||
{
|
||||
@@ -35,9 +35,6 @@ final class FragmentUriGenerator implements FragmentUriGeneratorInterface
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function generate(ControllerReference $controller, Request $request = null, bool $absolute = false, bool $strict = true, bool $sign = true): string
|
||||
{
|
||||
if (null === $request && (null === $this->requestStack || null === $request = $this->requestStack->getCurrentRequest())) {
|
||||
|
@@ -15,7 +15,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerReference;
|
||||
|
||||
/**
|
||||
* Interface implemented by rendering strategies able to generate an URL for a fragment.
|
||||
* Interface implemented by rendering strategies able to generate a URL for a fragment.
|
||||
*
|
||||
* @author Kévin Dunglas <kevin@dunglas.fr>
|
||||
*/
|
||||
|
@@ -24,10 +24,10 @@ use Twig\Environment;
|
||||
*/
|
||||
class HIncludeFragmentRenderer extends RoutableFragmentRenderer
|
||||
{
|
||||
private $globalDefaultTemplate;
|
||||
private $signer;
|
||||
private $twig;
|
||||
private $charset;
|
||||
private ?string $globalDefaultTemplate;
|
||||
private ?UriSigner $signer;
|
||||
private ?Environment $twig;
|
||||
private string $charset;
|
||||
|
||||
/**
|
||||
* @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
|
||||
@@ -42,24 +42,20 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
|
||||
|
||||
/**
|
||||
* Checks if a templating engine has been set.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTemplating()
|
||||
public function hasTemplating(): bool
|
||||
{
|
||||
return null !== $this->twig;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Additional available options:
|
||||
*
|
||||
* * default: The default content (it can be a template name or the content)
|
||||
* * id: An optional hx:include tag id attribute
|
||||
* * attributes: An optional array of hx:include tag attributes
|
||||
*/
|
||||
public function render($uri, Request $request, array $options = [])
|
||||
public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
|
||||
{
|
||||
if ($uri instanceof ControllerReference) {
|
||||
$uri = (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request);
|
||||
@@ -94,10 +90,7 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
|
||||
return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return 'hinclude';
|
||||
}
|
||||
|
@@ -27,8 +27,8 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
*/
|
||||
class InlineFragmentRenderer extends RoutableFragmentRenderer
|
||||
{
|
||||
private $kernel;
|
||||
private $dispatcher;
|
||||
private HttpKernelInterface $kernel;
|
||||
private ?EventDispatcherInterface $dispatcher;
|
||||
|
||||
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
|
||||
{
|
||||
@@ -37,13 +37,11 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Additional available options:
|
||||
*
|
||||
* * alt: an alternative URI to render in case of an error
|
||||
*/
|
||||
public function render($uri, Request $request, array $options = [])
|
||||
public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
|
||||
{
|
||||
$reference = null;
|
||||
if ($uri instanceof ControllerReference) {
|
||||
@@ -120,9 +118,7 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
|
||||
|
||||
static $setSession;
|
||||
|
||||
if (null === $setSession) {
|
||||
$setSession = \Closure::bind(static function ($subRequest, $request) { $subRequest->session = $request->session; }, null, Request::class);
|
||||
}
|
||||
$setSession ??= \Closure::bind(static function ($subRequest, $request) { $subRequest->session = $request->session; }, null, Request::class);
|
||||
$setSession($subRequest, $request);
|
||||
|
||||
if ($request->get('_format')) {
|
||||
@@ -135,10 +131,7 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
|
||||
return $subRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return 'inline';
|
||||
}
|
||||
|
@@ -42,10 +42,8 @@ abstract class RoutableFragmentRenderer implements FragmentRendererInterface
|
||||
*
|
||||
* @param bool $absolute Whether to generate an absolute URL or not
|
||||
* @param bool $strict Whether to allow non-scalar attributes or not
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function generateFragmentUri(ControllerReference $reference, Request $request, bool $absolute = false, bool $strict = true)
|
||||
protected function generateFragmentUri(ControllerReference $reference, Request $request, bool $absolute = false, bool $strict = true): string
|
||||
{
|
||||
return (new FragmentUriGenerator($this->fragmentPath))->generate($reference, $request, $absolute, $strict, false);
|
||||
}
|
||||
|
@@ -18,10 +18,7 @@ namespace Symfony\Component\HttpKernel\Fragment;
|
||||
*/
|
||||
class SsiFragmentRenderer extends AbstractSurrogateFragmentRenderer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return 'ssi';
|
||||
}
|
||||
|
Reference in New Issue
Block a user