Advertisement
ArcaniSGK507

Untitled

Mar 17th, 2025
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.69 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. /**
  6.  * Last Hammer Framework 2.0
  7.  * PHP Version 8.3 (Required).
  8.  *
  9.  * @see https://github.com/arcanisgk/LH-Framework
  10.  *
  11.  * @author    Walter Nuñez (arcanisgk/founder) <[email protected]>
  12.  * @copyright 2017 - 2024
  13.  * @license   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14.  * @note      This program is distributed in the hope that it will be useful
  15.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16.  * or FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18.  
  19. namespace Asset\Framework\ToolBox;
  20.  
  21. use ReflectionNamedType;
  22. use ReflectionObject;
  23. use stdClass;
  24.  
  25. /**
  26.  * Class that handles:
  27.  *
  28.  * @package Asset\Framework\ToolBox;
  29.  */
  30. class Dumper
  31. {
  32.  
  33.     /**
  34.      * @var Dumper|null Singleton instance of the class: Dumper.
  35.      */
  36.     private static ?self $instance = null;
  37.  
  38.     /**
  39.      * Dumper constructor.
  40.      */
  41.     public function __construct()
  42.     {
  43.  
  44.     }
  45.  
  46.     /**
  47.      * Entry Point to Dumper (dump) output of data
  48.      * @param array $param
  49.      * @return string|null
  50.      */
  51.     public static function dump(array $param): ?string
  52.     {
  53.         $stuff = (IS_CLI) ?
  54.             [
  55.                 'salE' => '',
  56.                 'salC' => '',
  57.                 'nl'   => PHP_EOL,
  58.             ] : [
  59.                 'salE' => '<pre>',
  60.                 'salC' => '</pre>',
  61.                 'nl'   => '<br>',
  62.             ];
  63.  
  64.         $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  65.         $caller    = $backtrace[1];
  66.         $fileInfo  = "[{$caller['file']}:{$caller['line']}]";
  67.         $output    = 'Output Variable: '.$stuff['nl'].$fileInfo.$stuff['nl'];
  68.  
  69.         foreach ($param['data'] as $arg) {
  70.             $output .= self::format($arg).$stuff['nl'];
  71.         }
  72.  
  73.         if (IS_CLI) {
  74.  
  75.             $drawBox = DrawBoxCLI::getInstance();
  76.  
  77.             echo $drawBox->drawBoxes($output, 2, 0, true, 0, 4);
  78.  
  79.         } else {
  80.             echo $stuff['salE'].$output.$stuff['salC'];
  81.         }
  82.  
  83.         return null;
  84.     }
  85.  
  86.     /**
  87.      * Format the variable for output.
  88.      *
  89.      * @param mixed $var
  90.      * @return string
  91.      */
  92.     private static function format(mixed $var): string
  93.     {
  94.         return self::export($var);
  95.     }
  96.  
  97.     /**
  98.      * Recursively export the variable with custom formatting.
  99.      *
  100.      * @param mixed $var
  101.      * @param int $indentLevel
  102.      * @return string
  103.      */
  104.     private static function export(mixed $var, int $indentLevel = 0): string
  105.     {
  106.         $indent = str_repeat('  ', $indentLevel);
  107.  
  108.         switch (gettype($var)) {
  109.             case 'NULL':
  110.                 return self::highlight('null', 'null');
  111.             case 'boolean':
  112.                 return self::highlight($var ? 'true' : 'false', 'boolean');
  113.             case 'double':
  114.             case 'integer':
  115.                 return self::highlight((string)$var, 'number');
  116.             case 'string':
  117.                 return self::highlight('"'.addslashes($var).'"', 'string');
  118.             case 'array':
  119.                 if (empty($var)) {
  120.                     return "[]";
  121.                 } else {
  122.                     $output = "[\n";
  123.                     foreach ($var as $key => $value) {
  124.                         $output .= $indent.'  '.self::export($key).' => '.self::export($value, $indentLevel + 1).",\n";
  125.                     }
  126.                     $output .= $indent.']';
  127.  
  128.                     return $output;
  129.                 }
  130.             case 'object':
  131.                 if ($indentLevel > 5) {
  132.                     return get_class($var).' {...}';
  133.                 }
  134.  
  135.                 if (method_exists($var, '__debugInfo')) {
  136.                     $debugInfo = $var->__debugInfo();
  137.                     if (!is_array($debugInfo) && !is_object($debugInfo)) {
  138.                         return get_class($var)." { /* Invalid __debugInfo output */ }";
  139.                     }
  140.  
  141.                     $output = get_class($var)." {\n";
  142.                     foreach ((array)$debugInfo as $name => $value) {
  143.                         $output .= $indent.'  '.
  144.                             self::highlight($name, 'property').': '.
  145.                             self::export($value, $indentLevel + 1)."\n";
  146.                     }
  147.                     $output .= $indent.'}';
  148.  
  149.                     return $output;
  150.                 }
  151.  
  152.                 $reflection = new ReflectionObject($var);
  153.                 $properties = $reflection->getProperties();
  154.                 if (empty($properties)) {
  155.                     $output = get_class($var)." {}";
  156.                 } else {
  157.                     $output = get_class($var)." {\n";
  158.                     foreach ($properties as $property) {
  159.                         $property->setAccessible(true);
  160.                         $name     = $property->getName();
  161.                         $type     = $property->getType();
  162.                         $typeName = $type ? $type->getName() : 'mixed';
  163.                         if (in_array($typeName, ['int', 'float', 'string', 'bool', 'array'])) {
  164.                             if ($property->isInitialized($var)) {
  165.                                 $value = $property->getValue($var);
  166.                             } else {
  167.                                 $value = '<uninitialized>';
  168.                             }
  169.                         } else {
  170.                             $other = $property->class;
  171.                             $value = 'object(class::'.$typeName.($other !== '' ? '||'.$other : '').')';
  172.                         }
  173.                         $output .= $indent.'  '.
  174.                             self::highlight($name, 'property').': '.
  175.                             self::export(
  176.                                 $value,
  177.                                 $indentLevel + 1
  178.                             )."\n";
  179.                     }
  180.                     $output .= $indent.'}';
  181.                 }
  182.  
  183.                 return $output;
  184.             case 'resource':
  185.                 return self::highlight('resource', 'resource');
  186.             default:
  187.                 return self::highlight('unknown type', 'unknown');
  188.         }
  189.     }
  190.  
  191.     private
  192.     static function highlight(
  193.         string $text,
  194.         string $type
  195.     ): string {
  196.         if (IS_CLI) {
  197.             $styles = [
  198.                 'null'     => "\033[0;35m", // Magenta
  199.                 'boolean'  => "\033[0;33m", // Yellow
  200.                 'number'   => "\033[0;32m", // Green
  201.                 'string'   => "\033[0;34m", // Blue
  202.                 'property' => "\033[0;36m", // Cyan
  203.                 'resource' => "\033[0;31m", // Red
  204.                 'unknown'  => "\033[1;37m", // White
  205.                 'reset'    => "\033[0m", // Reset
  206.             ];
  207.  
  208.             return $styles[$type].$text.$styles['reset'];
  209.         } else {
  210.             $styles = [
  211.                 'null'     => 'color: #c71585;',  // Rosa fuerte
  212.                 'boolean'  => 'color: #e6193c;',  // Rojo (red)
  213.                 'number'   => 'color: #407ee7;',  // Azul (blue)
  214.                 'string'   => 'color: #29a329;',  // Verde (green)
  215.                 'property' => 'color: #1999b3;',  // Cian (cyan)
  216.                 'resource' => 'color: #3d62f5;',  // Azul (blue)
  217.                 'unknown'  => 'color: #72898f;',  // Gris azulado (base0)
  218.             ];
  219.  
  220.             return '<span style="'.$styles[$type].'">'.htmlspecialchars($text).'</span>';
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Get the singleton instance of teh class Dumper.
  226.      *
  227.      * @return Dumper The singleton instance.
  228.      */
  229.     public static function getInstance(): self
  230.     {
  231.         if (!self::$instance instanceof self) {
  232.             self::$instance = new self();
  233.         }
  234.  
  235.         return self::$instance;
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement