Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Url | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFunctions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Renderer\Twig\Extensions; |
| 6 | |
| 7 | use Engelsystem\Http\UrlGeneratorInterface; |
| 8 | use Twig\Extension\AbstractExtension as TwigExtension; |
| 9 | use Twig\TwigFunction; |
| 10 | |
| 11 | class Url extends TwigExtension |
| 12 | { |
| 13 | public function __construct(protected UrlGeneratorInterface $urlGenerator) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @return TwigFunction[] |
| 19 | */ |
| 20 | public function getFunctions(): array |
| 21 | { |
| 22 | return [ |
| 23 | new TwigFunction('url', [$this, 'getUrl']), |
| 24 | ]; |
| 25 | } |
| 26 | |
| 27 | public function getUrl(string $path, array $parameters = []): string |
| 28 | { |
| 29 | // Fix legacy URLs |
| 30 | $path = str_replace('_', '-', $path); |
| 31 | |
| 32 | return $this->urlGenerator->to($path, $parameters); |
| 33 | } |
| 34 | } |