public function testTypesAreEqual() { $this->assertEquals('boolean', Types::TYPE_BOOLEAN); $this->assertEquals('integer', Types::TYPE_INTEGER); $this->assertEquals('float', Types::TYPE_FLOAT); $this->assertEquals('string', Types::TYPE_STRING); $this->assertEquals('array', Types::TYPE_ARRAY); $this->assertEquals('object', Types::TYPE_OBJECT); $this->assertEquals('resource', Types::TYPE_RESOURCE); $this->assertEquals('NULL', Types::TYPE_NULL); $this->assertEquals('callable', Types::TYPE_CALLABLE_CALLBACK); // check if getType is working properly $this->assertEquals('boolean', Types::getType(true)); $this->assertEquals('integer', Types::getType(1)); $this->assertEquals('integer', Types::getType(-1)); $this->assertEquals('integer', Types::getType(3232)); // Will fail - number is larger than PHP_INT_MAX on x64 systems or 32 for that matter // $this->assertEquals('integer', Types::getType(9223372036854775809)); $this->assertEquals('float', Types::getType(1.1)); $this->assertEquals('string', Types::getType('string')); $this->assertEquals('array', Types::getType([1, 'string'])); $this->assertEquals('object', Types::getType(new DateTime())); $this->assertEquals('NULL', Types::getType(NULL)); $this->assertEquals('callable', Types::getType(function () { })); }
/** * @param $dump * @param array $options */ public static function dump($dump, array $options = ['excerpt' => false]) { $settings = json_decode(file_get_contents(__DIR__ . '/../../settings.json'), true); if (is_null($settings)) { $error = "Settings file is invalid.<br>Possible problems:<br>"; $error .= " - The file doesn't exist, in which case feel free to create one yourself, or just copy it from <a target='_blank' href='https://gist.github.com/JunkyPic/4adbf5495845f4bb3ee684fd1f5078fa'>here</a>"; $error .= " and place in the src folder.<br>"; $error .= " - The json format is invalid. A tutorial on how to write valid json can be found <a target='_blank' href=\"http://lmgtfy.com/?q=how+to+create+json\">here</a>"; die($error); } $css = isset($settings['theme']) ? file_get_contents(__DIR__ . '/Themes/' . $settings['theme'] . '.css') : ''; $js = file_get_contents(__DIR__ . '/Themes/js.js'); if (isset($settings['remove-git-link']) && $settings['remove-git-link'] == false) { static::$output .= "<dl><dt><a class=\"github\" target=\"_blank\" href=\"https://github.com/JunkyPic/php-pretty-print\">Visit on Github</a></dt></dl></div>"; } static::$debugBacktrace = debug_backtrace(); // Parse the debug backtrace until the current file is reached foreach (static::$debugBacktrace as $key => $value) { if (isset($value['class']) && $value['class'] == __CLASS__) { // get whatever info is needed // in case anybody's confused about what the following code does: // it parses the file in which ::dump was called and searches // for the argument passed in. The idea is the to get argument // as a string literal, as in pass $array get the string literal // '$array' back, NOT the value of the $array variable // open the file in which this method was called $file = fopen($value['file'], 'r'); // get the name the argument passed in to ::dump while (($line = fgets($file)) !== false) { // explode the class name $explode = explode('\\', $value['class']); // get the last entry, meaning exactly the class name // and append the method $functionName = end($explode) . '::dump'; // if the current line contains this method if (strpos($line, $functionName) !== false) { // since only the name of the variable passed in is of interest // we can just replace the method and the trailing ); if (strpos($line, $functionName) !== false) { $argument = substr($line, strpos($line, $functionName)); $argument = str_replace(['\\', $functionName . '(', ');', '/'], '', $argument); $options['argument_name'] = preg_replace('~\\x{00a0}~', '', preg_replace('/\\s+/', '', trim($argument))); } // get other info of interest $options['file'] = $value['file']; $options['line'] = $value['line']; break; } } fclose($file); } } static::$output .= HtmlBuilder::create()->getHtml($dump, Types::getType($dump), $options); static::$output .= "</div>"; if (isset($settings['pre-tags']) && $settings['pre-tags'] == true) { static::$output .= "<style>{$css}</style>"; static::$output .= "<script src='/src/junky/php-pretty-print/Themes/js.js'></script>"; echo '<pre>'; echo static::$output; static::$output = ''; echo '</pre>'; die; } static::$output .= "<style>{$css}</style>"; static::$output .= "<script type=\"<text/javascript>\">{$js}</script>"; echo static::$output; static::$output = ''; die; }
/** * @param $array * @param $options * * @throws \Exception */ private function buildFromArrayRecursive($array, array $options) { $this->html .= "<dd class=\"" . $this->cssClasses['dd'] . "\">"; $this->html .= "<dl class=\"" . $this->cssClasses['dl'] . "\">"; foreach ($array as $key => $value) { if (is_object($value)) { $reflection = new \ReflectionObject($value); $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\"><span class=\"css-string-object\">Object: {$reflection->getName()}</span></dt>"; $this->buildFromObject($value, $reflection); } else { if (Types::getType($value) === Types::TYPE_ARRAY) { $typeKey = gettype($key); $typeValue = gettype($value); $lengthValue = count($value); $printKey = $this->escape($key); $lengthKey = strlen($key); if ($typeKey == 'string') { $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\">" . "<span class=\"css-array-keys\">{$typeKey} ({$lengthKey}) \"{$printKey}\"</span>" . "<span class=\"css-pointer\"> => </span>" . "<span class=\"css-array-values\">{$typeValue}({$lengthValue})</span>" . "<br/>" . "</dt>"; } else { $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\">" . "<span class=\"css-array-keys\">{$typeKey} {$printKey}</span>" . "<span class=\"css-pointer\"> => </span>" . "<span class=\"css-array-values\">{$typeValue}({$lengthValue})</span>" . "<br/>" . "</dt>"; } $this->buildFromArrayRecursive($value, $options); } else { $printKey = $this->escape($key); $printValue = $this->getExcerpt($this->escape($value), $options); $typeKey = gettype($key); $typeValue = gettype($value); $lengthValue = strlen($value); $lengthKey = strlen($key); if ($typeKey == 'string') { if ($typeValue == 'string') { $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\">" . "<span class=\"css-array-keys\">{$typeKey} ({$lengthKey}) \"{$printKey}\"</span>" . "<span class=\"css-pointer\"> => </span>" . "<span class=\"css-array-values\">{$typeValue} ({$lengthValue})\"{$printValue}\"</span>" . "</dt>" . "<dd class=\"" . $this->cssClasses['dd'] . "\"></dd>"; } else { $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\">" . "<span class=\"css-array-keys\">{$typeKey} ({$lengthKey}) \"{$printKey}\"</span>" . "<span class=\"css-pointer\"> => </span>" . "<span class=\"css-array-values\">{$typeValue} {$printValue}</span>" . "</dt>" . "<dd class=\"" . $this->cssClasses['dd'] . "\"></dd>"; } } else { if ($typeValue == 'string') { $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\">" . "<span class=\"css-array-keys\">{$typeKey} {$printKey}</span>" . "<span class=\"css-pointer\"> => </span>" . "<span class=\"css-array-values\">{$typeValue} ({$lengthValue})\"{$printValue}\"</span>" . "</dt>" . "<dd class=\"" . $this->cssClasses['dd'] . "\"></dd>"; } else { $this->html .= "<dt class=\"" . $this->cssClasses['dt'] . "\">\n <span class=\"css-array-keys\">{$typeKey} {$printKey}</span>" . "<span class=\"css-pointer\"> => </span>" . "<span class=\"css-array-values\">{$typeValue} {$printValue}</span>" . "</dt>" . "<dd class=\"" . $this->cssClasses['dd'] . "\"></dd>"; } } } } } $this->html .= "</dl>"; $this->html .= "</dd>"; }