1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Extension; |
13
|
|
|
|
14
|
|
|
use Cake\Log\LogTrait; |
15
|
|
|
use Cake\Event\Event; |
16
|
|
|
use Cake\Event\EventListenerInterface; |
17
|
|
|
use CakeDC\Api\Service\Service; |
18
|
|
|
use CakeDC\Api\Service\Action\Action; |
19
|
|
|
|
20
|
|
|
class LogExtension extends Extension implements EventListenerInterface |
21
|
|
|
{ |
22
|
|
|
use LogTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Service |
26
|
|
|
*/ |
27
|
|
|
protected $_service; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Action |
31
|
|
|
*/ |
32
|
|
|
protected $_action; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $_timer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns a list of events this object is implementing. When the class is registered |
41
|
|
|
* in an event manager, each individual method will be associated with the respective event. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function implementedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'Service.beforeDispatch' => 'beforeProcess', |
49
|
|
|
'Service.afterDispatch' => 'afterProcess', |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* before process |
55
|
|
|
* |
56
|
|
|
* @param Event $event An Event instance. |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function beforeProcess(Event $event) |
60
|
|
|
{ |
61
|
|
|
$this->_service = $event->data['service']; |
62
|
|
|
$this->_timer = microtime(true); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* after process |
67
|
|
|
* |
68
|
|
|
* @param Event $event An Event instance. |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function afterProcess(Event $event) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$duration = round((microtime(true) - $this->_timer) * 1000, 0); |
74
|
|
|
$url = $this->_service->baseUrl(); |
75
|
|
|
$data = $this->_service->parser()->params(); |
76
|
|
|
$result = $this->_service->result()->toArray(); |
77
|
|
|
$log = [ |
78
|
|
|
'url' => $url, |
79
|
|
|
'method' => env('REQUEST_METHOD'), |
80
|
|
|
'duration' => $duration . 'ms', |
81
|
|
|
'input' => $data, |
82
|
|
|
'result' => json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), |
83
|
|
|
]; |
84
|
|
|
$this->log($log); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.