1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Yii\Debug\Event\ProxyMethodCallEvent; |
8
|
|
|
|
9
|
|
|
use function get_class; |
10
|
|
|
|
11
|
|
|
trait ProxyLogTrait |
12
|
|
|
{ |
13
|
|
|
private ContainerProxyConfig $config; |
14
|
|
|
|
15
|
6 |
|
protected function logProxy( |
16
|
|
|
string $service, |
17
|
|
|
object $instance, |
18
|
|
|
string $method, |
19
|
|
|
array $arguments, |
20
|
|
|
$result, |
21
|
|
|
float $timeStart |
22
|
|
|
): void { |
23
|
6 |
|
$error = $this->getCurrentError(); |
|
|
|
|
24
|
6 |
|
$this->processLogData($arguments, $result, $error); |
25
|
|
|
|
26
|
6 |
|
if ($this->config->getCollector() !== null) { |
27
|
6 |
|
$this->logToCollector($service, $instance, $method, $arguments, $result, $error, $timeStart); |
28
|
|
|
} |
29
|
|
|
|
30
|
6 |
|
if ($this->config->getDispatcher() !== null) { |
31
|
6 |
|
$this->logToEvent($service, $instance, $method, $arguments, $result, $error, $timeStart); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
6 |
|
private function processLogData(array &$arguments, &$result, ?object &$error): void |
36
|
|
|
{ |
37
|
6 |
|
if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ARGUMENTS)) { |
38
|
|
|
$arguments = null; |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_RESULT)) { |
42
|
6 |
|
$result = null; |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ERROR)) { |
46
|
6 |
|
$error = null; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
private function logToCollector( |
51
|
|
|
string $service, |
52
|
|
|
object $instance, |
53
|
|
|
string $method, |
54
|
|
|
?array $arguments, |
55
|
|
|
$result, |
56
|
|
|
?object $error, |
57
|
|
|
float $timeStart |
58
|
|
|
): void { |
59
|
6 |
|
$this->config->getCollector()?->collect( |
60
|
|
|
$service, |
61
|
6 |
|
get_class($instance), |
62
|
|
|
$method, |
63
|
|
|
$arguments, |
64
|
|
|
$result, |
65
|
6 |
|
$this->getCurrentResultStatus(), |
66
|
|
|
$error, |
67
|
|
|
$timeStart, |
68
|
6 |
|
microtime(true), |
|
|
|
|
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
private function logToEvent( |
73
|
|
|
string $service, |
74
|
|
|
object $instance, |
75
|
|
|
string $method, |
76
|
|
|
?array $arguments, |
77
|
|
|
$result, |
78
|
|
|
?object $error, |
79
|
|
|
float $timeStart |
80
|
|
|
): void { |
81
|
6 |
|
$this->config->getDispatcher()?->dispatch( |
82
|
6 |
|
new ProxyMethodCallEvent( |
83
|
|
|
$service, |
84
|
6 |
|
get_class($instance), |
85
|
|
|
$method, |
86
|
|
|
$arguments, |
87
|
|
|
$result, |
88
|
6 |
|
$this->getCurrentResultStatus(), |
89
|
|
|
$error, |
90
|
|
|
$timeStart, |
91
|
6 |
|
microtime(true), |
|
|
|
|
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
private function getCurrentResultStatus(): string |
97
|
|
|
{ |
98
|
6 |
|
if (!$this->hasCurrentError()) { |
|
|
|
|
99
|
6 |
|
return 'success'; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return 'failed'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|