DolPhpCollector.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. use DebugBar\DataCollector\DataCollector;
  3. use DebugBar\DataCollector\Renderable;
  4. /**
  5. * Class PhpCollector
  6. *
  7. * This class collects all PHP errors, notice, advices, trigger_error,...
  8. * Supports 15 different types included.
  9. */
  10. class PhpCollector extends DataCollector implements Renderable
  11. {
  12. /**
  13. * Collector name.
  14. *
  15. * @var string
  16. */
  17. protected $name;
  18. /**
  19. * List of messages. Each item includes:
  20. * 'message', 'message_html', 'is_string', 'label', 'time'.
  21. *
  22. * @var array
  23. */
  24. protected $messages = [];
  25. /**
  26. * PHPCollector constructor.
  27. *
  28. * @param string $name The name used by this collector widget.
  29. */
  30. public function __construct($name = 'Error handler')
  31. {
  32. $this->name = $name;
  33. set_error_handler([$this, 'errorHandler'], E_ALL);
  34. }
  35. /**
  36. * Called by the DebugBar when data needs to be collected.
  37. *
  38. * @return array Collected data.
  39. */
  40. public function collect()
  41. {
  42. $messages = $this->getMessages();
  43. return [
  44. 'count' => count($messages),
  45. 'messages' => $messages,
  46. ];
  47. }
  48. /**
  49. * Returns a list of messages ordered by their timestamp.
  50. *
  51. * @return array A list of messages ordered by time.
  52. */
  53. public function getMessages()
  54. {
  55. $messages = $this->messages;
  56. usort($messages, function ($itemA, $itemB) {
  57. if ($itemA['time'] === $itemB['time']) {
  58. return 0;
  59. }
  60. return $itemA['time'] < $itemB['time'] ? -1 : 1;
  61. });
  62. return $messages;
  63. }
  64. /**
  65. * Returns a hash where keys are control names and their values an array of options as defined in
  66. * {@see DebugBar\JavascriptRenderer::addControl()}
  67. *
  68. * @return array Needed details to render the widget.
  69. */
  70. public function getWidgets()
  71. {
  72. $name = $this->getName();
  73. return [
  74. $name => [
  75. 'icon' => 'list',
  76. 'widget' => 'PhpDebugBar.Widgets.MessagesWidget',
  77. 'map' => "$name.messages",
  78. 'default' => '[]',
  79. ],
  80. "$name:badge" => [
  81. 'map' => "$name.count",
  82. 'default' => 'null',
  83. ],
  84. ];
  85. }
  86. /**
  87. * Returns the unique name of the collector.
  88. *
  89. * @return string The widget name.
  90. */
  91. public function getName()
  92. {
  93. return $this->name;
  94. }
  95. /**
  96. * Exception error handler. Called from constructor with set_error_handler to add all details.
  97. *
  98. * @param int $severity Error type.
  99. * @param string $message Message of error.
  100. * @param string $fileName File where error is generated.
  101. * @param int $line Line number where error is generated.
  102. *
  103. * @return void
  104. */
  105. public function errorHandler($severity, $message, $fileName, $line)
  106. {
  107. for ($i = 0; $i < 15; $i++) {
  108. if ($type = $severity & (2 ** $i)) {
  109. $label = $this->friendlyErrorType($type);
  110. $this->messages[] = [
  111. 'message' => $message . ' (' . $fileName . ':' . $line . ')',
  112. 'message_html' => null,
  113. 'is_string' => true,
  114. 'label' => $label,
  115. 'time' => microtime(true),
  116. ];
  117. }
  118. }
  119. }
  120. /**
  121. * Return error name from error code.
  122. *
  123. * @info http://php.net/manual/es/errorfunc.constants.php
  124. *
  125. * @param int $type Error code.
  126. *
  127. * @return string Error name.
  128. */
  129. private function friendlyErrorType($type)
  130. {
  131. $errors = [
  132. E_ERROR => 'ERROR',
  133. E_WARNING => 'WARNING',
  134. E_PARSE => 'PARSE',
  135. E_NOTICE => 'NOTICE',
  136. E_CORE_ERROR => 'CORE_ERROR',
  137. E_CORE_WARNING => 'CORE_WARNING',
  138. E_COMPILE_ERROR => 'COMPILE_ERROR',
  139. E_COMPILE_WARNING => 'COMPILE_WARNING',
  140. E_USER_ERROR => 'USER_ERROR',
  141. E_USER_WARNING => 'USER_WARNING',
  142. E_USER_NOTICE => 'USER_NOTICE',
  143. E_STRICT => 'STRICT',
  144. E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
  145. E_DEPRECATED => 'DEPRECATED',
  146. E_USER_DEPRECATED => 'USER_DEPRECATED',
  147. ];
  148. $result = '';
  149. if (isset($errors[$type])) {
  150. $result = $errors[$type];
  151. }
  152. return $result;
  153. }
  154. }