debug.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. class debug
  3. {
  4. private static $logs = [];
  5. private static $startTime;
  6. private static $closeTime;
  7. private static function typeFile(string $_string){
  8. switch ($_string) {
  9. case 'maintenance':
  10. return ".lock-maintenance";
  11. break;
  12. case 'debug':
  13. return ".active-debug";
  14. break;
  15. default:
  16. return ".active-debug-".$_string;
  17. break;
  18. }
  19. }
  20. public static function addFile(string $_string = NULL)
  21. {
  22. if($_string != NULL){
  23. $myfile = fopen(DOCUMENT_ROOT . self::typeFile($_string), "w");
  24. fclose($myfile);
  25. }
  26. }
  27. public static function removeFile(string $_string = NULL)
  28. {
  29. if($_string != NULL){
  30. unlink(DOCUMENT_ROOT . self::typeFile($_string));
  31. }
  32. }
  33. public static function isFile(string $_string = NULL)
  34. {
  35. return file_exists(DOCUMENT_ROOT . self::typeFile($_string)) ? TRUE : FALSE;
  36. }
  37. public static function dump($var, $label = 'Dump', $echo = true)
  38. {
  39. // Start output buffering
  40. ob_start();
  41. echo "<pre class='debug-dump'>";
  42. echo "<strong>" . $label . ":</strong> ";
  43. // Convert array or object to string
  44. if (is_array($var)) {
  45. print_r($var);
  46. } elseif (is_object($var)) {
  47. echo self::objectToString($var);
  48. } else {
  49. var_dump($var);
  50. }
  51. echo "</pre>";
  52. // Get the contents of the buffer
  53. $output = ob_get_clean();
  54. // If echo is true, print the output
  55. if ($echo) {
  56. echo $output;
  57. } else {
  58. return $output;
  59. }
  60. }
  61. public static function objectToString($object, $_tab = NULL) {
  62. ob_start();
  63. $tab = "&nbsp;&nbsp;&nbsp;&nbsp;" . $_tab;
  64. echo "<span class='debug-console-capsule'>Object (" . get_class($object) . ")\n</span>";
  65. echo $_tab . "<span class='debug-console-capsule'>{\n</span>";
  66. foreach (get_object_vars($object) as $property => $value) {
  67. echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$property</span>] => </span>";
  68. if (is_array($value)) {
  69. echo self::arrayToString($value, $tab);
  70. } elseif (is_object($value)) {
  71. echo self::objectToString($value, $tab);
  72. } else {
  73. echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
  74. }
  75. }
  76. echo $_tab . "<span class='debug-console-capsule'>}\n</span>";
  77. return ob_get_clean();
  78. }
  79. public static function arrayToString($array, $_tab = NULL)
  80. {
  81. ob_start();
  82. $tab = "&nbsp;&nbsp;&nbsp;&nbsp;".$_tab;
  83. echo "<span class='debug-console-capsule'>Array\n</span>";
  84. echo $_tab . "<span class='debug-console-capsule'>(\n</span>";
  85. foreach ($array as $key => $value) {
  86. echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$key</span>] => </span>";
  87. if (is_array($value)) {
  88. echo self::arrayToString($value, $tab);
  89. } elseif (is_object($value)) {
  90. echo self::objectToString($value, $tab);
  91. } else {
  92. echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
  93. }
  94. }
  95. echo $_tab . "<span class='debug-console-capsule'>)\n</span>";
  96. return ob_get_clean();
  97. }
  98. private static function variableToString($var, $label)
  99. {
  100. ob_start();
  101. echo "$label: ";
  102. if (is_array($var) || is_object($var)) {
  103. print_r($var);
  104. } else {
  105. var_dump($var);
  106. }
  107. return ob_get_clean();
  108. }
  109. public static function getTraces(){
  110. $return = "Trace : ";
  111. // Obtenir la trace d'exécution
  112. $backtrace = debug_backtrace();
  113. $nb = count($backtrace)-1;
  114. for ($i=$nb; $i > 0; $i--) {
  115. $return .= ($i != 0) ? "[".$backtrace[$i]["function"]."] " : NULL;
  116. $return .= str_replace(DOCUMENT_ROOT, '', $backtrace[$i]["file"]).":".$backtrace[$i]["line"];
  117. $return .= ($i != 1) ? " >> " : NULL;
  118. }
  119. return $return;
  120. }
  121. public static function print_r(array $_array, int $_exit = NULL)
  122. {
  123. echo "<div>".debug::getTraces() . "</div>";
  124. echo "<pre>";
  125. print_r($_array);
  126. echo "</pre>";
  127. ($_exit != NULL) ? exit() : NULL;
  128. }
  129. public static function isHtml(string $_string) : bool
  130. {
  131. $stripped_string = strip_tags($_string);
  132. return $_string !== $stripped_string;
  133. }
  134. public static function generateHtmlContent($htmlContent)
  135. {
  136. $id = md5(microtime());
  137. return <<<HTML
  138. <!DOCTYPE html>
  139. <html>
  140. <head>
  141. <title>Exécution de HTML</title>
  142. <style>
  143. .isolated-iframe {
  144. width: 100%;
  145. height: 300px;
  146. border: none;
  147. }
  148. </style>
  149. </head>
  150. <body>
  151. <iframe id="isolatedIframe-$id" class="isolated-iframe"></iframe>
  152. <script>
  153. var iframe = document.getElementById('isolatedIframe-$id');
  154. var doc = iframe.document || iframe.contentDocument || iframe.contentWindow.document;
  155. doc.open();
  156. doc.write(`{$htmlContent}`);
  157. doc.close();
  158. </script>
  159. </body>
  160. </html>
  161. HTML;
  162. }
  163. public static function startTimer()
  164. {
  165. self::$startTime = microtime(true);
  166. }
  167. public static function endTimer()
  168. {
  169. self::$closeTime = microtime(true) - self::$startTime;
  170. }
  171. }