dolibarr  17.0.4
DolPhpCollector.php
1 <?php
2 
3 use DebugBar\DataCollector\DataCollector;
4 use DebugBar\DataCollector\Renderable;
5 
12 class PhpCollector extends DataCollector implements Renderable
13 {
19  protected $name;
20 
27  protected $messages = [];
28 
34  public function __construct($name = 'Error handler')
35  {
36  $this->name = $name;
37  set_error_handler([$this, 'errorHandler'], E_ALL);
38  }
39 
45  public function collect()
46  {
47  $messages = $this->getMessages();
48  return [
49  'count' => count($messages),
50  'messages' => $messages,
51  ];
52  }
53 
59  public function getMessages()
60  {
61  $messages = $this->messages;
62 
63  usort($messages, function ($itemA, $itemB) {
64  if ($itemA['time'] === $itemB['time']) {
65  return 0;
66  }
67  return $itemA['time'] < $itemB['time'] ? -1 : 1;
68  });
69 
70  return $messages;
71  }
72 
79  public function getWidgets()
80  {
81  $name = $this->getName();
82  return [
83  $name => [
84  'icon' => 'list',
85  'widget' => 'PhpDebugBar.Widgets.MessagesWidget',
86  'map' => "$name.messages",
87  'default' => '[]',
88  ],
89  "$name:badge" => [
90  'map' => "$name.count",
91  'default' => 'null',
92  ],
93  ];
94  }
95 
101  public function getName()
102  {
103  return $this->name;
104  }
105 
116  public function errorHandler($severity, $message, $fileName, $line)
117  {
118  for ($i = 0; $i < 15; $i++) {
119  if ($type = $severity & (2 ** $i)) {
120  $label = $this->friendlyErrorType($type);
121  $this->messages[] = [
122  'message' => $message . ' (' . $fileName . ':' . $line . ')',
123  'message_html' => null,
124  'is_string' => true,
125  'label' => $label,
126  'time' => microtime(true),
127  ];
128  }
129  }
130  }
131 
141  private function friendlyErrorType($type)
142  {
143  $errors = [
144  E_ERROR => 'ERROR',
145  E_WARNING => 'WARNING',
146  E_PARSE => 'PARSE',
147  E_NOTICE => 'NOTICE',
148  E_CORE_ERROR => 'CORE_ERROR',
149  E_CORE_WARNING => 'CORE_WARNING',
150  E_COMPILE_ERROR => 'COMPILE_ERROR',
151  E_COMPILE_WARNING => 'COMPILE_WARNING',
152  E_USER_ERROR => 'USER_ERROR',
153  E_USER_WARNING => 'USER_WARNING',
154  E_USER_NOTICE => 'USER_NOTICE',
155  E_STRICT => 'STRICT',
156  E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
157  E_DEPRECATED => 'DEPRECATED',
158  E_USER_DEPRECATED => 'USER_DEPRECATED',
159  ];
160 
161  $result = '';
162  if (isset($errors[$type])) {
163  $result = $errors[$type];
164  }
165 
166  return $result;
167  }
168 }
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:122