1 | <?php |
||
16 | class PersistenceLogger |
||
17 | { |
||
18 | const NAME = 'PersistenceLogger'; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $count = 0; |
||
24 | |||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $logCalls = true; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $calls = array(); |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $unCachedHandlers = array(); |
||
39 | |||
40 | /** |
||
41 | * @param bool $logCalls Flag to enable logging of calls or not, should be disabled in prod |
||
42 | */ |
||
43 | public function __construct($logCalls = true) |
||
47 | |||
48 | /** |
||
49 | * Log SPI calls with method name and arguments until $maxLogCalls is reached. |
||
50 | * |
||
51 | * @param string $method |
||
52 | * @param array $arguments |
||
53 | */ |
||
54 | public function logCall($method, array $arguments = array()) |
||
55 | { |
||
56 | ++$this->count; |
||
57 | if ($this->logCalls) { |
||
58 | $this->calls[] = array( |
||
59 | 'method' => $method, |
||
60 | 'arguments' => $arguments, |
||
61 | 'trace' => $this->getSimpleCallTrace( |
||
62 | array_slice( |
||
63 | debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7), |
||
64 | 2 |
||
65 | ) |
||
66 | ), |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | private function getSimpleCallTrace(array $backtrace): array |
||
72 | { |
||
73 | $calls = []; |
||
74 | foreach ($backtrace as $call) { |
||
75 | if (!isset($call['class']) || strpos($call['class'], '\\') === false) { |
||
76 | // skip if class has no namspace (Symfony lazy proxy) or plain function |
||
77 | continue; |
||
78 | } |
||
79 | |||
80 | $calls[] = $call['class'] . $call['type'] . $call['function'] . '()'; |
||
81 | |||
82 | // Break out as soon as we have listed 1 class outside of kernel |
||
83 | if (strpos($call['class'], 'eZ\\Publish\\Core\\') !== 0) { |
||
84 | break; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | return $calls; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Log uncached handler being loaded. |
||
93 | * |
||
94 | * @param string $handler |
||
95 | */ |
||
96 | public function logUnCachedHandler($handler) |
||
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | public function getName() |
||
111 | |||
112 | /** |
||
113 | * @return int |
||
114 | */ |
||
115 | public function getCount() |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function isCallsLoggingEnabled() |
||
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getCalls() |
||
135 | |||
136 | /** |
||
137 | * @return array |
||
138 | */ |
||
139 | public function getLoadedUnCachedHandlers() |
||
143 | } |
||
144 |