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