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, string|object|array $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(): bool
  68. {
  69. return $this->data['redirect'];
  70. }
  71. public function getTargetUrl(): ?string
  72. {
  73. return $this->data['url'];
  74. }
  75. public function getTargetRoute(): ?string
  76. {
  77. return $this->data['route'];
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getName(): string
  83. {
  84. return 'router';
  85. }
  86. }