/** * Returns syntax highlighted source code. * @param string * @param int * @param int * @return string */ public static function highlightPhp($source, $line, $lines = 15, $vars = array()) { if (function_exists('ini_set')) { ini_set('highlight.comment', '#998; font-style: italic'); ini_set('highlight.default', '#000'); ini_set('highlight.html', '#06B'); ini_set('highlight.keyword', '#D24; font-weight: bold'); ini_set('highlight.string', '#080'); } $source = str_replace(array("\r\n", "\r"), "\n", $source); $source = explode("\n", highlight_string($source, TRUE)); $spans = 1; $out = $source[0]; // <code><span color=highlight.html> $source = explode('<br />', $source[1]); array_unshift($source, NULL); $start = $i = max(1, $line - floor($lines * 2 / 3)); while (--$i >= 1) { // find last highlighted block if (preg_match('#.*(</?span[^>]*>)#', $source[$i], $m)) { if ($m[1] !== '</span>') { $spans++; $out .= $m[1]; } break; } } $source = array_slice($source, $start, $lines, TRUE); end($source); $numWidth = strlen((string) key($source)); foreach ($source as $n => $s) { $spans += substr_count($s, '<span') - substr_count($s, '</span'); $s = str_replace(array("\r", "\n"), array('', ''), $s); preg_match_all('#<[^>]+>#', $s, $tags); if ($n == $line) { $out .= sprintf("<span class='highlight'>%{$numWidth}s: %s\n</span>%s", $n, strip_tags($s), implode('', $tags[0])); } else { $out .= sprintf("<span class='line'>%{$numWidth}s:</span> %s\n", $n, $s); } } $out .= str_repeat('</span>', $spans) . '</code>'; $out = preg_replace_callback('#">\\$(\\w+)( )?</span>#', function ($m) use($vars) { return isset($vars[$m[1]]) ? '" title="' . str_replace('"', '"', strip_tags(Helpers::htmlDump($vars[$m[1]]))) . $m[0] : $m[0]; }, $out); return "<pre><div>{$out}</div></pre>"; }
/** * Dumps information about a variable in readable format. * @param mixed variable to dump * @param bool return output instead of printing it? (bypasses $productionMode) * @return mixed variable itself or dump */ public static function dump($var, $return = FALSE) { if (!$return && self::$productionMode) { return $var; } $output = "<pre class=\"nette-dump\">" . Helpers::htmlDump($var) . "</pre>\n"; if (!$return) { $trace = debug_backtrace(FALSE); $i = Helpers::findTrace($trace, 'dump') ? 1 : 0; if (isset($trace[$i]['file'], $trace[$i]['line']) && is_file($trace[$i]['file'])) { $lines = file($trace[$i]['file']); preg_match('#dump\\((.*)\\)#', $lines[$trace[$i]['line'] - 1], $m); $output = substr_replace($output, ' title="' . htmlspecialchars((isset($m[0]) ? "{$m['0']} \n" : '') . "in file {$trace[$i]['file']} on line {$trace[$i]['line']}") . '"', 4, 0); if (self::$showLocation) { $output = substr_replace($output, ' <small>in ' . Helpers::editorLink($trace[$i]['file'], $trace[$i]['line']) . ":{$trace[$i]['line']}</small>", -8, 0); } } } if (self::$consoleMode) { if (self::$consoleColors && substr(getenv('TERM'), 0, 5) === 'xterm') { $output = preg_replace_callback('#<span class="php-(\\w+)">|</span>#', function ($m) { return "[" . (isset($m[1], Debugger::$consoleColors[$m[1]]) ? Debugger::$consoleColors[$m[1]] : '0') . "m"; }, $output); } $output = htmlspecialchars_decode(strip_tags($output), ENT_QUOTES); } if ($return) { return $output; } else { echo $output; return $var; } }
/** * Dumps information about a variable in readable format. * @param mixed variable to dump * @param bool return output instead of printing it? (bypasses $productionMode) * @return mixed variable itself or dump */ public static function dump($var, $return = FALSE) { if (!$return && self::$productionMode) { return $var; } $output = "<pre class=\"nette-dump\">" . Helpers::htmlDump($var) . "</pre>\n"; if (!$return) { $trace = debug_backtrace(); $i = !isset($trace[1]['class']) && isset($trace[1]['function']) && $trace[1]['function'] === 'dump' ? 1 : 0; if (isset($trace[$i]['file'], $trace[$i]['line']) && is_file($trace[$i]['file'])) { $lines = file($trace[$i]['file']); preg_match('#dump\\((.*)\\)#', $lines[$trace[$i]['line'] - 1], $m); $output = substr_replace($output, ' title="' . htmlspecialchars((isset($m[0]) ? "{$m['0']} \n" : '') . "in file {$trace[$i]['file']} on line {$trace[$i]['line']}") . '"', 4, 0); if (self::$showLocation) { $output = substr_replace($output, ' <small>in ' . Helpers::editorLink($trace[$i]['file'], $trace[$i]['line']) . ":{$trace[$i]['line']}</small>", -8, 0); } } } if (self::$consoleMode) { $output = htmlspecialchars_decode(strip_tags($output), ENT_NOQUOTES); } if ($return) { return $output; } else { echo $output; return $var; } }