-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathApiQueryLogListener.php
104 lines (93 loc) · 2.96 KB
/
ApiQueryLogListener.php
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
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace Crud\Listener;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\Exception\MissingDatasourceConfigException;
use Cake\Event\EventInterface;
use Crud\Log\QueryLogger;
use Crud\Traits\QueryLogTrait;
/**
* When loaded Crud API will include query logs in the response
*
* Very much like the DebugKit version, the SQL log will only be appended
* if the following conditions is true:
* 1) The request must be 'api' (.json/.xml) or 'jsonapi'
* 2) The debug level must be 2 or above
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
*/
class ApiQueryLogListener extends BaseListener
{
use QueryLogTrait;
/**
* {@inheritDoc}
*
* `connections` List of connection names to log. Empty means all defined connections.
*/
protected array $_defaultConfig = [
'connections' => [],
];
/**
* Returns a list of all events that will fire in the controller during its lifecycle.
* You can override this function to add you own listener callbacks
*
* We attach at priority 10 so normal bound events can run before us
*
* @return array<string, mixed>
*/
public function implementedEvents(): array
{
if (!$this->_checkRequestType('api')) {
return [];
}
return [
'Crud.beforeFilter' => ['callable' => [$this, 'setupLogging'], 'priority' => 1],
'Crud.beforeRender' => ['callable' => [$this, 'beforeRender'], 'priority' => 75],
];
}
/**
* Setup logging for all connections
*
* @param \Cake\Event\EventInterface<\Crud\Event\Subject> $event Event
* @return void
*/
public function setupLogging(EventInterface $event): void
{
$connections = $this->getConfig('connections') ?: ConnectionManager::configured();
foreach ($connections as $connectionName) {
try {
$driver = ConnectionManager::get($connectionName)->getDriver();
if (method_exists($driver, 'setLogger')) {
$driver->setLogger(new QueryLogger());
}
} catch (MissingDatasourceConfigException $e) {
//Safe to ignore this :-)
}
}
}
/**
* Appends the query log to the JSON or XML output
*
* @param \Cake\Event\EventInterface<\Crud\Event\Subject> $event Event
* @return void
*/
public function beforeRender(EventInterface $event): void
{
if (!Configure::read('debug')) {
return;
}
$this->_action()->setConfig('serialize.queryLog', 'queryLog');
$this->_controller()->set('queryLog', $this->getQueryLogs());
}
/**
* Public getter to expose logs for use in other (exception) classes.
*
* @return array
*/
public function getQueryLogs(): array
{
return $this->_getQueryLog();
}
}