-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathSqlTraceTrait.php
More file actions
95 lines (86 loc) · 3 KB
/
SqlTraceTrait.php
File metadata and controls
95 lines (86 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 5.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Model\Table;
use Cake\Core\Configure;
use Cake\Error\Debugger;
use Cake\ORM\Query\DeleteQuery;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\Query\UpdateQuery;
/**
* Add this trait to your Table class to append the file reference of where a Query object was created.
*
* @mixin \Cake\ORM\Table
*/
trait SqlTraceTrait
{
/**
* Overwrite parent table method to inject SQL comment
*/
public function selectQuery(): SelectQuery
{
return $this->fileStamp(parent::selectQuery());
}
/**
* Overwrite parent table method to inject SQL comment
*/
public function updateQuery(): UpdateQuery
{
return $this->fileStamp(parent::updateQuery());
}
/**
* Overwrite parent table method to inject SQL comment
*/
public function deleteQuery(): DeleteQuery
{
return $this->fileStamp(parent::deleteQuery());
}
/**
* Applies a comment to a query about which file created it.
*
* @template T of \Cake\ORM\Query\SelectQuery|\Cake\ORM\Query\UpdateQuery|\Cake\ORM\Query\DeleteQuery
* @param \Cake\ORM\Query\SelectQuery|\Cake\ORM\Query\UpdateQuery|\Cake\ORM\Query\DeleteQuery $query The Query to insert a comment into.
* @phpstan-param T $query
* @param int $start How many entries in the stack trace to skip.
* @param bool $debugOnly False to always stamp queries with a comment.
* @return \Cake\ORM\Query\SelectQuery|\Cake\ORM\Query\UpdateQuery|\Cake\ORM\Query\DeleteQuery
* @phpstan-return T
*/
protected function fileStamp(
SelectQuery|UpdateQuery|DeleteQuery $query,
int $start = 1,
bool $debugOnly = true,
): SelectQuery|UpdateQuery|DeleteQuery {
if (!Configure::read('debug') && $debugOnly === true) {
return $query;
}
$traces = Debugger::trace(['start' => $start, 'format' => 'array']);
$file = '[unknown]';
$line = '??';
if (is_array($traces)) {
foreach ($traces as $trace) {
$path = $trace['file'];
$line = $trace['line'];
$file = Debugger::trimPath($path);
if ($path === '[internal]') {
continue;
}
if (defined('CAKE_CORE_INCLUDE_PATH') && strpos($path, CAKE_CORE_INCLUDE_PATH) !== 0) {
break;
}
}
}
return $query->comment(sprintf('%s (line %s)', $file, $line));
}
}