dolibarr  19.0.0-dev
DolPhpCollector.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
24 use DebugBar\DataCollector\DataCollector;
25 use DebugBar\DataCollector\Renderable;
26 
33 class PhpCollector extends DataCollector implements Renderable
34 {
40  protected $name;
41 
48  protected $messages = [];
49 
55  public function __construct($name = 'Error handler')
56  {
57  $this->name = $name;
58  set_error_handler([$this, 'errorHandler'], E_ALL);
59  }
60 
66  public function collect()
67  {
68  $messages = $this->getMessages();
69  return [
70  'count' => count($messages),
71  'messages' => $messages,
72  ];
73  }
74 
80  public function getMessages()
81  {
82  $messages = $this->messages;
83 
84  usort($messages, function ($itemA, $itemB) {
85  if ($itemA['time'] === $itemB['time']) {
86  return 0;
87  }
88  return $itemA['time'] < $itemB['time'] ? -1 : 1;
89  });
90 
91  return $messages;
92  }
93 
100  public function getWidgets()
101  {
102  $name = $this->getName();
103  return [
104  $name => [
105  'icon' => 'list',
106  'widget' => 'PhpDebugBar.Widgets.MessagesWidget',
107  'map' => "$name.messages",
108  'default' => '[]',
109  ],
110  "$name:badge" => [
111  'map' => "$name.count",
112  'default' => 'null',
113  ],
114  ];
115  }
116 
122  public function getName()
123  {
124  return $this->name;
125  }
126 
137  public function errorHandler($severity, $message, $fileName, $line)
138  {
139  for ($i = 0; $i < 15; $i++) {
140  if ($type = $severity & (2 ** $i)) {
141  $label = $this->friendlyErrorType($type);
142  $this->messages[] = [
143  'message' => $message . ' (' . $fileName . ':' . $line . ')',
144  'message_html' => null,
145  'is_string' => true,
146  'label' => $label,
147  'time' => microtime(true),
148  ];
149  }
150  }
151  }
152 
162  private function friendlyErrorType($type)
163  {
164  $errors = [
165  E_ERROR => 'ERROR',
166  E_WARNING => 'WARNING',
167  E_PARSE => 'PARSE',
168  E_NOTICE => 'NOTICE',
169  E_CORE_ERROR => 'CORE_ERROR',
170  E_CORE_WARNING => 'CORE_WARNING',
171  E_COMPILE_ERROR => 'COMPILE_ERROR',
172  E_COMPILE_WARNING => 'COMPILE_WARNING',
173  E_USER_ERROR => 'USER_ERROR',
174  E_USER_WARNING => 'USER_WARNING',
175  E_USER_NOTICE => 'USER_NOTICE',
176  E_STRICT => 'STRICT',
177  E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
178  E_DEPRECATED => 'DEPRECATED',
179  E_USER_DEPRECATED => 'USER_DEPRECATED',
180  ];
181 
182  $result = '';
183  if (isset($errors[$type])) {
184  $result = $errors[$type];
185  }
186 
187  return $result;
188  }
189 }
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:123