설명 없음

RouterDataCollector.php 2.5KB

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