add twig routing extension to generate url using Node
This commit is contained in:
parent
c7c9a636ff
commit
2e7fa33a3e
97
core/Twig/Extension/RoutingExtension.php
Normal file
97
core/Twig/Extension/RoutingExtension.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\Twig\Extension;
|
||||
|
||||
use App\Core\Entity\Site\Node;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\Node\Expression\ArrayExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Node as TwigNode;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class RoutingExtension extends AbstractExtension
|
||||
{
|
||||
private UrlGeneratorInterface $generator;
|
||||
|
||||
public function __construct(UrlGeneratorInterface $generator)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('node_url', [$this, 'getNodeUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||
new TwigFunction('node_path', [$this, 'getNodePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||
new TwigFunction('safe_node_url', [$this, 'getSafeNodeUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||
new TwigFunction('safe_node_path', [$this, 'getSafeNodePath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
|
||||
];
|
||||
}
|
||||
|
||||
public function getNodePath(Node $node, array $parameters = [], bool $relative = false): ?string
|
||||
{
|
||||
if ($node->hasExternalUrl()) {
|
||||
return $node->getUrl();
|
||||
}
|
||||
|
||||
return $this->generator->generate(
|
||||
$node->getRouteName(),
|
||||
$parameters,
|
||||
$relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH
|
||||
);
|
||||
}
|
||||
|
||||
public function getNodeUrl(Node $node, array $parameters = [], bool $schemeRelative = false): ?string
|
||||
{
|
||||
if ($node->hasExternalUrl()) {
|
||||
return $node->getUrl();
|
||||
}
|
||||
|
||||
return $this->generator->generate(
|
||||
$node->getRouteName(),
|
||||
$parameters,
|
||||
$schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL
|
||||
);
|
||||
}
|
||||
|
||||
public function getSafeNodePath(Node $node, array $parameters = [], bool $relative = false): ?string
|
||||
{
|
||||
try {
|
||||
return $this->getNodePath($node, $parameters, $relative);
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function getSafeNodeUrl(Node $node, array $parameters = [], bool $schemeRelative = false): ?string
|
||||
{
|
||||
try {
|
||||
return $this->getNodeUrl($node, $parameters, $schemeRelative);
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Symfony\Bridge\Twig\Extension\RoutingExtension::isUrlGenerationSafe
|
||||
*/
|
||||
public function isUrlGenerationSafe(TwigNode $argsNode): array
|
||||
{
|
||||
// support named arguments
|
||||
$paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (
|
||||
$argsNode->hasNode(1) ? $argsNode->getNode(1) : null
|
||||
);
|
||||
|
||||
if (null === $paramsNode || $paramsNode instanceof ArrayExpression && \count($paramsNode) <= 2 &&
|
||||
(!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof ConstantExpression)
|
||||
) {
|
||||
return ['html'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue