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

  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 \SplObjectStorage $controllers;
  24. public function __construct()
  25. {
  26. $this->reset();
  27. }
  28. /**
  29. * @final
  30. */
  31. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
  32. {
  33. if ($response instanceof RedirectResponse) {
  34. $this->data['redirect'] = true;
  35. $this->data['url'] = $response->getTargetUrl();
  36. if ($this->controllers->contains($request)) {
  37. $this->data['route'] = $this->guessRoute($request, $this->controllers[$request]);
  38. }
  39. }
  40. unset($this->controllers[$request]);
  41. }
  42. public function reset(): void
  43. {
  44. $this->controllers = new \SplObjectStorage();
  45. $this->data = [
  46. 'redirect' => false,
  47. 'url' => null,
  48. 'route' => null,
  49. ];
  50. }
  51. protected function guessRoute(Request $request, string|object|array $controller): string
  52. {
  53. return 'n/a';
  54. }
  55. /**
  56. * Remembers the controller associated to each request.
  57. */
  58. public function onKernelController(ControllerEvent $event): void
  59. {
  60. $this->controllers[$event->getRequest()] = $event->getController();
  61. }
  62. /**
  63. * @return bool Whether this request will result in a redirect
  64. */
  65. public function getRedirect(): bool
  66. {
  67. return $this->data['redirect'];
  68. }
  69. public function getTargetUrl(): ?string
  70. {
  71. return $this->data['url'];
  72. }
  73. public function getTargetRoute(): ?string
  74. {
  75. return $this->data['route'];
  76. }
  77. public function getName(): string
  78. {
  79. return 'router';
  80. }
  81. }