|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Propel\PropelBundle\DataCollector; |
| 13 | + |
| 14 | +use Propel\PropelBundle\Logger\PropelLogger; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | +use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 18 | + |
| 19 | +/** |
| 20 | + * The PropelDataCollector collector class collects information. |
| 21 | + * |
| 22 | + * @author William Durand <william.durand1@gmail.com> |
| 23 | + */ |
| 24 | +class PropelDataCollector extends DataCollector |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Propel logger. |
| 28 | + * |
| 29 | + * @var PropelLogger |
| 30 | + */ |
| 31 | + private $logger; |
| 32 | + |
| 33 | + /** |
| 34 | + * Propel configuration. |
| 35 | + * |
| 36 | + * @var \PropelConfiguration |
| 37 | + */ |
| 38 | + protected $propelConfiguration; |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructor. |
| 42 | + * |
| 43 | + * @param PropelLogger $logger A Propel logger. |
| 44 | + * @param \PropelConfiguration $propelConfiguration The Propel configuration object. |
| 45 | + */ |
| 46 | + public function __construct(PropelLogger $logger, \PropelConfiguration $propelConfiguration) |
| 47 | + { |
| 48 | + $this->logger = $logger; |
| 49 | + $this->propelConfiguration = $propelConfiguration; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + public function collect(Request $request, Response $response, \Exception $exception = null) |
| 56 | + { |
| 57 | + $this->data = array( |
| 58 | + 'queries' => $this->buildQueries(), |
| 59 | + 'querycount' => $this->countQueries(), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the collector name. |
| 65 | + * |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + public function getName() |
| 69 | + { |
| 70 | + return 'propel'; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns the collected stats for all executed SQL queries. |
| 75 | + * |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + public function getQueries() |
| 79 | + { |
| 80 | + return $this->data['queries']; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the query count. |
| 85 | + * |
| 86 | + * @return int |
| 87 | + */ |
| 88 | + public function getQueryCount() |
| 89 | + { |
| 90 | + return $this->data['querycount']; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the total time spent on running all queries. |
| 95 | + * |
| 96 | + * @return float |
| 97 | + */ |
| 98 | + public function getTime() |
| 99 | + { |
| 100 | + $time = 0; |
| 101 | + foreach ($this->data['queries'] as $query) { |
| 102 | + $time += (float) $query['time']; |
| 103 | + } |
| 104 | + |
| 105 | + return $time; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Computes the stats of all executed SQL queries. |
| 110 | + * |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + private function buildQueries() |
| 114 | + { |
| 115 | + $queries = array(); |
| 116 | + |
| 117 | + $outerGlue = $this->propelConfiguration->getParameter('debugpdo.logging.outerglue', ' | '); |
| 118 | + $innerGlue = $this->propelConfiguration->getParameter('debugpdo.logging.innerglue', ': '); |
| 119 | + |
| 120 | + foreach ($this->logger->getQueries() as $q) { |
| 121 | + $parts = explode($outerGlue, $q, 4); |
| 122 | + |
| 123 | + $times = explode($innerGlue, $parts[0]); |
| 124 | + $con = explode($innerGlue, $parts[2]); |
| 125 | + $memories = explode($innerGlue, $parts[1]); |
| 126 | + |
| 127 | + $sql = trim($parts[3]); |
| 128 | + $con = trim($con[1]); |
| 129 | + $time = trim($times[1]); |
| 130 | + $memory = trim($memories[1]); |
| 131 | + |
| 132 | + $queries[] = array('connection' => $con, 'sql' => $sql, 'time' => $time, 'memory' => $memory); |
| 133 | + } |
| 134 | + |
| 135 | + return $queries; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns the total count of SQL queries. |
| 140 | + * |
| 141 | + * @return int |
| 142 | + */ |
| 143 | + private function countQueries() |
| 144 | + { |
| 145 | + return count($this->logger->getQueries()); |
| 146 | + } |
| 147 | +} |
0 commit comments