vendor/symfony/http-kernel/DataCollector/RouterDataCollector.php line 72

Open in your IDE?
  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\HttpKernel\DataCollector;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  15. /**
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class RouterDataCollector extends DataCollector
  19. {
  20. /**
  21. * @var \SplObjectStorage<Request, callable>
  22. */
  23. protected $controllers;
  24. public function __construct()
  25. {
  26. $this->reset();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. *
  31. * @final
  32. */
  33. public function collect(Request $request, Response $response, ?\Throwable $exception = null)
  34. {
  35. if ($response instanceof RedirectResponse) {
  36. $this->data['redirect'] = true;
  37. $this->data['url'] = $response->getTargetUrl();
  38. if ($this->controllers->contains($request)) {
  39. $this->data['route'] = $this->guessRoute($request, $this->controllers[$request]);
  40. }
  41. }
  42. unset($this->controllers[$request]);
  43. }
  44. public function reset()
  45. {
  46. $this->controllers = new \SplObjectStorage();
  47. $this->data = [
  48. 'redirect' => false,
  49. 'url' => null,
  50. 'route' => null,
  51. ];
  52. }
  53. protected function guessRoute(Request $request, $controller)
  54. {
  55. return 'n/a';
  56. }
  57. /**
  58. * Remembers the controller associated to each request.
  59. */
  60. public function onKernelController(ControllerEvent $event)
  61. {
  62. $this->controllers[$event->getRequest()] = $event->getController();
  63. }
  64. /**
  65. * @return bool Whether this request will result in a redirect
  66. */
  67. public function getRedirect()
  68. {
  69. return $this->data['redirect'];
  70. }
  71. /**
  72. * @return string|null
  73. */
  74. public function getTargetUrl()
  75. {
  76. return $this->data['url'];
  77. }
  78. /**
  79. * @return string|null
  80. */
  81. public function getTargetRoute()
  82. {
  83. return $this->data['route'];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getName()
  89. {
  90. return 'router';
  91. }
  92. }