dolibarr 21.0.0-alpha
DolPhpCollector.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25use DebugBar\DataCollector\DataCollector;
26use DebugBar\DataCollector\Renderable;
27
34class PhpCollector extends DataCollector implements Renderable
35{
41 protected $name;
42
49 protected $messages = [];
50
56 public function __construct($name = 'Error handler')
57 {
58 $this->name = $name;
59 set_error_handler([$this, 'errorHandler'], E_ALL);
60 }
61
67 public function collect()
68 {
69 $messages = $this->getMessages();
70 return [
71 'count' => count($messages),
72 'messages' => $messages,
73 ];
74 }
75
81 public function getMessages()
82 {
83 $messages = $this->messages;
84
85 usort(
86 $messages,
92 static function ($itemA, $itemB) {
93 if ($itemA['time'] === $itemB['time']) {
94 return 0;
95 }
96 return $itemA['time'] < $itemB['time'] ? -1 : 1;
97 }
98 );
99
100 return $messages;
101 }
102
109 public function getWidgets()
110 {
111 $name = $this->getName();
112 return [
113 $name => [
114 'icon' => 'list',
115 'widget' => 'PhpDebugBar.Widgets.MessagesWidget',
116 'map' => "$name.messages",
117 'default' => '[]',
118 ],
119 "$name:badge" => [
120 'map' => "$name.count",
121 'default' => 'null',
122 ],
123 ];
124 }
125
131 public function getName()
132 {
133 return $this->name;
134 }
135
146 public function errorHandler($severity, $message, $fileName, $line)
147 {
148 for ($i = 0; $i < 15; $i++) {
149 if ($type = $severity & (1 << $i)) {
150 $label = $this->friendlyErrorType($type);
151 $this->messages[] = [
152 'message' => $message . ' (' . $fileName . ':' . $line . ')',
153 'message_html' => null,
154 'is_string' => true,
155 'label' => $label,
156 'time' => microtime(true),
157 ];
158 }
159 }
160 }
161
171 private function friendlyErrorType($type)
172 {
173 $errors = [
174 E_ERROR => 'ERROR',
175 E_WARNING => 'WARNING',
176 E_PARSE => 'PARSE',
177 E_NOTICE => 'NOTICE',
178 E_CORE_ERROR => 'CORE_ERROR',
179 E_CORE_WARNING => 'CORE_WARNING',
180 E_COMPILE_ERROR => 'COMPILE_ERROR',
181 E_COMPILE_WARNING => 'COMPILE_WARNING',
182 E_USER_ERROR => 'USER_ERROR',
183 E_USER_WARNING => 'USER_WARNING',
184 E_USER_NOTICE => 'USER_NOTICE',
185 E_STRICT => 'STRICT',
186 E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
187 E_DEPRECATED => 'DEPRECATED',
188 E_USER_DEPRECATED => 'USER_DEPRECATED',
189 ];
190
191 $result = '';
192 if (isset($errors[$type])) {
193 $result = $errors[$type];
194 }
195
196 return $result;
197 }
198}
Class PhpCollector.
collect()
Called by the DebugBar when data needs to be collected.
getWidgets()
Returns a hash where keys are control names and their values an array of options as defined in {.
__construct($name='Error handler')
PHPCollector constructor.
getMessages()
Returns a list of messages ordered by their timestamp.
getName()
Returns the unique name of the collector.
errorHandler($severity, $message, $fileName, $line)
Exception error handler.
friendlyErrorType($type)
Return error name from error code.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:142