| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- class debug
- {
- private static $logs = [];
- private static $startTime;
- private static $closeTime;
- private static function typeFile(string $_string){
- switch ($_string) {
- case 'maintenance':
- return ".lock-maintenance";
- break;
- case 'debug':
- return ".active-debug";
- break;
- default:
- return ".active-debug-".$_string;
- 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)
- {
- if($_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 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 isHtml(string $_string) : bool
- {
- $stripped_string = strip_tags($_string);
- return $_string !== $stripped_string;
- }
- public static function generateHtmlContent($htmlContent)
- {
- $id = md5(microtime());
- return <<<HTML
- <!DOCTYPE html>
- <html>
- <head>
- <title>Exécution de HTML</title>
- <style>
- .isolated-iframe {
- width: 100%;
- height: 300px;
- border: none;
- }
- </style>
- </head>
- <body>
- <iframe id="isolatedIframe-$id" class="isolated-iframe"></iframe>
- <script>
- var iframe = document.getElementById('isolatedIframe-$id');
- var doc = iframe.document || iframe.contentDocument || iframe.contentWindow.document;
- doc.open();
- doc.write(`{$htmlContent}`);
- doc.close();
- </script>
- </body>
- </html>
- HTML;
- }
- public static function startTimer()
- {
- self::$startTime = microtime(true);
- }
- public static function endTimer()
- {
- self::$closeTime = microtime(true) - self::$startTime;
- }
- }
|