CsrfExtension.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Extension\Csrf;
  11. use Symfony\Component\Form\AbstractExtension;
  12. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. /**
  15. * This extension protects forms by using a CSRF token.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. class CsrfExtension extends AbstractExtension
  20. {
  21. private $tokenManager;
  22. private $translator;
  23. private $translationDomain;
  24. public function __construct(CsrfTokenManagerInterface $tokenManager, TranslatorInterface $translator = null, string $translationDomain = null)
  25. {
  26. $this->tokenManager = $tokenManager;
  27. $this->translator = $translator;
  28. $this->translationDomain = $translationDomain;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function loadTypeExtensions()
  34. {
  35. return [
  36. new Type\FormTypeCsrfExtension($this->tokenManager, true, '_token', $this->translator, $this->translationDomain),
  37. ];
  38. }
  39. }