RouterDataCollector.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  15. /**
  16. * RouterDataCollector.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class RouterDataCollector extends DataCollector
  21. {
  22. protected $controllers;
  23. public function __construct()
  24. {
  25. $this->controllers = new \SplObjectStorage();
  26. $this->data = array(
  27. 'redirect' => false,
  28. 'url' => null,
  29. 'route' => null,
  30. );
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function collect(Request $request, Response $response, \Exception $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. protected function guessRoute(Request $request, $controller)
  47. {
  48. return 'n/a';
  49. }
  50. /**
  51. * Remembers the controller associated to each request.
  52. *
  53. * @param FilterControllerEvent $event The filter controller event
  54. */
  55. public function onKernelController(FilterControllerEvent $event)
  56. {
  57. $this->controllers[$event->getRequest()] = $event->getController();
  58. }
  59. /**
  60. * @return bool Whether this request will result in a redirect
  61. */
  62. public function getRedirect()
  63. {
  64. return $this->data['redirect'];
  65. }
  66. /**
  67. * @return string|null The target URL
  68. */
  69. public function getTargetUrl()
  70. {
  71. return $this->data['url'];
  72. }
  73. /**
  74. * @return string|null The target route
  75. */
  76. public function getTargetRoute()
  77. {
  78. return $this->data['route'];
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getName()
  84. {
  85. return 'router';
  86. }
  87. }