LogoutUrlHelper.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Bundle\SecurityBundle\Templating\Helper;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
  15. use Symfony\Component\Templating\Helper\Helper;
  16. /**
  17. * LogoutUrlHelper provides generator functions for the logout URL.
  18. *
  19. * @author Jeremy Mikola <jmikola@gmail.com>
  20. */
  21. class LogoutUrlHelper extends Helper
  22. {
  23. private $generator;
  24. /**
  25. * @param ContainerInterface|LogoutUrlGenerator $generator A ContainerInterface or LogoutUrlGenerator instance
  26. * @param UrlGeneratorInterface|null $router The router service
  27. * @param TokenStorageInterface|null $tokenStorage The token storage service
  28. *
  29. * @deprecated Passing a ContainerInterface as a first argument is deprecated since 2.7 and will be removed in 3.0.
  30. * @deprecated Passing a second and third argument is deprecated since 2.7 and will be removed in 3.0.
  31. */
  32. public function __construct($generator, UrlGeneratorInterface $router = null, TokenStorageInterface $tokenStorage = null)
  33. {
  34. if ($generator instanceof ContainerInterface) {
  35. @trigger_error('The '.__CLASS__.' constructor will require a LogoutUrlGenerator instead of a ContainerInterface instance in 3.0.', E_USER_DEPRECATED);
  36. if ($generator->has('security.logout_url_generator')) {
  37. $this->generator = $generator->get('security.logout_url_generator');
  38. } else {
  39. $this->generator = new LogoutUrlGenerator($generator->get('request_stack'), $router, $tokenStorage);
  40. }
  41. } else {
  42. $this->generator = $generator;
  43. }
  44. }
  45. /**
  46. * Generates the absolute logout path for the firewall.
  47. *
  48. * @param string|null $key The firewall key or null to use the current firewall key
  49. *
  50. * @return string The logout path
  51. */
  52. public function getLogoutPath($key)
  53. {
  54. return $this->generator->getLogoutPath($key, UrlGeneratorInterface::ABSOLUTE_PATH);
  55. }
  56. /**
  57. * Generates the absolute logout URL for the firewall.
  58. *
  59. * @param string|null $key The firewall key or null to use the current firewall key
  60. *
  61. * @return string The logout URL
  62. */
  63. public function getLogoutUrl($key)
  64. {
  65. return $this->generator->getLogoutUrl($key, UrlGeneratorInterface::ABSOLUTE_URL);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getName()
  71. {
  72. return 'logout_url';
  73. }
  74. }