菜谱项目

RouterDataCollector.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public function onKernelController(FilterControllerEvent $event)
  54. {
  55. $this->controllers[$event->getRequest()] = $event->getController();
  56. }
  57. /**
  58. * @return bool Whether this request will result in a redirect
  59. */
  60. public function getRedirect()
  61. {
  62. return $this->data['redirect'];
  63. }
  64. /**
  65. * @return string|null The target URL
  66. */
  67. public function getTargetUrl()
  68. {
  69. return $this->data['url'];
  70. }
  71. /**
  72. * @return string|null The target route
  73. */
  74. public function getTargetRoute()
  75. {
  76. return $this->data['route'];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getName()
  82. {
  83. return 'router';
  84. }
  85. }