| Total Complexity | 9 |
| Total Lines | 76 |
| Duplicated Lines | 0 % |
| Coverage | 76% |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class DebugStack implements SQLLogger |
||
| 11 | { |
||
| 12 | const SOURCE_DISABLED = 0; |
||
| 13 | const SOURCE_ENABLED = 1; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Executed SQL queries. |
||
| 17 | * |
||
| 18 | * @var mixed[][] |
||
| 19 | */ |
||
| 20 | public $queries = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * If Debug Stack is enabled (log queries) or not. |
||
| 24 | * |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | public $enabled = true; |
||
| 28 | |||
| 29 | /** @var float|null */ |
||
| 30 | public $start = null; |
||
| 31 | |||
| 32 | /** @var int */ |
||
| 33 | public $currentQuery = 0; |
||
| 34 | |||
| 35 | private $includeSource; |
||
| 36 | |||
| 37 | 7963 | public function __construct($includeSource = self::SOURCE_DISABLED) |
|
| 38 | { |
||
| 39 | 7963 | $this->includeSource = $includeSource; |
|
| 40 | 7963 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | 7479 | public function startQuery($sql, ?array $params = null, ?array $types = null) |
|
| 46 | { |
||
| 47 | 7479 | if (! $this->enabled) { |
|
| 48 | 27 | return; |
|
| 49 | } |
||
| 50 | |||
| 51 | 7452 | $this->start = microtime(true); |
|
| 52 | $log = [ |
||
| 53 | 7452 | 'sql' => $sql, |
|
| 54 | 7452 | 'params' => $params, |
|
| 55 | 7452 | 'types' => $types, |
|
| 56 | 7452 | 'executionMS' => 0, |
|
| 57 | ]; |
||
| 58 | |||
| 59 | 7452 | if ($this->includeSource !== self::SOURCE_DISABLED) { |
|
| 60 | $log['querySource'] = $this->findQuerySource(); |
||
| 61 | } |
||
| 62 | |||
| 63 | 7452 | $this->queries[++$this->currentQuery] = $log; |
|
| 64 | 7452 | } |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 7307 | public function stopQuery() |
|
| 70 | { |
||
| 71 | 7307 | if (! $this->enabled) { |
|
| 72 | 27 | return; |
|
| 73 | } |
||
| 74 | |||
| 75 | 7280 | $this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start; |
|
| 76 | 7280 | } |
|
| 77 | |||
| 78 | private function findQuerySource() |
||
| 86 | } |
||
| 87 | } |
||
| 88 |