mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
115 lines
3.2 KiB
PHP
115 lines
3.2 KiB
PHP
<?php
|
|
/*
|
|
*/
|
|
namespace Chill\MainBundle\Templating;
|
|
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFunction;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Bridge\Twig\Extension\RoutingExtension;
|
|
|
|
/**
|
|
* Provides function to build path with returnPath.
|
|
*
|
|
* The logic of the function is based on the original routing extension.
|
|
*
|
|
*/
|
|
class ChillTwigRoutingHelper extends AbstractExtension
|
|
{
|
|
/**
|
|
*
|
|
* @var RequestStack
|
|
*/
|
|
protected $requestStack;
|
|
|
|
/**
|
|
*
|
|
* @var RoutingExtension
|
|
*/
|
|
protected $originalExtension;
|
|
|
|
public function __construct(
|
|
RequestStack $requestStack,
|
|
RoutingExtension $originalExtension
|
|
) {
|
|
$this->requestStack = $requestStack;
|
|
$this->originalExtension = $originalExtension;
|
|
}
|
|
|
|
|
|
public function getFunctions()
|
|
{
|
|
return [
|
|
new TwigFunction('chill_return_path_or', [$this, 'getReturnPathOr'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
|
new TwigFunction('chill_path_add_return_path', [$this, 'getPathAddReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
|
new TwigFunction('chill_path_forward_return_path', [$this, 'getPathForwardReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] )
|
|
];
|
|
}
|
|
|
|
public function isUrlGenerationSafe(\Twig_Node $argsNode)
|
|
{
|
|
return $this->originalExtension->isUrlGenerationSafe($argsNode);
|
|
}
|
|
|
|
/**
|
|
* Return the return path if it exists, or generate the path if not.
|
|
*
|
|
* @param string $name
|
|
* @param array $parameters
|
|
* @param bool $relative
|
|
* @return string
|
|
*/
|
|
public function getReturnPathOr($name, $parameters = [], $relative = false)
|
|
{
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
|
|
if ($request->query->has('returnPath')) {
|
|
return $request->query->get('returnPath');
|
|
}
|
|
|
|
return $this->originalExtension->getPath($name, $parameters, $relative);
|
|
}
|
|
|
|
/**
|
|
* Build an url with a returnPath parameter to current page
|
|
*
|
|
* @param string $name
|
|
* @param array $parameters
|
|
* @param bool $relative
|
|
* @return string
|
|
*/
|
|
public function getPathAddReturnPath($name, $parameters = [], $relative = false)
|
|
{
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
|
|
$parameters['returnPath'] = $request->getRequestUri();
|
|
|
|
return $this->originalExtension->getPath($name, $parameters, $relative);
|
|
}
|
|
|
|
/**
|
|
* Build an url with a returnPath parameter to current page
|
|
*
|
|
* @param string $name
|
|
* @param array $parameters
|
|
* @param bool $relative
|
|
* @return string
|
|
*/
|
|
public function getPathForwardReturnPath($name, $parameters = [], $relative = false)
|
|
{
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
|
|
if ($request->query->has('returnPath')) {
|
|
$parameters['returnPath'] = $request->query->get('returnPath');
|
|
}
|
|
|
|
return $this->originalExtension
|
|
->getPath(
|
|
$name,
|
|
$parameters,
|
|
$relative
|
|
);
|
|
}
|
|
|
|
}
|