Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Csrf | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFunctions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getCsrfField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCsrfToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Renderer\Twig\Extensions; |
| 6 | |
| 7 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 8 | use Twig\Extension\AbstractExtension as TwigExtension; |
| 9 | use Twig\TwigFunction; |
| 10 | |
| 11 | class Csrf extends TwigExtension |
| 12 | { |
| 13 | public function __construct(protected SessionInterface $session) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @return TwigFunction[] |
| 19 | */ |
| 20 | public function getFunctions(): array |
| 21 | { |
| 22 | return [ |
| 23 | new TwigFunction('csrf', [$this, 'getCsrfField'], ['is_safe' => ['html']]), |
| 24 | new TwigFunction('csrf_token', [$this, 'getCsrfToken']), |
| 25 | ]; |
| 26 | } |
| 27 | |
| 28 | public function getCsrfField(): string |
| 29 | { |
| 30 | return sprintf('<input type="hidden" name="_token" value="%s">', $this->getCsrfToken()); |
| 31 | } |
| 32 | |
| 33 | public function getCsrfToken(): string |
| 34 | { |
| 35 | return $this->session->get('_token'); |
| 36 | } |
| 37 | } |