dolibarr 21.0.0-beta
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
50 protected $messages = [];
51
57 public function __construct($name = 'Error handler')
58 {
59 $this->name = $name;
60 set_error_handler([$this, 'errorHandler'], E_ALL);
61 }
62
68 public function collect()
69 {
70 $messages = $this->getMessages();
71 return [
72 'count' => count($messages),
73 'messages' => $messages,
74 ];
75 }
76
82 public function getMessages()
83 {
84 $messages = $this->messages;
85
86 usort(
87 $messages,
93 static function ($itemA, $itemB) {
94 if ($itemA['time'] === $itemB['time']) {
95 return 0;
96 }
97 return $itemA['time'] < $itemB['time'] ? -1 : 1;
98 }
99 );
100
101 return $messages;
102 }
103
110 public function getWidgets()
111 {
112 $name = $this->getName();
113 return [
114 $name => [
115 'icon' => 'list',
116 'widget' => 'PhpDebugBar.Widgets.MessagesWidget',
117 'map' => "$name.messages",
118 'default' => '[]',
119 ],
120 "$name:badge" => [
121 'map' => "$name.count",
122 'default' => 'null',
123 ],
124 ];
125 }
126
132 public function getName()
133 {
134 return $this->name;
135 }
136
147 public function errorHandler($severity, $message, $fileName, $line)
148 {
149 for ($i = 0; $i < 15; $i++) {
150 if ($type = $severity & (1 << $i)) {
151 $label = $this->friendlyErrorType($type);
152 $this->messages[] = [
153 'message' => $message . ' (' . $fileName . ':' . $line . ')',
154 'message_html' => null,
155 'is_string' => true,
156 'label' => $label,
157 'time' => microtime(true),
158 ];
159 }
160 }
161 }
162
172 private function friendlyErrorType($type)
173 {
174 $errors = [
175 E_ERROR => 'ERROR',
176 E_WARNING => 'WARNING',
177 E_PARSE => 'PARSE',
178 E_NOTICE => 'NOTICE',
179 E_CORE_ERROR => 'CORE_ERROR',
180 E_CORE_WARNING => 'CORE_WARNING',
181 E_COMPILE_ERROR => 'COMPILE_ERROR',
182 E_COMPILE_WARNING => 'COMPILE_WARNING',
183 E_USER_ERROR => 'USER_ERROR',
184 E_USER_WARNING => 'USER_WARNING',
185 E_USER_NOTICE => 'USER_NOTICE',
186 E_STRICT => 'STRICT',
187 E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
188 E_DEPRECATED => 'DEPRECATED',
189 E_USER_DEPRECATED => 'USER_DEPRECATED',
190 ];
191
192 $result = '';
193 if (isset($errors[$type])) {
194 $result = $errors[$type];
195 }
196
197 return $result;
198 }
199}
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:152