1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BfwSql\Observers; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Requests observer. |
9
|
|
|
* Create a log with requests executed and informations about it. |
10
|
|
|
* |
11
|
|
|
* @package bfw-sql |
12
|
|
|
* @author Vermeulen Maxime <[email protected]> |
13
|
|
|
* @version 2.0 |
14
|
|
|
*/ |
15
|
|
|
class Basic implements \SplObserver |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @const ERR_SYSTEM_QUERY_CONTEXT_CLASS Exception code if we receive the |
19
|
|
|
* event "system query" with(out) a query generated by the system. |
20
|
|
|
*/ |
21
|
|
|
const ERR_SYSTEM_QUERY_CONTEXT_CLASS = 2301001; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \BFW\Monolog $monolog The bfw monolog instance define for bfw-sql |
25
|
|
|
*/ |
26
|
|
|
protected $monolog; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string $action The last action to send to observers |
30
|
|
|
*/ |
31
|
|
|
protected $action = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed $context The context to send to observers |
35
|
|
|
*/ |
36
|
|
|
protected $context = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param \BFW\Monolog $monolog |
42
|
|
|
*/ |
43
|
|
|
public function __construct(\BFW\Monolog $monolog) |
44
|
|
|
{ |
45
|
|
|
$this->monolog = $monolog; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Getter accessor to monolog property |
50
|
|
|
* |
51
|
|
|
* @return \BFW\Monolog |
52
|
|
|
*/ |
53
|
|
|
public function getMonolog(): \BFW\Monolog |
54
|
|
|
{ |
55
|
|
|
return $this->monolog; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Getter accessor to action property |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getAction(): string |
64
|
|
|
{ |
65
|
|
|
return $this->action; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Getter accessor to context property |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function getContext() |
74
|
|
|
{ |
75
|
|
|
return $this->context; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function update(\SplSubject $subject) |
82
|
|
|
{ |
83
|
|
|
if ($this->haveMonologHandler() === false) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->action = $subject->getAction(); |
88
|
|
|
$this->context = $subject->getContext(); |
89
|
|
|
|
90
|
|
|
$this->analyzeUpdate(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if there are handlers declared to monolog. |
95
|
|
|
* Not need to run the system if there are no place to send the infos. |
96
|
|
|
* |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
|
|
protected function haveMonologHandler(): bool |
100
|
|
|
{ |
101
|
|
|
$handlers = $this->monolog->getHandlers(); |
102
|
|
|
|
103
|
|
|
return !empty($handlers); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Analyze the update sent by subjects to search if the notify is for us |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected function analyzeUpdate() |
112
|
|
|
{ |
113
|
|
|
if ($this->action === 'user query') { |
114
|
|
|
$this->userQuery(); |
115
|
|
|
} elseif ($this->action === 'system query') { |
116
|
|
|
$this->systemQuery(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Log a query generate/writed by an user. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
protected function userQuery() |
126
|
|
|
{ |
127
|
|
|
$query = $this->context->request; |
128
|
|
|
$error = $this->context->error; |
129
|
|
|
|
130
|
|
|
$this->addQueryToMonoLog($query, $error, []); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Log a query generated by AbstractAction system. |
135
|
|
|
* |
136
|
|
|
* @throws Exception If the context not contain an AbstractAction object. |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function systemQuery() |
141
|
|
|
{ |
142
|
|
|
if ($this->context instanceof \BfwSql\Executers\Common === false) { |
143
|
|
|
throw new Exception( |
144
|
|
|
'"system query" event should have an Executers\Common class' |
145
|
|
|
.' into the context.', |
146
|
|
|
self::ERR_SYSTEM_QUERY_CONTEXT_CLASS |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$query = $this->context->getQuery(); |
151
|
|
|
$queryTxt = $query->getAssembledRequest(); |
152
|
|
|
$preparedArgs = $query->getPreparedParams(); |
153
|
|
|
$error = $this->context->getLastErrorInfos(); |
154
|
|
|
|
155
|
|
|
$this->addQueryToMonoLog($queryTxt, $error, $preparedArgs); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Send a query to monolog |
160
|
|
|
* |
161
|
|
|
* @param string $query The query to log |
162
|
|
|
* @param array $error The error infos about the query |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
protected function addQueryToMonoLog( |
167
|
|
|
string $query, |
168
|
|
|
array $error, |
169
|
|
|
array $preparedArgs |
170
|
|
|
) { |
171
|
|
|
$logTxt = 'Type: '.$this->action.' ; ' |
172
|
|
|
.'Query: '.$query. ' ; ' |
173
|
|
|
.'Errors: '.print_r($error, true). ';'; |
174
|
|
|
|
175
|
|
|
$this->monolog->getLogger()->debug($logTxt, $preparedArgs); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|