Add ChillUrlGenerator and ChillUrlGeneratorInterface

Introduced a new interface ChillUrlGeneratorInterface for URL generation with return path handling. Implemented this interface in the ChillUrlGenerator class, which uses Symfony components to manage URL generation and request information.
This commit is contained in:
Julien Fastré 2024-09-25 10:57:52 +02:00
parent 75005b4ed6
commit 5a467ae38d
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Routing;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final readonly class ChillUrlGenerator implements ChillUrlGeneratorInterface
{
public function __construct(private UrlGeneratorInterface $urlGenerator, private RequestStack $requestStack) {}
public function generate(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
return $this->urlGenerator->generate($name, $parameters, $referenceType);
}
public function generateWithReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
$uri = $this->requestStack->getCurrentRequest()->getRequestUri();
return $this->urlGenerator->generate($name, [$parameters, 'returnPath' => $uri], $referenceType);
}
public function returnPathOr(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
$request = $this->requestStack->getCurrentRequest();
if ($request->query->has('returnPath')) {
return $request->query->get('returnPath');
}
return $this->urlGenerator->generate($name, $parameters, $referenceType);
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Routing;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Interface for generating URLs with returnPath information.
*/
interface ChillUrlGeneratorInterface
{
/**
* Generate an URL without any return path. This is the same as using @see{UrlGeneratorInterface::generate}.
*/
public function generate(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string;
/**
* Generate an url, append a return path in it.
*/
public function generateWithReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string;
/**
* Get the return path or, if any, generate an url.
*/
public function returnPathOr(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string;
}

View File

@ -22,3 +22,8 @@ services:
class: Chill\MainBundle\Routing\MenuTwig
tags:
- { name: twig.extension }
Chill\MainBundle\Routing\ChillUrlGenerator: ~
Chill\MainBundle\Routing\ChillUrlGeneratorInterface:
alias: 'Chill\MainBundle\Routing\ChillUrlGenerator'