ProfilerController.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Bundle
  4. *
  5. * The code was originally distributed inside the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Doctrine\Bundle\DoctrineBundle\Controller;
  14. use Symfony\Component\DependencyInjection\ContainerAware;
  15. use Symfony\Component\HttpFoundation\Response;
  16. /**
  17. * ProfilerController.
  18. *
  19. * @author Christophe Coevoet <stof@notk.org>
  20. */
  21. class ProfilerController extends ContainerAware
  22. {
  23. /**
  24. * Renders the profiler panel for the given token.
  25. *
  26. * @param string $token The profiler token
  27. * @param string $connectionName
  28. * @param integer $query
  29. *
  30. * @return Response A Response instance
  31. */
  32. public function explainAction($token, $connectionName, $query)
  33. {
  34. /** @var $profiler \Symfony\Component\HttpKernel\Profiler\Profiler */
  35. $profiler = $this->container->get('profiler');
  36. $profiler->disable();
  37. $profile = $profiler->loadProfile($token);
  38. $queries = $profile->getCollector('db')->getQueries();
  39. if (!isset($queries[$connectionName][$query])) {
  40. return new Response('This query does not exist.');
  41. }
  42. $query = $queries[$connectionName][$query];
  43. if (!$query['explainable']) {
  44. return new Response('This query cannot be explained.');
  45. }
  46. /** @var $connection \Doctrine\DBAL\Connection */
  47. $connection = $this->container->get('doctrine')->getConnection($connectionName);
  48. try {
  49. $results = $connection->executeQuery('EXPLAIN '.$query['sql'], $query['params'], $query['types'])
  50. ->fetchAll(\PDO::FETCH_ASSOC);
  51. } catch (\Exception $e) {
  52. return new Response('This query cannot be explained.');
  53. }
  54. return $this->container->get('templating')->renderResponse('DoctrineBundle:Collector:explain.html.twig', array(
  55. 'data' => $results,
  56. 'query' => $query,
  57. ));
  58. }
  59. }