| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- class debug
- {
- private static $logs = [];
- private static $startTime;
- private static $closeTime;
- private static function typeFile(string $_string){
- switch ($_string) {
- case 'debug':
- return FILE_DEBUG;
- break;
- case 'maintenance':
- return FILE_MAINTENANCE;
- break;
- case 'submit':
- return FILE_DEBUG_SUBMIT;
- break;
- case 'sql':
- return FILE_DEBUG_SQL;
- break;
- default:
- return NULL;
- break;
- }
- }
-
- public static function addFile(string $_string = NULL)
- {
- if($_string != NULL){
- $myfile = fopen(DOCUMENT_ROOT . self::typeFile($_string), "w");
- fclose($myfile);
- }
- }
- public static function removeFile(string $_string = NULL)
- {
- unlink(DOCUMENT_ROOT . self::typeFile($_string));
- }
- public static function isFile(string $_string = NULL)
- {
- return (file_exists(DOCUMENT_ROOT . self::typeFile($_string))) ? TRUE : FALSE;
- }
- public static function isSubmit()
- {
- return (file_exists(DOCUMENT_ROOT . FILE_DEBUG_SUBMIT)) ? TRUE : FALSE;
- }
-
- public static function includeDebug()
- {
- if (debug::isFile("debug")) {
- echo '<link rel="stylesheet" href="css/debug.css">';
- }
- }
- public static function dump($var, $label = 'Dump', $echo = true)
- {
- // Start output buffering
- ob_start();
- echo "<pre class='debug-dump'>";
- echo "<strong>$label:</strong> ";
- // Convert array or object to string
- if (is_array($var)) {
- print_r($var);
- } elseif (is_object($var)) {
- echo self::objectToString($var);
- } else {
- var_dump($var);
- }
- echo "</pre>";
- // Get the contents of the buffer
- $output = ob_get_clean();
- // If echo is true, print the output
- if ($echo) {
- echo $output;
- } else {
- return $output;
- }
- }
- public static function objectToString($object, $_tab = NULL) {
- ob_start();
- $tab = " " . $_tab;
-
- echo "<span class='debug-console-capsule'>Object (" . get_class($object) . ")\n</span>";
- echo $_tab . "<span class='debug-console-capsule'>{\n</span>";
-
- foreach (get_object_vars($object) as $property => $value) {
- echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$property</span>] => </span>";
- if (is_array($value)) {
- echo self::arrayToString($value, $tab);
- } elseif (is_object($value)) {
- echo self::objectToString($value, $tab);
- } else {
- echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
- }
- }
-
- echo $_tab . "<span class='debug-console-capsule'>}\n</span>";
-
- return ob_get_clean();
- }
- public static function arrayToString($array, $_tab = NULL)
- {
- ob_start();
- $tab = " ".$_tab;
-
- echo "<span class='debug-console-capsule'>Array\n</span>";
- echo $_tab . "<span class='debug-console-capsule'>(\n</span>";
-
- foreach ($array as $key => $value) {
- echo $tab . "<span class='debug-console-capsule'>[<span class='debug-console-key'>$key</span>] => </span>";
- if (is_array($value)) {
- echo self::arrayToString($value, $tab);
- } elseif (is_object($value)) {
- echo self::objectToString($value, $tab);
- } else {
- echo "<span class='debug-console-value'>" . var_export($value, true) . "</span>\n";
- }
- }
-
- echo $_tab . "<span class='debug-console-capsule'>)\n</span>";
-
- return ob_get_clean();
- }
- private static function variableToString($var, $label)
- {
- ob_start();
- echo "$label: ";
- if (is_array($var) || is_object($var)) {
- print_r($var);
- } else {
- var_dump($var);
- }
- return ob_get_clean();
- }
- public static function getTraces(){
- $return = "Trace : ";
-
- // Obtenir la trace d'exécution
- $backtrace = debug_backtrace();
- $nb = count($backtrace)-1;
- for ($i=$nb; $i > 0; $i--) {
- $return .= ($i != 0) ? "[".$backtrace[$i]["function"]."] " : NULL;
- $return .= str_replace(DOCUMENT_ROOT, '', $backtrace[$i]["file"]).":".$backtrace[$i]["line"];
- $return .= ($i != 1) ? " >> " : NULL;
- }
- return $return;
- }
- public static function print_r(array $_array, int $_exit = NULL)
- {
- echo "<div>".debug::getTraces() . "</div>";
- echo "<pre>";
- print_r($_array);
- echo "</pre>";
- ($_exit != NULL) ? exit() : NULL;
- }
- public static function log($_message, $_mark = NULL)
- {
- $mark = "<div class='debug-head-console'>";
- $mark .= ($_mark != NULL) ? "<div style='font-weight: bold;'>". $_mark . "</div>" : NULL;
- $mark .= self::getTraces();
- $mark .= "</div>";
- if($_message != NULL) {
- if(is_array($_message)){
- self::$logs[] = $mark ."<div class='debug-console'>" . self::arrayToString($_message) . "</div>";
- } elseif(is_object($_message)){
- self::$logs[] = $mark ."<div class='debug-console'>" . self::objectToString($_message) . "</div>";
- } elseif($mark != NULL){
- self::$logs[] = $mark ."<div class='debug-console' style='color:blue;'>" . $_message . "</div>";
- } else {
- self::$logs[] = $_message;
- }
- }
- }
- public static function logSession($_message, $_mark = NULL)
- {
- $mark = "<div class='debug-head-console'>";
- $mark .= ($_mark != NULL) ? "<div style='font-weight: bold;'>". $_mark . "</div>" : NULL;
- $mark .= self::getTraces();
- $mark .= "</div>";
- if($_message != NULL) {
- if(is_array($_message)){
- self::setSession($mark ."<div class='debug-console'>" . self::arrayToString($_message) . "</div>");
- } elseif(is_object($_message)){
- self::setSession($mark ."<div class='debug-console'>" . self::objectToString($_message) . "</div>");
- } elseif($mark != NULL){
- self::setSession($mark ."<div class='debug-console' style='color:blue;'>" . $_message . "</div>");
- } else {
- self::setSession($_message);
- }
- }
- }
- public static function renderLogs()
- {
- echo "<div id='debugger-logs'>";
- echo "<div class='debug-renderLogs-header'>";
- echo "PHP ". phpversion() . " | MYSQL " . db::version() . " | " . number_format(self::$closeTime, 4) . " secondes ";
- echo "</div>";
- echo "<div class=\"form-check form-switch\" style=\"margin-top: -30px;\">
- <div>
- <input class=\"form-check-input\" type=\"checkbox\" id=\"checkIsSubmit\" " . core::checkboxSelecter(self::isFile("submit"), 0) . " >
- <label class=\"form-check-label\" for=\"checkIsSubmit\">Intercepter les Submit</label>
- </div>
- <div>
- <input class=\"form-check-input\" type=\"checkbox\" id=\"checkIsSql\" " . core::checkboxSelecter(self::isFile("sql"), 0) . " >
- <label class=\"form-check-label\" for=\"checkIsSql\">Intercepter les requêtes SQL</label>
- </div>
- </div>";
- foreach (self::$logs as $log) {
- echo ($log != NULL) ? "<div class='debug-renderLogs-print'>".nl2br($log)."</div>" : NULL;
- }
- if(self::ifSession()){
- foreach (self::getSession() as $logSession) {
- echo ($logSession != NULL) ? "<div class='debug-renderLogs-print'>".nl2br($logSession)."</div>" : NULL;
- }
- }
- echo "</div>";
- get::javascript("debug");
- }
- public static function init()
- {
- // Register shutdown function to render logs at the end of the script execution
- register_shutdown_function(function () {
- self::renderLogs();
- });
- }
- public static function startTimer()
- {
- self::$startTime = microtime(true);
- }
- public static function endTimer()
- {
- self::$closeTime = microtime(true) - self::$startTime;
- }
- public static function getBadge(string $_link,string $_label){
- return '<a href="'. $_link .'" target="_blank" class="badge debug-badge">'. $_label .'</a>';
- }
- public static function printEnvironnement(){
- echo (ENVIRONNEMENT != "PROD") ? " [" . ENVIRONNEMENT . "]" : NULL;
- }
- private static function buildBadge(array $_data){
- $color = empty($_data["color"]) ? "balck" : $_data["color"];
- $backgroundColor = empty($_data["background-color"]) ? "orange" : $_data["background-color"];
- $class = empty($_data["class"]) ? NULL : $_data["class"];
- $link = empty($_data["link"]) ? "#" : $_data["link"];
- $txt = empty($_data["txt"]) ? NULL : $_data["txt"];
- return "<a href=\"/" . $link . "\"><span class=\"badge " . $class . "\" style=\"background-color:" . $backgroundColor . "; color:" . $color . "; padding: 3px 5px 2px 5px; margin-right: 5px;\">" . $txt . "</span></a>";
- }
- public static function getBadges(){
- $return = "";
- if(debug::isFile("maintenance")){
- $return .= self::buildBadge([
- "link" => "/parametres.html",
- "background-color" => "red",
- "color" => "white",
- "txt" => "SITE EN MODE MAINTENANCE"
- ]);
- }
- if(debug::isFile("debug")){
- $return .= self::buildBadge([
- "class" => "toggle-logs",
- "link" => "#",
- "background-color" => "orangered",
- "color" => "white",
- "txt" => "MODE DEBUG"
- ]);
- }
- if(self::isFile("sql")){
- $return .= self::buildBadge([
- "class" => "toggle-logs",
- "link" => "#",
- "background-color" => "orange",
- "color" => "black",
- "txt" => "DEBUG SQL"
- ]);
- }
- if(self::isFile("submit")){
- $return .= self::buildBadge([
- "class" => "toggle-logs",
- "link" => "#",
- "background-color" => "orange",
- "color" => "black",
- "txt" => "DEBUG SUBMIT"
- ]);
- }
- return $return;
- }
- public static function setSession($_data){
- $_SESSION["DEBUG"][] = $_data;
- }
- public static function getSession(){
- $return = $_SESSION["DEBUG"];
- self::resetSession();
- return $return;
- }
- public static function ifSession(){
- return empty($_SESSION["DEBUG"]) ? FALSE : TRUE;
- }
- public static function resetSession(){
- unset($_SESSION["DEBUG"]);
- }
- }
|