LogoutUrlExtension.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. 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()
  30. {
  31. return array(
  32. new TwigFunction('logout_url', array($this, 'getLogoutUrl')),
  33. new TwigFunction('logout_path', array($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. * @return string The relative logout URL
  42. */
  43. public function getLogoutPath($key = null)
  44. {
  45. return $this->generator->getLogoutPath($key);
  46. }
  47. /**
  48. * Generates the absolute logout URL for the firewall.
  49. *
  50. * @param string|null $key The firewall key or null to use the current firewall key
  51. *
  52. * @return string The absolute logout URL
  53. */
  54. public function getLogoutUrl($key = null)
  55. {
  56. return $this->generator->getLogoutUrl($key);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getName()
  62. {
  63. return 'logout_url';
  64. }
  65. }