Completed
Branch master (d17104)
by Christian
21:20
created

DoctrineSqlLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A startQuery() 0 21 4
A stopQuery() 0 4 2
A getQueries() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Log;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use Doctrine\DBAL\Logging\SQLLogger;
20
use Psr\Log\LoggerAwareInterface;
21
use Psr\Log\LoggerAwareTrait;
22
use TYPO3\CMS\Adminpanel\Utility\MemoryUtility;
23
24
/**
25
 * Doctrine SQL Logger implementation for recording queries for the admin panel
26
 */
27
class DoctrineSqlLogger implements SQLLogger, LoggerAwareInterface
28
{
29
    use LoggerAwareTrait;
30
31
    /**
32
     * Executed SQL queries.
33
     *
34
     * @var array
35
     */
36
    protected $queries = [];
37
38
    /**
39
     * If Debug Stack is enabled (log queries) or not.
40
     *
41
     * @var bool
42
     */
43
    protected $enabled = true;
44
45
    /**
46
     * @var float
47
     */
48
    protected $start;
49
50
    /**
51
     * @var int
52
     */
53
    protected $currentQuery = 0;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function startQuery($sql, array $params = null, array $types = null)
59
    {
60
        if ($this->enabled && MemoryUtility::isMemoryConsumptionTooHigh()) {
61
            $this->enabled = false;
62
            $this->logger->warning('SQL Logging consumed too much memory, aborted. Not all queries have been recorded.');
63
        }
64
        if ($this->enabled) {
65
            $this->start = microtime(true);
66
            $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7);
67
            // remove this method
68
            array_shift($backtrace);
69
            // remove doctrine execute query
70
            array_shift($backtrace);
71
            // remove queryBuilder execute
72
            array_shift($backtrace);
73
            $this->queries[++$this->currentQuery] = [
74
                'sql' => $sql,
75
                'params' => $params,
76
                'types' => $types,
77
                'executionMS' => 0,
78
                'backtrace' => $backtrace
79
            ];
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function stopQuery()
87
    {
88
        if ($this->enabled) {
89
            $this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
90
        }
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getQueries(): array
97
    {
98
        return $this->queries;
99
    }
100
}
101