1<?php
2
3namespace Doctrine\DBAL\Logging;
4
5use function microtime;
6
7/**
8 * Includes executed SQLs in a Debug Stack.
9 */
10class DebugStack implements SQLLogger
11{
12    /**
13     * Executed SQL queries.
14     *
15     * @var array<int, array<string, mixed>>
16     */
17    public $queries = [];
18
19    /**
20     * If Debug Stack is enabled (log queries) or not.
21     *
22     * @var bool
23     */
24    public $enabled = true;
25
26    /** @var float|null */
27    public $start = null;
28
29    /** @var int */
30    public $currentQuery = 0;
31
32    /**
33     * {@inheritdoc}
34     */
35    public function startQuery($sql, ?array $params = null, ?array $types = null)
36    {
37        if (! $this->enabled) {
38            return;
39        }
40
41        $this->start = microtime(true);
42
43        $this->queries[++$this->currentQuery] = [
44            'sql' => $sql,
45            'params' => $params,
46            'types' => $types,
47            'executionMS' => 0,
48        ];
49    }
50
51    /**
52     * {@inheritdoc}
53     */
54    public function stopQuery()
55    {
56        if (! $this->enabled) {
57            return;
58        }
59
60        $this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
61    }
62}
63