RequestDataCollector.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Bundle\FrameworkBundle\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestDataCollector;
  14. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  15. /**
  16. * RequestDataCollector.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class RequestDataCollector extends BaseRequestDataCollector
  21. {
  22. protected $controllers;
  23. public function __construct()
  24. {
  25. $this->controllers = new \SplObjectStorage();
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function collect(Request $request, Response $response, \Exception $exception = null)
  31. {
  32. parent::collect($request, $response, $exception);
  33. $this->data['route'] = $request->attributes->get('_route');
  34. $this->data['controller'] = 'n/a';
  35. if (isset($this->controllers[$request])) {
  36. $controller = $this->controllers[$request];
  37. if (is_array($controller)) {
  38. $this->data['controller'] = array(get_class($controller[0]), $controller[1]);
  39. } elseif ($controller instanceof \Closure) {
  40. $this->data['controller'] = 'Closure';
  41. } else {
  42. $this->data['controller'] = (string) $controller ?: 'n/a';
  43. }
  44. unset($this->controllers[$request]);
  45. }
  46. }
  47. public function onCoreController(FilterControllerEvent $event)
  48. {
  49. $this->controllers[$event->getRequest()] = $event->getController();
  50. }
  51. /**
  52. * Gets the route.
  53. *
  54. * @return string The route
  55. */
  56. public function getRoute()
  57. {
  58. return $this->data['route'];
  59. }
  60. /**
  61. * Gets the controller.
  62. *
  63. * @return string The controller as a string
  64. */
  65. public function getController()
  66. {
  67. return $this->data['controller'];
  68. }
  69. }