1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHALCON-EXT package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhalconExt\Db; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\Adapter; |
|
|
|
|
15
|
|
|
use Phalcon\Events\Event; |
|
|
|
|
16
|
|
|
use PhalconExt\Logger\LogsToFile; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SQL logger implemented as db event listener. |
20
|
|
|
* |
21
|
|
|
* @author Jitendra Adhikari <[email protected]> |
22
|
|
|
* @license MIT |
23
|
|
|
* |
24
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
25
|
|
|
*/ |
26
|
|
|
class Logger |
27
|
|
|
{ |
28
|
|
|
use LogsToFile; |
29
|
|
|
|
30
|
|
|
/** @var int The current count of fired sqls */ |
31
|
|
|
protected $index = -1; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
protected $config = []; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $logFormat = "%message%;\n"; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $fileExtension = '.sql'; |
41
|
|
|
|
42
|
|
|
public function __construct(array $config) |
43
|
|
|
{ |
44
|
|
|
$this->config = $config + ['skipFirst' => 0, 'addHeader' => false, 'backtraceLevel' => 0]; |
45
|
|
|
|
46
|
|
|
if (!empty($this->config['enabled']) && !empty($this->config['logPath'])) { |
47
|
|
|
$this->activate($this->config['logPath']); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Run before firing query. |
53
|
|
|
* |
54
|
|
|
* @param Event $event |
55
|
|
|
* @param Adapter $connection |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function beforeQuery(Event $event, Adapter $connection) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (!$this->activated) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (++$this->index < $this->config['skipFirst']) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->log($this->getHeader() . $this->getBacktrace() . $this->getParsedSql($connection)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get log header like request uri and datetime. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getHeader(): string |
78
|
|
|
{ |
79
|
|
|
if ($this->index !== $this->config['skipFirst'] || !$this->config['addHeader']) { |
80
|
|
|
return ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$header = '-- ' . ($_SERVER['REQUEST_URI'] ?? '') . \date(' [Y-m-d H:i:s]') . "\n"; |
84
|
|
|
|
85
|
|
|
return $header |
86
|
|
|
. \str_pad('-- -', \strlen($header), '-', STR_PAD_RIGHT) |
87
|
|
|
. "\n"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the backtrace of paths leading to logged query. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getBacktrace(): string |
96
|
|
|
{ |
97
|
|
|
if ($this->config['backtraceLevel'] < 1) { |
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$trace = ''; |
102
|
|
|
foreach (\array_slice(\debug_backtrace(), 2, $this->config['backtraceLevel']) as $tr) { |
103
|
|
|
$tr += ['function' => 'n/a', 'line' => 0, 'file' => 'n/a']; |
104
|
|
|
|
105
|
|
|
$trace .= " -- {$tr['file']}:{$tr['line']}#{$tr['function']}()\n"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $trace; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the properly interpolated sql. |
113
|
|
|
* |
114
|
|
|
* @param Adapter $connection |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getParsedSql($connection): string |
119
|
|
|
{ |
120
|
|
|
$parts = $connection->convertBoundParams( |
121
|
|
|
$connection->getSqlStatement(), |
122
|
|
|
$connection->getSQLVariables() ?: [] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$binds = $parts['params'] ?: $connection->getSQLVariables(); |
126
|
|
|
|
127
|
|
|
return \vsprintf(\str_replace('?', "'%s'", $parts['sql']), $binds); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Run after firing query. |
132
|
|
|
* |
133
|
|
|
* @param Event $event |
134
|
|
|
* @param Adapter $connection |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function afterQuery(Event $event, $connection) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
// Do nothing. |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths