2
0

debug.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. class debug
  3. {
  4. private static $logs = [];
  5. private static $startTime;
  6. private static $closeTime;
  7. public static function addFileSubmit()
  8. {
  9. $myfile = fopen(DOCUMENT_ROOT . FILE_DEBUG_SUBMIT, "w");
  10. fclose($myfile);
  11. }
  12. public static function removeFileSubmit()
  13. {
  14. unlink(DOCUMENT_ROOT . FILE_DEBUG_SUBMIT);
  15. }
  16. public static function isSubmit()
  17. {
  18. return (file_exists(DOCUMENT_ROOT . FILE_DEBUG_SUBMIT)) ? TRUE : FALSE;
  19. }
  20. public static function includeDebug()
  21. {
  22. if (core::isDebug()) {
  23. echo '<link rel="stylesheet" href="css/debug.css">';
  24. }
  25. }
  26. public static function dump($var, $label = 'Dump', $echo = true)
  27. {
  28. // Start output buffering
  29. ob_start();
  30. echo "<pre class='debug-dump'>";
  31. echo "<strong>$label:</strong> ";
  32. // Convert array or object to string
  33. if (is_array($var)) {
  34. print_r($var);
  35. } elseif (is_object($var)) {
  36. echo self::objectToString($var);
  37. } else {
  38. var_dump($var);
  39. }
  40. echo "</pre>";
  41. // Get the contents of the buffer
  42. $output = ob_get_clean();
  43. // If echo is true, print the output
  44. if ($echo) {
  45. echo $output;
  46. } else {
  47. return $output;
  48. }
  49. }
  50. public static function objectToString($object, $_tab = NULL) {
  51. ob_start();
  52. $tab = "&nbsp;&nbsp;&nbsp;&nbsp;" . $_tab;
  53. echo "<span class='debug-console-capsule'>Object (" . get_class($object) . ")\n</span>";
  54. echo $_tab . "<span class='debug-console-capsule'>{\n</span>";
  55. foreach (get_object_vars($object) as $property => $value) {
  56. echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$property</span>] => </span>";
  57. if (is_array($value)) {
  58. echo self::arrayToString($value, $tab);
  59. } elseif (is_object($value)) {
  60. echo self::objectToString($value, $tab);
  61. } else {
  62. echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
  63. }
  64. }
  65. echo $_tab . "<span class='debug-console-capsule'>}\n</span>";
  66. return ob_get_clean();
  67. }
  68. public static function arrayToString($array, $_tab = NULL)
  69. {
  70. ob_start();
  71. $tab = "&nbsp;&nbsp;&nbsp;&nbsp;".$_tab;
  72. echo "<span class='debug-console-capsule'>Array\n</span>";
  73. echo $_tab . "<span class='debug-console-capsule'>(\n</span>";
  74. foreach ($array as $key => $value) {
  75. echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$key</span>] => </span>";
  76. if (is_array($value)) {
  77. echo self::arrayToString($value, $tab);
  78. } elseif (is_object($value)) {
  79. echo self::objectToString($value, $tab);
  80. } else {
  81. echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
  82. }
  83. }
  84. echo $_tab . "<span class='debug-console-capsule'>)\n</span>";
  85. return ob_get_clean();
  86. }
  87. private static function variableToString($var, $label)
  88. {
  89. ob_start();
  90. echo "$label: ";
  91. if (is_array($var) || is_object($var)) {
  92. print_r($var);
  93. } else {
  94. var_dump($var);
  95. }
  96. return ob_get_clean();
  97. }
  98. public static function getTraces(){
  99. $return = "Trace : ";
  100. // Obtenir la trace d'exécution
  101. $backtrace = debug_backtrace();
  102. $nb = count($backtrace)-1;
  103. for ($i=$nb; $i > 0; $i--) {
  104. $return .= ($i != 0) ? "[".$backtrace[$i]["function"]."] " : NULL;
  105. $return .= str_replace(DOCUMENT_ROOT, '', $backtrace[$i]["file"]).":".$backtrace[$i]["line"];
  106. $return .= ($i != 1) ? " >> " : NULL;
  107. }
  108. return $return;
  109. }
  110. public static function log($_message, $_mark = NULL)
  111. {
  112. $mark = "<div class='debug-head-console'>";
  113. $mark .= ($_mark != NULL) ? "<div style='font-weight: bold;'>". $_mark . "</div>" : NULL;
  114. $mark .= self::getTraces();
  115. $mark .= "</div>";
  116. if($_message != NULL) {
  117. if(is_array($_message)){
  118. self::$logs[] = $mark ."<div class='debug-console'>" . self::arrayToString($_message) . "</div>";
  119. } elseif(is_object($_message)){
  120. self::$logs[] = $mark ."<div class='debug-console'>" . self::objectToString($_message) . "</div>";
  121. } elseif($mark != NULL){
  122. self::$logs[] = $mark ."<div class='debug-console' style='color:blue;'>" . $_message . "</div>";
  123. } else {
  124. self::$logs[] = $_message;
  125. }
  126. }
  127. }
  128. public static function logSession($_message, $_mark = NULL)
  129. {
  130. $mark = "<div class='debug-head-console'>";
  131. $mark .= ($_mark != NULL) ? "<div style='font-weight: bold;'>". $_mark . "</div>" : NULL;
  132. $mark .= self::getTraces();
  133. $mark .= "</div>";
  134. if($_message != NULL) {
  135. if(is_array($_message)){
  136. self::setSession($mark ."<div class='debug-console'>" . self::arrayToString($_message) . "</div>");
  137. } elseif(is_object($_message)){
  138. self::setSession($mark ."<div class='debug-console'>" . self::objectToString($_message) . "</div>");
  139. } elseif($mark != NULL){
  140. self::setSession($mark ."<div class='debug-console' style='color:blue;'>" . $_message . "</div>");
  141. } else {
  142. self::setSession($_message);
  143. }
  144. }
  145. }
  146. public static function renderLogs()
  147. {
  148. //if (!empty(self::$logs)) {
  149. echo "<div id='debugger-logs'>";
  150. echo "<div class='debug-renderLogs-header'>";
  151. echo "PHP ". phpversion() . " | " . number_format(self::$closeTime, 4) . " secondes ";
  152. echo "</div>";
  153. echo "<div class=\"form-check form-switch\">
  154. <input class=\"form-check-input\" type=\"checkbox\" id=\"checkIsSubmit\" " . core::checkboxSelecter(self::isSubmit(), 0) . " >
  155. <label class=\"form-check-label\" for=\"checkIsSubmit\">Intercepter la validation</label>
  156. </div>";
  157. foreach (self::$logs as $log) {
  158. echo ($log != NULL) ? "<div class='debug-renderLogs-print'>".nl2br($log)."</div>" : NULL;
  159. }
  160. if(self::ifSession()){
  161. foreach (self::getSession() as $logSession) {
  162. echo ($logSession != NULL) ? "<div class='debug-renderLogs-print'>".nl2br($logSession)."</div>" : NULL;
  163. }
  164. }
  165. echo "</div>";
  166. echo "
  167. <script>
  168. $(document).ready(function() {
  169. $('#checkIsSubmit').on('change', function() {
  170. window.location.href = '/submit.php?from=parametres-debug-submit-activation&actif=' + $('#checkIsSubmit').prop('checked');
  171. });
  172. $('.toggle-logs').click(function() {
  173. $('#debugger-logs').slideToggle();
  174. });
  175. // Apply syntax highlighting to log entries
  176. $('.log-entry').each(function() {
  177. var html = $(this).html();
  178. html = html.replace(/(\\$\\w+)/g, '<span class=\"variable\">$1</span>');
  179. html = html.replace(/(\\bfunction\\b)/g, '<span class=\"function\">$1</span>');
  180. $(this).html(html);
  181. });
  182. });
  183. </script>
  184. ";
  185. //}
  186. }
  187. public static function init()
  188. {
  189. // Register shutdown function to render logs at the end of the script execution
  190. register_shutdown_function(function () {
  191. self::renderLogs();
  192. });
  193. }
  194. public static function startTimer()
  195. {
  196. self::$startTime = microtime(true);
  197. }
  198. public static function endTimer()
  199. {
  200. self::$closeTime = microtime(true) - self::$startTime;
  201. }
  202. public static function getBadge(string $_link,string $_label){
  203. return '<a href="'. $_link .'" target="_blank" class="badge debug-badge">'. $_label .'</a>';
  204. }
  205. public static function printEnvironnement(){
  206. echo (ENVIRONNEMENT != "PROD") ? " [" . ENVIRONNEMENT . "]" : NULL;
  207. }
  208. public static function getBadges(){
  209. $return = "";
  210. if(core::isMaintenance()){
  211. $return .= "<a href=\"/parametsres.html\"><span class=\"badge\" style=\"background-color:red; margin:5px;\">SITE EN MODE MAINTENANCE</span></a>";
  212. }
  213. if(core::isDebug()){
  214. $return .= "<a href=\"#\"><span class=\"badge toggle-logs\" style=\"background-color:orange; color:black; margin:5px;\">MODE DEBUG</span></a>";
  215. }
  216. if(self::isSubmit()){
  217. $return .= "<a href=\"#\"><span class=\"badge toggle-logs\" style=\"background-color:orange; color:black; margin:5px;\">MODE DEBUG SUBMIT</span></a>";
  218. }
  219. return $return;
  220. }
  221. public static function setSession($_data){
  222. $_SESSION["DEBUG"][] = $_data;
  223. }
  224. public static function getSession(){
  225. $return = $_SESSION["DEBUG"];
  226. self::resetSession();
  227. return $return;
  228. }
  229. public static function ifSession(){
  230. return empty($_SESSION["DEBUG"]) ? FALSE : TRUE;
  231. }
  232. public static function resetSession(){
  233. unset($_SESSION["DEBUG"]);
  234. }
  235. }