LogoutUrlExtension.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Bridge\Twig\Extension;
  11. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFunction;
  14. /**
  15. * LogoutUrlHelper provides generator functions for the logout URL to Twig.
  16. *
  17. * @author Jeremy Mikola <jmikola@gmail.com>
  18. */
  19. final class LogoutUrlExtension extends AbstractExtension
  20. {
  21. private $generator;
  22. public function __construct(LogoutUrlGenerator $generator)
  23. {
  24. $this->generator = $generator;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getFunctions(): array
  30. {
  31. return [
  32. new TwigFunction('logout_url', [$this, 'getLogoutUrl']),
  33. new TwigFunction('logout_path', [$this, 'getLogoutPath']),
  34. ];
  35. }
  36. /**
  37. * Generates the relative logout URL for the firewall.
  38. *
  39. * @param string|null $key The firewall key or null to use the current firewall key
  40. */
  41. public function getLogoutPath(string $key = null): string
  42. {
  43. return $this->generator->getLogoutPath($key);
  44. }
  45. /**
  46. * Generates the absolute logout URL for the firewall.
  47. *
  48. * @param string|null $key The firewall key or null to use the current firewall key
  49. */
  50. public function getLogoutUrl(string $key = null): string
  51. {
  52. return $this->generator->getLogoutUrl($key);
  53. }
  54. }