@@ -29,304 +29,304 @@ |
||
29 | 29 | */ |
30 | 30 | abstract class AbstractLexer |
31 | 31 | { |
32 | - /** |
|
33 | - * Lexer original input string. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $input; |
|
38 | - |
|
39 | - /** |
|
40 | - * Array of scanned tokens. |
|
41 | - * |
|
42 | - * Each token is an associative array containing three items: |
|
43 | - * - 'value' : the string value of the token in the input string |
|
44 | - * - 'type' : the type of the token (identifier, numeric, string, input |
|
45 | - * parameter, none) |
|
46 | - * - 'position' : the position of the token in the input string |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - private $tokens = array(); |
|
51 | - |
|
52 | - /** |
|
53 | - * Current lexer position in input string. |
|
54 | - * |
|
55 | - * @var integer |
|
56 | - */ |
|
57 | - private $position = 0; |
|
58 | - |
|
59 | - /** |
|
60 | - * Current peek of current lexer position. |
|
61 | - * |
|
62 | - * @var integer |
|
63 | - */ |
|
64 | - private $peek = 0; |
|
65 | - |
|
66 | - /** |
|
67 | - * The next token in the input. |
|
68 | - * |
|
69 | - * @var array |
|
70 | - */ |
|
71 | - public $lookahead; |
|
72 | - |
|
73 | - /** |
|
74 | - * The last matched/seen token. |
|
75 | - * |
|
76 | - * @var array |
|
77 | - */ |
|
78 | - public $token; |
|
79 | - |
|
80 | - /** |
|
81 | - * Sets the input data to be tokenized. |
|
82 | - * |
|
83 | - * The Lexer is immediately reset and the new input tokenized. |
|
84 | - * Any unprocessed tokens from any previous input are lost. |
|
85 | - * |
|
86 | - * @param string $input The input to be tokenized. |
|
87 | - * |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function setInput($input) |
|
91 | - { |
|
92 | - $this->input = $input; |
|
93 | - $this->tokens = array(); |
|
94 | - |
|
95 | - $this->reset(); |
|
96 | - $this->scan($input); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Resets the lexer. |
|
101 | - * |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - public function reset() |
|
105 | - { |
|
106 | - $this->lookahead = null; |
|
107 | - $this->token = null; |
|
108 | - $this->peek = 0; |
|
109 | - $this->position = 0; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Resets the peek pointer to 0. |
|
114 | - * |
|
115 | - * @return void |
|
116 | - */ |
|
117 | - public function resetPeek() |
|
118 | - { |
|
119 | - $this->peek = 0; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Resets the lexer position on the input to the given position. |
|
124 | - * |
|
125 | - * @param integer $position Position to place the lexical scanner. |
|
126 | - * |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public function resetPosition($position = 0) |
|
130 | - { |
|
131 | - $this->position = $position; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Retrieve the original lexer's input until a given position. |
|
136 | - * |
|
137 | - * @param integer $position |
|
138 | - * |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function getInputUntilPosition($position) |
|
142 | - { |
|
143 | - return substr($this->input, 0, $position); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Checks whether a given token matches the current lookahead. |
|
148 | - * |
|
149 | - * @param integer|string $token |
|
150 | - * |
|
151 | - * @return boolean |
|
152 | - */ |
|
153 | - public function isNextToken($token) |
|
154 | - { |
|
155 | - return null !== $this->lookahead && $this->lookahead['type'] === $token; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Checks whether any of the given tokens matches the current lookahead. |
|
160 | - * |
|
161 | - * @param array $tokens |
|
162 | - * |
|
163 | - * @return boolean |
|
164 | - */ |
|
165 | - public function isNextTokenAny(array $tokens) |
|
166 | - { |
|
167 | - return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Moves to the next token in the input string. |
|
172 | - * |
|
173 | - * @return boolean |
|
174 | - */ |
|
175 | - public function moveNext() |
|
176 | - { |
|
177 | - $this->peek = 0; |
|
178 | - $this->token = $this->lookahead; |
|
179 | - $this->lookahead = (isset($this->tokens[$this->position])) |
|
180 | - ? $this->tokens[$this->position++] : null; |
|
181 | - |
|
182 | - return $this->lookahead !== null; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Tells the lexer to skip input tokens until it sees a token with the given value. |
|
187 | - * |
|
188 | - * @param string $type The token type to skip until. |
|
189 | - * |
|
190 | - * @return void |
|
191 | - */ |
|
192 | - public function skipUntil($type) |
|
193 | - { |
|
194 | - while ($this->lookahead !== null && $this->lookahead['type'] !== $type) { |
|
195 | - $this->moveNext(); |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Checks if given value is identical to the given token. |
|
201 | - * |
|
202 | - * @param mixed $value |
|
203 | - * @param integer $token |
|
204 | - * |
|
205 | - * @return boolean |
|
206 | - */ |
|
207 | - public function isA($value, $token) |
|
208 | - { |
|
209 | - return $this->getType($value) === $token; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Moves the lookahead token forward. |
|
214 | - * |
|
215 | - * @return array|null The next token or NULL if there are no more tokens ahead. |
|
216 | - */ |
|
217 | - public function peek() |
|
218 | - { |
|
219 | - if (isset($this->tokens[$this->position + $this->peek])) { |
|
220 | - return $this->tokens[$this->position + $this->peek++]; |
|
221 | - } else { |
|
222 | - return null; |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Peeks at the next token, returns it and immediately resets the peek. |
|
228 | - * |
|
229 | - * @return array|null The next token or NULL if there are no more tokens ahead. |
|
230 | - */ |
|
231 | - public function glimpse() |
|
232 | - { |
|
233 | - $peek = $this->peek(); |
|
234 | - $this->peek = 0; |
|
235 | - return $peek; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Scans the input string for tokens. |
|
240 | - * |
|
241 | - * @param string $input A query string. |
|
242 | - * |
|
243 | - * @return void |
|
244 | - */ |
|
245 | - protected function scan($input) |
|
246 | - { |
|
247 | - static $regex; |
|
248 | - |
|
249 | - if ( ! isset($regex)) { |
|
250 | - $regex = sprintf( |
|
251 | - '/(%s)|%s/%s', |
|
252 | - implode(')|(', $this->getCatchablePatterns()), |
|
253 | - implode('|', $this->getNonCatchablePatterns()), |
|
254 | - $this->getModifiers() |
|
255 | - ); |
|
256 | - } |
|
257 | - |
|
258 | - $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; |
|
259 | - $matches = preg_split($regex, $input, -1, $flags); |
|
260 | - |
|
261 | - if (false === $matches) { |
|
262 | - // Work around https://bugs.php.net/78122 |
|
263 | - $matches = array(array($input, 0)); |
|
264 | - } |
|
265 | - |
|
266 | - foreach ($matches as $match) { |
|
267 | - // Must remain before 'value' assignment since it can change content |
|
268 | - $type = $this->getType($match[0]); |
|
269 | - |
|
270 | - $this->tokens[] = array( |
|
271 | - 'value' => $match[0], |
|
272 | - 'type' => $type, |
|
273 | - 'position' => $match[1], |
|
274 | - ); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Gets the literal for a given token. |
|
280 | - * |
|
281 | - * @param integer $token |
|
282 | - * |
|
283 | - * @return string |
|
284 | - */ |
|
285 | - public function getLiteral($token) |
|
286 | - { |
|
287 | - $className = get_class($this); |
|
288 | - $reflClass = new \ReflectionClass($className); |
|
289 | - $constants = $reflClass->getConstants(); |
|
290 | - |
|
291 | - foreach ($constants as $name => $value) { |
|
292 | - if ($value === $token) { |
|
293 | - return $className . '::' . $name; |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - return $token; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Regex modifiers |
|
302 | - * |
|
303 | - * @return string |
|
304 | - */ |
|
305 | - protected function getModifiers() |
|
306 | - { |
|
307 | - return 'i'; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Lexical catchable patterns. |
|
312 | - * |
|
313 | - * @return array |
|
314 | - */ |
|
315 | - abstract protected function getCatchablePatterns(); |
|
316 | - |
|
317 | - /** |
|
318 | - * Lexical non-catchable patterns. |
|
319 | - * |
|
320 | - * @return array |
|
321 | - */ |
|
322 | - abstract protected function getNonCatchablePatterns(); |
|
323 | - |
|
324 | - /** |
|
325 | - * Retrieve token type. Also processes the token value if necessary. |
|
326 | - * |
|
327 | - * @param string $value |
|
328 | - * |
|
329 | - * @return integer |
|
330 | - */ |
|
331 | - abstract protected function getType(&$value); |
|
32 | + /** |
|
33 | + * Lexer original input string. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $input; |
|
38 | + |
|
39 | + /** |
|
40 | + * Array of scanned tokens. |
|
41 | + * |
|
42 | + * Each token is an associative array containing three items: |
|
43 | + * - 'value' : the string value of the token in the input string |
|
44 | + * - 'type' : the type of the token (identifier, numeric, string, input |
|
45 | + * parameter, none) |
|
46 | + * - 'position' : the position of the token in the input string |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + private $tokens = array(); |
|
51 | + |
|
52 | + /** |
|
53 | + * Current lexer position in input string. |
|
54 | + * |
|
55 | + * @var integer |
|
56 | + */ |
|
57 | + private $position = 0; |
|
58 | + |
|
59 | + /** |
|
60 | + * Current peek of current lexer position. |
|
61 | + * |
|
62 | + * @var integer |
|
63 | + */ |
|
64 | + private $peek = 0; |
|
65 | + |
|
66 | + /** |
|
67 | + * The next token in the input. |
|
68 | + * |
|
69 | + * @var array |
|
70 | + */ |
|
71 | + public $lookahead; |
|
72 | + |
|
73 | + /** |
|
74 | + * The last matched/seen token. |
|
75 | + * |
|
76 | + * @var array |
|
77 | + */ |
|
78 | + public $token; |
|
79 | + |
|
80 | + /** |
|
81 | + * Sets the input data to be tokenized. |
|
82 | + * |
|
83 | + * The Lexer is immediately reset and the new input tokenized. |
|
84 | + * Any unprocessed tokens from any previous input are lost. |
|
85 | + * |
|
86 | + * @param string $input The input to be tokenized. |
|
87 | + * |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function setInput($input) |
|
91 | + { |
|
92 | + $this->input = $input; |
|
93 | + $this->tokens = array(); |
|
94 | + |
|
95 | + $this->reset(); |
|
96 | + $this->scan($input); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Resets the lexer. |
|
101 | + * |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + public function reset() |
|
105 | + { |
|
106 | + $this->lookahead = null; |
|
107 | + $this->token = null; |
|
108 | + $this->peek = 0; |
|
109 | + $this->position = 0; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Resets the peek pointer to 0. |
|
114 | + * |
|
115 | + * @return void |
|
116 | + */ |
|
117 | + public function resetPeek() |
|
118 | + { |
|
119 | + $this->peek = 0; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Resets the lexer position on the input to the given position. |
|
124 | + * |
|
125 | + * @param integer $position Position to place the lexical scanner. |
|
126 | + * |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public function resetPosition($position = 0) |
|
130 | + { |
|
131 | + $this->position = $position; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Retrieve the original lexer's input until a given position. |
|
136 | + * |
|
137 | + * @param integer $position |
|
138 | + * |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function getInputUntilPosition($position) |
|
142 | + { |
|
143 | + return substr($this->input, 0, $position); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Checks whether a given token matches the current lookahead. |
|
148 | + * |
|
149 | + * @param integer|string $token |
|
150 | + * |
|
151 | + * @return boolean |
|
152 | + */ |
|
153 | + public function isNextToken($token) |
|
154 | + { |
|
155 | + return null !== $this->lookahead && $this->lookahead['type'] === $token; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Checks whether any of the given tokens matches the current lookahead. |
|
160 | + * |
|
161 | + * @param array $tokens |
|
162 | + * |
|
163 | + * @return boolean |
|
164 | + */ |
|
165 | + public function isNextTokenAny(array $tokens) |
|
166 | + { |
|
167 | + return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Moves to the next token in the input string. |
|
172 | + * |
|
173 | + * @return boolean |
|
174 | + */ |
|
175 | + public function moveNext() |
|
176 | + { |
|
177 | + $this->peek = 0; |
|
178 | + $this->token = $this->lookahead; |
|
179 | + $this->lookahead = (isset($this->tokens[$this->position])) |
|
180 | + ? $this->tokens[$this->position++] : null; |
|
181 | + |
|
182 | + return $this->lookahead !== null; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Tells the lexer to skip input tokens until it sees a token with the given value. |
|
187 | + * |
|
188 | + * @param string $type The token type to skip until. |
|
189 | + * |
|
190 | + * @return void |
|
191 | + */ |
|
192 | + public function skipUntil($type) |
|
193 | + { |
|
194 | + while ($this->lookahead !== null && $this->lookahead['type'] !== $type) { |
|
195 | + $this->moveNext(); |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Checks if given value is identical to the given token. |
|
201 | + * |
|
202 | + * @param mixed $value |
|
203 | + * @param integer $token |
|
204 | + * |
|
205 | + * @return boolean |
|
206 | + */ |
|
207 | + public function isA($value, $token) |
|
208 | + { |
|
209 | + return $this->getType($value) === $token; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Moves the lookahead token forward. |
|
214 | + * |
|
215 | + * @return array|null The next token or NULL if there are no more tokens ahead. |
|
216 | + */ |
|
217 | + public function peek() |
|
218 | + { |
|
219 | + if (isset($this->tokens[$this->position + $this->peek])) { |
|
220 | + return $this->tokens[$this->position + $this->peek++]; |
|
221 | + } else { |
|
222 | + return null; |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Peeks at the next token, returns it and immediately resets the peek. |
|
228 | + * |
|
229 | + * @return array|null The next token or NULL if there are no more tokens ahead. |
|
230 | + */ |
|
231 | + public function glimpse() |
|
232 | + { |
|
233 | + $peek = $this->peek(); |
|
234 | + $this->peek = 0; |
|
235 | + return $peek; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Scans the input string for tokens. |
|
240 | + * |
|
241 | + * @param string $input A query string. |
|
242 | + * |
|
243 | + * @return void |
|
244 | + */ |
|
245 | + protected function scan($input) |
|
246 | + { |
|
247 | + static $regex; |
|
248 | + |
|
249 | + if ( ! isset($regex)) { |
|
250 | + $regex = sprintf( |
|
251 | + '/(%s)|%s/%s', |
|
252 | + implode(')|(', $this->getCatchablePatterns()), |
|
253 | + implode('|', $this->getNonCatchablePatterns()), |
|
254 | + $this->getModifiers() |
|
255 | + ); |
|
256 | + } |
|
257 | + |
|
258 | + $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; |
|
259 | + $matches = preg_split($regex, $input, -1, $flags); |
|
260 | + |
|
261 | + if (false === $matches) { |
|
262 | + // Work around https://bugs.php.net/78122 |
|
263 | + $matches = array(array($input, 0)); |
|
264 | + } |
|
265 | + |
|
266 | + foreach ($matches as $match) { |
|
267 | + // Must remain before 'value' assignment since it can change content |
|
268 | + $type = $this->getType($match[0]); |
|
269 | + |
|
270 | + $this->tokens[] = array( |
|
271 | + 'value' => $match[0], |
|
272 | + 'type' => $type, |
|
273 | + 'position' => $match[1], |
|
274 | + ); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Gets the literal for a given token. |
|
280 | + * |
|
281 | + * @param integer $token |
|
282 | + * |
|
283 | + * @return string |
|
284 | + */ |
|
285 | + public function getLiteral($token) |
|
286 | + { |
|
287 | + $className = get_class($this); |
|
288 | + $reflClass = new \ReflectionClass($className); |
|
289 | + $constants = $reflClass->getConstants(); |
|
290 | + |
|
291 | + foreach ($constants as $name => $value) { |
|
292 | + if ($value === $token) { |
|
293 | + return $className . '::' . $name; |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + return $token; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Regex modifiers |
|
302 | + * |
|
303 | + * @return string |
|
304 | + */ |
|
305 | + protected function getModifiers() |
|
306 | + { |
|
307 | + return 'i'; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Lexical catchable patterns. |
|
312 | + * |
|
313 | + * @return array |
|
314 | + */ |
|
315 | + abstract protected function getCatchablePatterns(); |
|
316 | + |
|
317 | + /** |
|
318 | + * Lexical non-catchable patterns. |
|
319 | + * |
|
320 | + * @return array |
|
321 | + */ |
|
322 | + abstract protected function getNonCatchablePatterns(); |
|
323 | + |
|
324 | + /** |
|
325 | + * Retrieve token type. Also processes the token value if necessary. |
|
326 | + * |
|
327 | + * @param string $value |
|
328 | + * |
|
329 | + * @return integer |
|
330 | + */ |
|
331 | + abstract protected function getType(&$value); |
|
332 | 332 | } |
@@ -12,17 +12,17 @@ |
||
12 | 12 | */ |
13 | 13 | class NullLogger extends AbstractLogger |
14 | 14 | { |
15 | - /** |
|
16 | - * Logs with an arbitrary level. |
|
17 | - * |
|
18 | - * @param mixed $level |
|
19 | - * @param string $message |
|
20 | - * @param array $context |
|
21 | - * |
|
22 | - * @return void |
|
23 | - */ |
|
24 | - public function log($level, $message, array $context = array()) |
|
25 | - { |
|
26 | - // noop |
|
27 | - } |
|
15 | + /** |
|
16 | + * Logs with an arbitrary level. |
|
17 | + * |
|
18 | + * @param mixed $level |
|
19 | + * @param string $message |
|
20 | + * @param array $context |
|
21 | + * |
|
22 | + * @return void |
|
23 | + */ |
|
24 | + public function log($level, $message, array $context = array()) |
|
25 | + { |
|
26 | + // noop |
|
27 | + } |
|
28 | 28 | } |
@@ -12,129 +12,129 @@ |
||
12 | 12 | */ |
13 | 13 | trait LoggerTrait |
14 | 14 | { |
15 | - /** |
|
16 | - * System is unusable. |
|
17 | - * |
|
18 | - * @param string $message |
|
19 | - * @param array $context |
|
20 | - * |
|
21 | - * @return void |
|
22 | - */ |
|
23 | - public function emergency($message, array $context = array()) |
|
24 | - { |
|
25 | - $this->log(LogLevel::EMERGENCY, $message, $context); |
|
26 | - } |
|
15 | + /** |
|
16 | + * System is unusable. |
|
17 | + * |
|
18 | + * @param string $message |
|
19 | + * @param array $context |
|
20 | + * |
|
21 | + * @return void |
|
22 | + */ |
|
23 | + public function emergency($message, array $context = array()) |
|
24 | + { |
|
25 | + $this->log(LogLevel::EMERGENCY, $message, $context); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Action must be taken immediately. |
|
30 | - * |
|
31 | - * Example: Entire website down, database unavailable, etc. This should |
|
32 | - * trigger the SMS alerts and wake you up. |
|
33 | - * |
|
34 | - * @param string $message |
|
35 | - * @param array $context |
|
36 | - * |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - public function alert($message, array $context = array()) |
|
40 | - { |
|
41 | - $this->log(LogLevel::ALERT, $message, $context); |
|
42 | - } |
|
28 | + /** |
|
29 | + * Action must be taken immediately. |
|
30 | + * |
|
31 | + * Example: Entire website down, database unavailable, etc. This should |
|
32 | + * trigger the SMS alerts and wake you up. |
|
33 | + * |
|
34 | + * @param string $message |
|
35 | + * @param array $context |
|
36 | + * |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + public function alert($message, array $context = array()) |
|
40 | + { |
|
41 | + $this->log(LogLevel::ALERT, $message, $context); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Critical conditions. |
|
46 | - * |
|
47 | - * Example: Application component unavailable, unexpected exception. |
|
48 | - * |
|
49 | - * @param string $message |
|
50 | - * @param array $context |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function critical($message, array $context = array()) |
|
55 | - { |
|
56 | - $this->log(LogLevel::CRITICAL, $message, $context); |
|
57 | - } |
|
44 | + /** |
|
45 | + * Critical conditions. |
|
46 | + * |
|
47 | + * Example: Application component unavailable, unexpected exception. |
|
48 | + * |
|
49 | + * @param string $message |
|
50 | + * @param array $context |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function critical($message, array $context = array()) |
|
55 | + { |
|
56 | + $this->log(LogLevel::CRITICAL, $message, $context); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Runtime errors that do not require immediate action but should typically |
|
61 | - * be logged and monitored. |
|
62 | - * |
|
63 | - * @param string $message |
|
64 | - * @param array $context |
|
65 | - * |
|
66 | - * @return void |
|
67 | - */ |
|
68 | - public function error($message, array $context = array()) |
|
69 | - { |
|
70 | - $this->log(LogLevel::ERROR, $message, $context); |
|
71 | - } |
|
59 | + /** |
|
60 | + * Runtime errors that do not require immediate action but should typically |
|
61 | + * be logged and monitored. |
|
62 | + * |
|
63 | + * @param string $message |
|
64 | + * @param array $context |
|
65 | + * |
|
66 | + * @return void |
|
67 | + */ |
|
68 | + public function error($message, array $context = array()) |
|
69 | + { |
|
70 | + $this->log(LogLevel::ERROR, $message, $context); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Exceptional occurrences that are not errors. |
|
75 | - * |
|
76 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
77 | - * that are not necessarily wrong. |
|
78 | - * |
|
79 | - * @param string $message |
|
80 | - * @param array $context |
|
81 | - * |
|
82 | - * @return void |
|
83 | - */ |
|
84 | - public function warning($message, array $context = array()) |
|
85 | - { |
|
86 | - $this->log(LogLevel::WARNING, $message, $context); |
|
87 | - } |
|
73 | + /** |
|
74 | + * Exceptional occurrences that are not errors. |
|
75 | + * |
|
76 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
77 | + * that are not necessarily wrong. |
|
78 | + * |
|
79 | + * @param string $message |
|
80 | + * @param array $context |
|
81 | + * |
|
82 | + * @return void |
|
83 | + */ |
|
84 | + public function warning($message, array $context = array()) |
|
85 | + { |
|
86 | + $this->log(LogLevel::WARNING, $message, $context); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Normal but significant events. |
|
91 | - * |
|
92 | - * @param string $message |
|
93 | - * @param array $context |
|
94 | - * |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - public function notice($message, array $context = array()) |
|
98 | - { |
|
99 | - $this->log(LogLevel::NOTICE, $message, $context); |
|
100 | - } |
|
89 | + /** |
|
90 | + * Normal but significant events. |
|
91 | + * |
|
92 | + * @param string $message |
|
93 | + * @param array $context |
|
94 | + * |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + public function notice($message, array $context = array()) |
|
98 | + { |
|
99 | + $this->log(LogLevel::NOTICE, $message, $context); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Interesting events. |
|
104 | - * |
|
105 | - * Example: User logs in, SQL logs. |
|
106 | - * |
|
107 | - * @param string $message |
|
108 | - * @param array $context |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function info($message, array $context = array()) |
|
113 | - { |
|
114 | - $this->log(LogLevel::INFO, $message, $context); |
|
115 | - } |
|
102 | + /** |
|
103 | + * Interesting events. |
|
104 | + * |
|
105 | + * Example: User logs in, SQL logs. |
|
106 | + * |
|
107 | + * @param string $message |
|
108 | + * @param array $context |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function info($message, array $context = array()) |
|
113 | + { |
|
114 | + $this->log(LogLevel::INFO, $message, $context); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Detailed debug information. |
|
119 | - * |
|
120 | - * @param string $message |
|
121 | - * @param array $context |
|
122 | - * |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function debug($message, array $context = array()) |
|
126 | - { |
|
127 | - $this->log(LogLevel::DEBUG, $message, $context); |
|
128 | - } |
|
117 | + /** |
|
118 | + * Detailed debug information. |
|
119 | + * |
|
120 | + * @param string $message |
|
121 | + * @param array $context |
|
122 | + * |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function debug($message, array $context = array()) |
|
126 | + { |
|
127 | + $this->log(LogLevel::DEBUG, $message, $context); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Logs with an arbitrary level. |
|
132 | - * |
|
133 | - * @param mixed $level |
|
134 | - * @param string $message |
|
135 | - * @param array $context |
|
136 | - * |
|
137 | - * @return void |
|
138 | - */ |
|
139 | - abstract public function log($level, $message, array $context = array()); |
|
130 | + /** |
|
131 | + * Logs with an arbitrary level. |
|
132 | + * |
|
133 | + * @param mixed $level |
|
134 | + * @param string $message |
|
135 | + * @param array $context |
|
136 | + * |
|
137 | + * @return void |
|
138 | + */ |
|
139 | + abstract public function log($level, $message, array $context = array()); |
|
140 | 140 | } |
@@ -19,105 +19,105 @@ |
||
19 | 19 | */ |
20 | 20 | interface LoggerInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * System is unusable. |
|
24 | - * |
|
25 | - * @param string $message |
|
26 | - * @param array $context |
|
27 | - * |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - public function emergency($message, array $context = array()); |
|
22 | + /** |
|
23 | + * System is unusable. |
|
24 | + * |
|
25 | + * @param string $message |
|
26 | + * @param array $context |
|
27 | + * |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + public function emergency($message, array $context = array()); |
|
31 | 31 | |
32 | - /** |
|
33 | - * Action must be taken immediately. |
|
34 | - * |
|
35 | - * Example: Entire website down, database unavailable, etc. This should |
|
36 | - * trigger the SMS alerts and wake you up. |
|
37 | - * |
|
38 | - * @param string $message |
|
39 | - * @param array $context |
|
40 | - * |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function alert($message, array $context = array()); |
|
32 | + /** |
|
33 | + * Action must be taken immediately. |
|
34 | + * |
|
35 | + * Example: Entire website down, database unavailable, etc. This should |
|
36 | + * trigger the SMS alerts and wake you up. |
|
37 | + * |
|
38 | + * @param string $message |
|
39 | + * @param array $context |
|
40 | + * |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function alert($message, array $context = array()); |
|
44 | 44 | |
45 | - /** |
|
46 | - * Critical conditions. |
|
47 | - * |
|
48 | - * Example: Application component unavailable, unexpected exception. |
|
49 | - * |
|
50 | - * @param string $message |
|
51 | - * @param array $context |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function critical($message, array $context = array()); |
|
45 | + /** |
|
46 | + * Critical conditions. |
|
47 | + * |
|
48 | + * Example: Application component unavailable, unexpected exception. |
|
49 | + * |
|
50 | + * @param string $message |
|
51 | + * @param array $context |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function critical($message, array $context = array()); |
|
56 | 56 | |
57 | - /** |
|
58 | - * Runtime errors that do not require immediate action but should typically |
|
59 | - * be logged and monitored. |
|
60 | - * |
|
61 | - * @param string $message |
|
62 | - * @param array $context |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function error($message, array $context = array()); |
|
57 | + /** |
|
58 | + * Runtime errors that do not require immediate action but should typically |
|
59 | + * be logged and monitored. |
|
60 | + * |
|
61 | + * @param string $message |
|
62 | + * @param array $context |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function error($message, array $context = array()); |
|
67 | 67 | |
68 | - /** |
|
69 | - * Exceptional occurrences that are not errors. |
|
70 | - * |
|
71 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
72 | - * that are not necessarily wrong. |
|
73 | - * |
|
74 | - * @param string $message |
|
75 | - * @param array $context |
|
76 | - * |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - public function warning($message, array $context = array()); |
|
68 | + /** |
|
69 | + * Exceptional occurrences that are not errors. |
|
70 | + * |
|
71 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
72 | + * that are not necessarily wrong. |
|
73 | + * |
|
74 | + * @param string $message |
|
75 | + * @param array $context |
|
76 | + * |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + public function warning($message, array $context = array()); |
|
80 | 80 | |
81 | - /** |
|
82 | - * Normal but significant events. |
|
83 | - * |
|
84 | - * @param string $message |
|
85 | - * @param array $context |
|
86 | - * |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function notice($message, array $context = array()); |
|
81 | + /** |
|
82 | + * Normal but significant events. |
|
83 | + * |
|
84 | + * @param string $message |
|
85 | + * @param array $context |
|
86 | + * |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function notice($message, array $context = array()); |
|
90 | 90 | |
91 | - /** |
|
92 | - * Interesting events. |
|
93 | - * |
|
94 | - * Example: User logs in, SQL logs. |
|
95 | - * |
|
96 | - * @param string $message |
|
97 | - * @param array $context |
|
98 | - * |
|
99 | - * @return void |
|
100 | - */ |
|
101 | - public function info($message, array $context = array()); |
|
91 | + /** |
|
92 | + * Interesting events. |
|
93 | + * |
|
94 | + * Example: User logs in, SQL logs. |
|
95 | + * |
|
96 | + * @param string $message |
|
97 | + * @param array $context |
|
98 | + * |
|
99 | + * @return void |
|
100 | + */ |
|
101 | + public function info($message, array $context = array()); |
|
102 | 102 | |
103 | - /** |
|
104 | - * Detailed debug information. |
|
105 | - * |
|
106 | - * @param string $message |
|
107 | - * @param array $context |
|
108 | - * |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function debug($message, array $context = array()); |
|
103 | + /** |
|
104 | + * Detailed debug information. |
|
105 | + * |
|
106 | + * @param string $message |
|
107 | + * @param array $context |
|
108 | + * |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function debug($message, array $context = array()); |
|
112 | 112 | |
113 | - /** |
|
114 | - * Logs with an arbitrary level. |
|
115 | - * |
|
116 | - * @param mixed $level |
|
117 | - * @param string $message |
|
118 | - * @param array $context |
|
119 | - * |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function log($level, $message, array $context = array()); |
|
113 | + /** |
|
114 | + * Logs with an arbitrary level. |
|
115 | + * |
|
116 | + * @param mixed $level |
|
117 | + * @param string $message |
|
118 | + * @param array $context |
|
119 | + * |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function log($level, $message, array $context = array()); |
|
123 | 123 | } |
@@ -11,118 +11,118 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class AbstractLogger implements LoggerInterface |
13 | 13 | { |
14 | - /** |
|
15 | - * System is unusable. |
|
16 | - * |
|
17 | - * @param string $message |
|
18 | - * @param array $context |
|
19 | - * |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public function emergency($message, array $context = array()) |
|
23 | - { |
|
24 | - $this->log(LogLevel::EMERGENCY, $message, $context); |
|
25 | - } |
|
14 | + /** |
|
15 | + * System is unusable. |
|
16 | + * |
|
17 | + * @param string $message |
|
18 | + * @param array $context |
|
19 | + * |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public function emergency($message, array $context = array()) |
|
23 | + { |
|
24 | + $this->log(LogLevel::EMERGENCY, $message, $context); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Action must be taken immediately. |
|
29 | - * |
|
30 | - * Example: Entire website down, database unavailable, etc. This should |
|
31 | - * trigger the SMS alerts and wake you up. |
|
32 | - * |
|
33 | - * @param string $message |
|
34 | - * @param array $context |
|
35 | - * |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - public function alert($message, array $context = array()) |
|
39 | - { |
|
40 | - $this->log(LogLevel::ALERT, $message, $context); |
|
41 | - } |
|
27 | + /** |
|
28 | + * Action must be taken immediately. |
|
29 | + * |
|
30 | + * Example: Entire website down, database unavailable, etc. This should |
|
31 | + * trigger the SMS alerts and wake you up. |
|
32 | + * |
|
33 | + * @param string $message |
|
34 | + * @param array $context |
|
35 | + * |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + public function alert($message, array $context = array()) |
|
39 | + { |
|
40 | + $this->log(LogLevel::ALERT, $message, $context); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Critical conditions. |
|
45 | - * |
|
46 | - * Example: Application component unavailable, unexpected exception. |
|
47 | - * |
|
48 | - * @param string $message |
|
49 | - * @param array $context |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function critical($message, array $context = array()) |
|
54 | - { |
|
55 | - $this->log(LogLevel::CRITICAL, $message, $context); |
|
56 | - } |
|
43 | + /** |
|
44 | + * Critical conditions. |
|
45 | + * |
|
46 | + * Example: Application component unavailable, unexpected exception. |
|
47 | + * |
|
48 | + * @param string $message |
|
49 | + * @param array $context |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function critical($message, array $context = array()) |
|
54 | + { |
|
55 | + $this->log(LogLevel::CRITICAL, $message, $context); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Runtime errors that do not require immediate action but should typically |
|
60 | - * be logged and monitored. |
|
61 | - * |
|
62 | - * @param string $message |
|
63 | - * @param array $context |
|
64 | - * |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function error($message, array $context = array()) |
|
68 | - { |
|
69 | - $this->log(LogLevel::ERROR, $message, $context); |
|
70 | - } |
|
58 | + /** |
|
59 | + * Runtime errors that do not require immediate action but should typically |
|
60 | + * be logged and monitored. |
|
61 | + * |
|
62 | + * @param string $message |
|
63 | + * @param array $context |
|
64 | + * |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function error($message, array $context = array()) |
|
68 | + { |
|
69 | + $this->log(LogLevel::ERROR, $message, $context); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Exceptional occurrences that are not errors. |
|
74 | - * |
|
75 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
76 | - * that are not necessarily wrong. |
|
77 | - * |
|
78 | - * @param string $message |
|
79 | - * @param array $context |
|
80 | - * |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function warning($message, array $context = array()) |
|
84 | - { |
|
85 | - $this->log(LogLevel::WARNING, $message, $context); |
|
86 | - } |
|
72 | + /** |
|
73 | + * Exceptional occurrences that are not errors. |
|
74 | + * |
|
75 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
76 | + * that are not necessarily wrong. |
|
77 | + * |
|
78 | + * @param string $message |
|
79 | + * @param array $context |
|
80 | + * |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function warning($message, array $context = array()) |
|
84 | + { |
|
85 | + $this->log(LogLevel::WARNING, $message, $context); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Normal but significant events. |
|
90 | - * |
|
91 | - * @param string $message |
|
92 | - * @param array $context |
|
93 | - * |
|
94 | - * @return void |
|
95 | - */ |
|
96 | - public function notice($message, array $context = array()) |
|
97 | - { |
|
98 | - $this->log(LogLevel::NOTICE, $message, $context); |
|
99 | - } |
|
88 | + /** |
|
89 | + * Normal but significant events. |
|
90 | + * |
|
91 | + * @param string $message |
|
92 | + * @param array $context |
|
93 | + * |
|
94 | + * @return void |
|
95 | + */ |
|
96 | + public function notice($message, array $context = array()) |
|
97 | + { |
|
98 | + $this->log(LogLevel::NOTICE, $message, $context); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Interesting events. |
|
103 | - * |
|
104 | - * Example: User logs in, SQL logs. |
|
105 | - * |
|
106 | - * @param string $message |
|
107 | - * @param array $context |
|
108 | - * |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function info($message, array $context = array()) |
|
112 | - { |
|
113 | - $this->log(LogLevel::INFO, $message, $context); |
|
114 | - } |
|
101 | + /** |
|
102 | + * Interesting events. |
|
103 | + * |
|
104 | + * Example: User logs in, SQL logs. |
|
105 | + * |
|
106 | + * @param string $message |
|
107 | + * @param array $context |
|
108 | + * |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function info($message, array $context = array()) |
|
112 | + { |
|
113 | + $this->log(LogLevel::INFO, $message, $context); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Detailed debug information. |
|
118 | - * |
|
119 | - * @param string $message |
|
120 | - * @param array $context |
|
121 | - * |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - public function debug($message, array $context = array()) |
|
125 | - { |
|
126 | - $this->log(LogLevel::DEBUG, $message, $context); |
|
127 | - } |
|
116 | + /** |
|
117 | + * Detailed debug information. |
|
118 | + * |
|
119 | + * @param string $message |
|
120 | + * @param array $context |
|
121 | + * |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + public function debug($message, array $context = array()) |
|
125 | + { |
|
126 | + $this->log(LogLevel::DEBUG, $message, $context); |
|
127 | + } |
|
128 | 128 | } |
@@ -13,128 +13,128 @@ |
||
13 | 13 | */ |
14 | 14 | abstract class LoggerInterfaceTest extends \PHPUnit_Framework_TestCase |
15 | 15 | { |
16 | - /** |
|
17 | - * @return LoggerInterface |
|
18 | - */ |
|
19 | - abstract public function getLogger(); |
|
20 | - |
|
21 | - /** |
|
22 | - * This must return the log messages in order. |
|
23 | - * |
|
24 | - * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>". |
|
25 | - * |
|
26 | - * Example ->error('Foo') would yield "error Foo". |
|
27 | - * |
|
28 | - * @return string[] |
|
29 | - */ |
|
30 | - abstract public function getLogs(); |
|
31 | - |
|
32 | - public function testImplements() |
|
33 | - { |
|
34 | - $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * @dataProvider provideLevelsAndMessages |
|
39 | - */ |
|
40 | - public function testLogsAtAllLevels($level, $message) |
|
41 | - { |
|
42 | - $logger = $this->getLogger(); |
|
43 | - $logger->{$level}($message, array('user' => 'Bob')); |
|
44 | - $logger->log($level, $message, array('user' => 'Bob')); |
|
45 | - |
|
46 | - $expected = array( |
|
47 | - $level.' message of level '.$level.' with context: Bob', |
|
48 | - $level.' message of level '.$level.' with context: Bob', |
|
49 | - ); |
|
50 | - $this->assertEquals($expected, $this->getLogs()); |
|
51 | - } |
|
52 | - |
|
53 | - public function provideLevelsAndMessages() |
|
54 | - { |
|
55 | - return array( |
|
56 | - LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), |
|
57 | - LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), |
|
58 | - LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), |
|
59 | - LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), |
|
60 | - LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), |
|
61 | - LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), |
|
62 | - LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), |
|
63 | - LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), |
|
64 | - ); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @expectedException \Psr\Log\InvalidArgumentException |
|
69 | - */ |
|
70 | - public function testThrowsOnInvalidLevel() |
|
71 | - { |
|
72 | - $logger = $this->getLogger(); |
|
73 | - $logger->log('invalid level', 'Foo'); |
|
74 | - } |
|
75 | - |
|
76 | - public function testContextReplacement() |
|
77 | - { |
|
78 | - $logger = $this->getLogger(); |
|
79 | - $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); |
|
80 | - |
|
81 | - $expected = array('info {Message {nothing} Bob Bar a}'); |
|
82 | - $this->assertEquals($expected, $this->getLogs()); |
|
83 | - } |
|
84 | - |
|
85 | - public function testObjectCastToString() |
|
86 | - { |
|
87 | - if (method_exists($this, 'createPartialMock')) { |
|
88 | - $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); |
|
89 | - } else { |
|
90 | - $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); |
|
91 | - } |
|
92 | - $dummy->expects($this->once()) |
|
93 | - ->method('__toString') |
|
94 | - ->will($this->returnValue('DUMMY')); |
|
95 | - |
|
96 | - $this->getLogger()->warning($dummy); |
|
97 | - |
|
98 | - $expected = array('warning DUMMY'); |
|
99 | - $this->assertEquals($expected, $this->getLogs()); |
|
100 | - } |
|
101 | - |
|
102 | - public function testContextCanContainAnything() |
|
103 | - { |
|
104 | - $context = array( |
|
105 | - 'bool' => true, |
|
106 | - 'null' => null, |
|
107 | - 'string' => 'Foo', |
|
108 | - 'int' => 0, |
|
109 | - 'float' => 0.5, |
|
110 | - 'nested' => array('with object' => new DummyTest), |
|
111 | - 'object' => new \DateTime, |
|
112 | - 'resource' => fopen('php://memory', 'r'), |
|
113 | - ); |
|
114 | - |
|
115 | - $this->getLogger()->warning('Crazy context data', $context); |
|
116 | - |
|
117 | - $expected = array('warning Crazy context data'); |
|
118 | - $this->assertEquals($expected, $this->getLogs()); |
|
119 | - } |
|
120 | - |
|
121 | - public function testContextExceptionKeyCanBeExceptionOrOtherValues() |
|
122 | - { |
|
123 | - $logger = $this->getLogger(); |
|
124 | - $logger->warning('Random message', array('exception' => 'oops')); |
|
125 | - $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); |
|
126 | - |
|
127 | - $expected = array( |
|
128 | - 'warning Random message', |
|
129 | - 'critical Uncaught Exception!' |
|
130 | - ); |
|
131 | - $this->assertEquals($expected, $this->getLogs()); |
|
132 | - } |
|
16 | + /** |
|
17 | + * @return LoggerInterface |
|
18 | + */ |
|
19 | + abstract public function getLogger(); |
|
20 | + |
|
21 | + /** |
|
22 | + * This must return the log messages in order. |
|
23 | + * |
|
24 | + * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>". |
|
25 | + * |
|
26 | + * Example ->error('Foo') would yield "error Foo". |
|
27 | + * |
|
28 | + * @return string[] |
|
29 | + */ |
|
30 | + abstract public function getLogs(); |
|
31 | + |
|
32 | + public function testImplements() |
|
33 | + { |
|
34 | + $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * @dataProvider provideLevelsAndMessages |
|
39 | + */ |
|
40 | + public function testLogsAtAllLevels($level, $message) |
|
41 | + { |
|
42 | + $logger = $this->getLogger(); |
|
43 | + $logger->{$level}($message, array('user' => 'Bob')); |
|
44 | + $logger->log($level, $message, array('user' => 'Bob')); |
|
45 | + |
|
46 | + $expected = array( |
|
47 | + $level.' message of level '.$level.' with context: Bob', |
|
48 | + $level.' message of level '.$level.' with context: Bob', |
|
49 | + ); |
|
50 | + $this->assertEquals($expected, $this->getLogs()); |
|
51 | + } |
|
52 | + |
|
53 | + public function provideLevelsAndMessages() |
|
54 | + { |
|
55 | + return array( |
|
56 | + LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), |
|
57 | + LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), |
|
58 | + LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), |
|
59 | + LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), |
|
60 | + LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), |
|
61 | + LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), |
|
62 | + LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), |
|
63 | + LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), |
|
64 | + ); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @expectedException \Psr\Log\InvalidArgumentException |
|
69 | + */ |
|
70 | + public function testThrowsOnInvalidLevel() |
|
71 | + { |
|
72 | + $logger = $this->getLogger(); |
|
73 | + $logger->log('invalid level', 'Foo'); |
|
74 | + } |
|
75 | + |
|
76 | + public function testContextReplacement() |
|
77 | + { |
|
78 | + $logger = $this->getLogger(); |
|
79 | + $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); |
|
80 | + |
|
81 | + $expected = array('info {Message {nothing} Bob Bar a}'); |
|
82 | + $this->assertEquals($expected, $this->getLogs()); |
|
83 | + } |
|
84 | + |
|
85 | + public function testObjectCastToString() |
|
86 | + { |
|
87 | + if (method_exists($this, 'createPartialMock')) { |
|
88 | + $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); |
|
89 | + } else { |
|
90 | + $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); |
|
91 | + } |
|
92 | + $dummy->expects($this->once()) |
|
93 | + ->method('__toString') |
|
94 | + ->will($this->returnValue('DUMMY')); |
|
95 | + |
|
96 | + $this->getLogger()->warning($dummy); |
|
97 | + |
|
98 | + $expected = array('warning DUMMY'); |
|
99 | + $this->assertEquals($expected, $this->getLogs()); |
|
100 | + } |
|
101 | + |
|
102 | + public function testContextCanContainAnything() |
|
103 | + { |
|
104 | + $context = array( |
|
105 | + 'bool' => true, |
|
106 | + 'null' => null, |
|
107 | + 'string' => 'Foo', |
|
108 | + 'int' => 0, |
|
109 | + 'float' => 0.5, |
|
110 | + 'nested' => array('with object' => new DummyTest), |
|
111 | + 'object' => new \DateTime, |
|
112 | + 'resource' => fopen('php://memory', 'r'), |
|
113 | + ); |
|
114 | + |
|
115 | + $this->getLogger()->warning('Crazy context data', $context); |
|
116 | + |
|
117 | + $expected = array('warning Crazy context data'); |
|
118 | + $this->assertEquals($expected, $this->getLogs()); |
|
119 | + } |
|
120 | + |
|
121 | + public function testContextExceptionKeyCanBeExceptionOrOtherValues() |
|
122 | + { |
|
123 | + $logger = $this->getLogger(); |
|
124 | + $logger->warning('Random message', array('exception' => 'oops')); |
|
125 | + $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); |
|
126 | + |
|
127 | + $expected = array( |
|
128 | + 'warning Random message', |
|
129 | + 'critical Uncaught Exception!' |
|
130 | + ); |
|
131 | + $this->assertEquals($expected, $this->getLogs()); |
|
132 | + } |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | class DummyTest |
136 | 136 | { |
137 | - public function __toString() |
|
138 | - { |
|
139 | - } |
|
137 | + public function __toString() |
|
138 | + { |
|
139 | + } |
|
140 | 140 | } |
@@ -7,12 +7,12 @@ |
||
7 | 7 | */ |
8 | 8 | class LogLevel |
9 | 9 | { |
10 | - const EMERGENCY = 'emergency'; |
|
11 | - const ALERT = 'alert'; |
|
12 | - const CRITICAL = 'critical'; |
|
13 | - const ERROR = 'error'; |
|
14 | - const WARNING = 'warning'; |
|
15 | - const NOTICE = 'notice'; |
|
16 | - const INFO = 'info'; |
|
17 | - const DEBUG = 'debug'; |
|
10 | + const EMERGENCY = 'emergency'; |
|
11 | + const ALERT = 'alert'; |
|
12 | + const CRITICAL = 'critical'; |
|
13 | + const ERROR = 'error'; |
|
14 | + const WARNING = 'warning'; |
|
15 | + const NOTICE = 'notice'; |
|
16 | + const INFO = 'info'; |
|
17 | + const DEBUG = 'debug'; |
|
18 | 18 | } |
@@ -7,12 +7,12 @@ |
||
7 | 7 | */ |
8 | 8 | interface LoggerAwareInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Sets a logger instance on the object. |
|
12 | - * |
|
13 | - * @param LoggerInterface $logger |
|
14 | - * |
|
15 | - * @return void |
|
16 | - */ |
|
17 | - public function setLogger(LoggerInterface $logger); |
|
10 | + /** |
|
11 | + * Sets a logger instance on the object. |
|
12 | + * |
|
13 | + * @param LoggerInterface $logger |
|
14 | + * |
|
15 | + * @return void |
|
16 | + */ |
|
17 | + public function setLogger(LoggerInterface $logger); |
|
18 | 18 | } |
@@ -7,20 +7,20 @@ |
||
7 | 7 | */ |
8 | 8 | trait LoggerAwareTrait |
9 | 9 | { |
10 | - /** |
|
11 | - * The logger instance. |
|
12 | - * |
|
13 | - * @var LoggerInterface |
|
14 | - */ |
|
15 | - protected $logger; |
|
10 | + /** |
|
11 | + * The logger instance. |
|
12 | + * |
|
13 | + * @var LoggerInterface |
|
14 | + */ |
|
15 | + protected $logger; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Sets a logger. |
|
19 | - * |
|
20 | - * @param LoggerInterface $logger |
|
21 | - */ |
|
22 | - public function setLogger(LoggerInterface $logger) |
|
23 | - { |
|
24 | - $this->logger = $logger; |
|
25 | - } |
|
17 | + /** |
|
18 | + * Sets a logger. |
|
19 | + * |
|
20 | + * @param LoggerInterface $logger |
|
21 | + */ |
|
22 | + public function setLogger(LoggerInterface $logger) |
|
23 | + { |
|
24 | + $this->logger = $logger; |
|
25 | + } |
|
26 | 26 | } |