rostenkowski /
doctrine
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Rostenkowski\Doctrine\Debugger; |
||
| 4 | |||
| 5 | |||
| 6 | use Doctrine\DBAL\Logging\SQLLogger; |
||
| 7 | use Nette\Utils\Html; |
||
| 8 | use Tracy\Dumper; |
||
| 9 | use Tracy\Helpers; |
||
| 10 | use Tracy\IBarPanel; |
||
| 11 | use const DEBUG_BACKTRACE_IGNORE_ARGS; |
||
| 12 | use function count; |
||
| 13 | use function debug_backtrace; |
||
| 14 | use function dirname; |
||
| 15 | use function strlen; |
||
| 16 | use function substr; |
||
| 17 | |||
| 18 | class TracyBar implements SQLLogger, IBarPanel |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $appDir; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $tempDir; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var float |
||
| 33 | */ |
||
| 34 | private $start; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $current; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $queries = []; |
||
| 45 | |||
| 46 | private $totalTime; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * debugger panel max width |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $width = '960px'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * debugger panel max height |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $height = '720px'; |
||
| 61 | |||
| 62 | |||
| 63 | public function __construct(string $appDir, string $tempDir) |
||
| 64 | { |
||
| 65 | $this->appDir = $appDir; |
||
| 66 | $this->tempDir = $tempDir; |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | public function getHeight(): string |
||
| 71 | { |
||
| 72 | return $this->height; |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | public function setHeight(string $height) |
||
| 77 | { |
||
| 78 | $this->height = $height; |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | public function getPanel(): string |
||
| 83 | { |
||
| 84 | $totalQueries = count($this->queries); |
||
| 85 | $color = $totalQueries ? 'green' : '#555555'; |
||
| 86 | $timeCaption = $this->formatTime($this->getTotalTime()); |
||
| 87 | $panel = $this->getTemplate('panel'); |
||
| 88 | $row = $this->getTemplate('query'); |
||
| 89 | $buffer = ''; |
||
| 90 | $colorizer = new SimpleQueryColorizer(); |
||
| 91 | foreach ($this->queries as $query) { |
||
| 92 | |||
| 93 | $link = Helpers::editorUri($query['file'], $query['line']); |
||
| 94 | $linkText = '…/' . substr($query['file'], strlen(dirname($this->appDir)) + 1) . ':' . $query['line']; |
||
| 95 | $a = Html::el('a')->setAttribute('href', $link)->setText($linkText); |
||
| 96 | $buffer .= sprintf($row, |
||
| 97 | $this->formatTime($query['dur']), |
||
| 98 | $colorizer->colorize($query['sql'], true), |
||
| 99 | $this->dump($query['params']), |
||
| 100 | (string) $a |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | return sprintf($panel, $color, $timeCaption, $totalQueries, $this->width, $this->height, $buffer); |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | public function getTab(): string |
||
| 109 | { |
||
| 110 | $count = count($this->queries); |
||
| 111 | $color = $count ? 'green' : '#555555'; |
||
| 112 | $time = $this->formatTime($this->getTotalTime()); |
||
| 113 | $template = $this->getTemplate('tab'); |
||
| 114 | |||
| 115 | return sprintf($template, $color, $time, $count); |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | private function formatTime($microseconds) |
||
| 120 | { |
||
| 121 | return number_format($microseconds * 1000, 1, '.', ' ') . ' ms'; |
||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | private function getTotalTime() |
||
| 126 | { |
||
| 127 | if ($this->totalTime === NULL) { |
||
| 128 | foreach ($this->queries as $query) { |
||
| 129 | $this->totalTime += $query['dur']; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | return $this->totalTime; |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | private function getTemplate($name) |
||
| 138 | { |
||
| 139 | return file_get_contents(__DIR__ . "/templates/$name.html"); |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | private function dump($params) |
||
| 144 | { |
||
| 145 | if ($params) { |
||
| 146 | |||
| 147 | return Dumper::toHtml($params, [Dumper::COLLAPSE => 1]); |
||
| 148 | } |
||
| 149 | |||
| 150 | return ''; |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | public function getWidth(): string |
||
| 155 | { |
||
| 156 | return $this->width; |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | public function setWidth(string $width) |
||
| 161 | { |
||
| 162 | $this->width = $width; |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | public function startQuery($sql, array $params = NULL, array $types = NULL) |
||
| 167 | { |
||
| 168 | $this->start = (float) microtime(true); |
||
| 169 | |||
| 170 | $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
| 171 | |||
| 172 | foreach ($trace as $i => $item) { |
||
| 173 | if (isset($trace[$i]['file'])) { |
||
| 174 | if ( |
||
| 175 | substr($trace[$i]['file'], 0, strlen($this->tempDir)) === $this->tempDir |
||
| 176 | || |
||
| 177 | substr($trace[$i]['file'], 0, strlen($this->appDir)) === $this->appDir |
||
| 178 | ) { |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->queries[++$this->current] = [ |
||
| 185 | 'sql' => trim($sql, " \t\n\r\0\x0B\""), |
||
| 186 | 'params' => $params, |
||
| 187 | 'types' => $types, |
||
| 188 | 'dur' => 0, |
||
| 189 | 'file' => $trace[$i]['file'], |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 190 | 'line' => $trace[$i]['line'], |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | |||
| 194 | |||
| 195 | public function stopQuery() |
||
| 196 | { |
||
| 197 | $this->queries[$this->current]['dur'] = (float) microtime(true) - $this->start; |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | } |
||
| 202 |