DoctrineDataCollector.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\DoctrineBundle\DataCollector;
  11. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Bundle\DoctrineBundle\Logger\DbalLogger;
  15. use Symfony\Bridge\Doctrine\RegistryInterface;
  16. /**
  17. * DoctrineDataCollector.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class DoctrineDataCollector extends DataCollector
  22. {
  23. private $connections;
  24. private $managers;
  25. private $logger;
  26. public function __construct(RegistryInterface $registry, DbalLogger $logger = null)
  27. {
  28. $this->connections = $registry->getConnectionNames();
  29. $this->managers = $registry->getEntityManagerNames();
  30. $this->logger = $logger;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function collect(Request $request, Response $response, \Exception $exception = null)
  36. {
  37. $this->data = array(
  38. 'queries' => null !== $this->logger ? $this->logger->queries : array(),
  39. 'connections' => $this->connections,
  40. 'managers' => $this->managers,
  41. );
  42. }
  43. public function getManagers()
  44. {
  45. return $this->data['managers'];
  46. }
  47. public function getConnections()
  48. {
  49. return $this->data['connections'];
  50. }
  51. public function getQueryCount()
  52. {
  53. return count($this->data['queries']);
  54. }
  55. public function getQueries()
  56. {
  57. return $this->data['queries'];
  58. }
  59. public function getTime()
  60. {
  61. $time = 0;
  62. foreach ($this->data['queries'] as $query) {
  63. $time += $query['executionMS'];
  64. }
  65. return $time;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getName()
  71. {
  72. return 'db';
  73. }
  74. }