RedirectDestination.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Drupal\Component\Utility\UrlHelper;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. /**
  6. * Provides helpers for redirect destinations.
  7. */
  8. class RedirectDestination implements RedirectDestinationInterface {
  9. /**
  10. * The request stack.
  11. *
  12. * @var \Symfony\Component\HttpFoundation\RequestStack
  13. */
  14. protected $requestStack;
  15. /**
  16. * The URL generator.
  17. *
  18. * @var \Drupal\Core\Routing\UrlGeneratorInterface
  19. */
  20. protected $urlGenerator;
  21. /**
  22. * The destination used by the current request.
  23. *
  24. * @var string
  25. */
  26. protected $destination;
  27. /**
  28. * Constructs a new RedirectDestination instance.
  29. *
  30. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  31. * The request stack.
  32. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
  33. * The URL generator.
  34. */
  35. public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator) {
  36. $this->requestStack = $request_stack;
  37. $this->urlGenerator = $url_generator;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getAsArray() {
  43. return ['destination' => $this->get()];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function get() {
  49. if (!isset($this->destination)) {
  50. $query = $this->requestStack->getCurrentRequest()->query;
  51. if (UrlHelper::isExternal($query->get('destination'))) {
  52. $this->destination = '/';
  53. }
  54. elseif ($query->has('destination')) {
  55. $this->destination = $query->get('destination');
  56. }
  57. else {
  58. $this->destination = $this->urlGenerator->generateFromRoute('<current>', [], ['query' => UrlHelper::filterQueryParameters($query->all())]);
  59. }
  60. }
  61. return $this->destination;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function set($new_destination) {
  67. $this->destination = $new_destination;
  68. return $this;
  69. }
  70. }