Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| CsrfExtension | |
100.00% |
8 / 8 |
|
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 | |||
| getCsrfToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCsrfInput | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Shared\View; |
| 6 | |
| 7 | use Twig\Extension\AbstractExtension; |
| 8 | use Twig\TwigFunction; |
| 9 | use Yiisoft\Csrf\CsrfTokenInterface; |
| 10 | |
| 11 | class CsrfExtension extends AbstractExtension |
| 12 | { |
| 13 | public function __construct( |
| 14 | private CsrfTokenInterface $csrfToken |
| 15 | ) {} |
| 16 | |
| 17 | public function getFunctions(): array |
| 18 | { |
| 19 | return [ |
| 20 | new TwigFunction('csrf_token', [$this, 'getCsrfToken']), |
| 21 | new TwigFunction('csrf_input', [$this, 'getCsrfInput'], ['is_safe' => ['html']]), |
| 22 | ]; |
| 23 | } |
| 24 | |
| 25 | public function getCsrfToken(): string |
| 26 | { |
| 27 | return $this->csrfToken->getValue(); |
| 28 | } |
| 29 | |
| 30 | public function getCsrfInput(): string |
| 31 | { |
| 32 | $token = htmlspecialchars($this->csrfToken->getValue(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
| 33 | return sprintf('<input type="hidden" name="_csrf" value="%s">', $token); |
| 34 | } |
| 35 | } |