function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { /** @var ResponseInterface $response */ $response = $next(); $lang = $this->locale->locale(); // The check for app->translation is made here instead of conditionally adding this middleware // because loaded modules can change this setting. if (!$this->settings->translation || !$lang) { return $response; } if (!isset(self::$translation[$lang])) { // Load and merge all translation files now. self::$translation[$lang] = []; $trans =& self::$translation[$lang]; $folders = $this->settings->languageFolders; foreach ($folders as $folder) { $path = "{$folder}/{$lang}.ini"; $newTrans = fileExists($path) ? parse_ini_file($path) : null; if ($newTrans) { $trans = array_merge($trans, $newTrans); } } if (!$trans) { $paths = array_map(function ($path) { return "<li>" . ErrorConsole::shortFileName($path); }, $folders); throw new ConfigException("A translation file for language <b>{$lang}</b> was not found.\n<p>Search paths:\n<ul>" . implode('', $paths) . "</ul>", FlashType::ERROR); } } $out = preg_replace_callback(self::FIND_TRANS_KEY, function ($args) use($lang) { $a = $args[1]; return empty(self::$translation[$lang][$a]) ? '$' . $a : preg_replace('#\\r?\\n#', '<br>', self::$translation[$lang][$a]); }, $response->getBody()); return $response->withBody($this->responseFactory->makeBody($out)); }
/** * Outputs a stack trace up to the first call to a trace function (which may be this one or a wrapper that calls * this one). * <p>It displays detailed timing and memory consumption information about each function/method call. * * <p>It requires a logger panel named 'trace' to be defined. * <p>It also requires XDebug to be installed. * * ##### Usage * * Put the following code at the place where you want the trace log to be captured: * * \PhpKit\WebConsole\DebugConsole\DebugConsole::trace (); * * @throws Exception */ public static function trace() { if (!extension_loaded('xdebug')) { throw new Exception("<kbd>trace()</kbd> requires Xdebug to be installed."); } $v = ini_get('xdebug.collect_params'); ob_start(); ini_set('xdebug.collect_params', 2); xdebug_print_function_stack(); $trace = ob_get_clean(); $trace = preg_replace('@^(?:.*?)<table class=\'xdebug-error xe-xdebug\'(.*?)<tr>(?:.*?)>Location</th></tr>@s', '<table class="__console-table trace"$1<colgroup> <col width=40><col width=72><col width=72><col width=72><col width=75%><col width=25%> <thead><tr><th>#<th>Time (ms)<th>Delta (ms)<th>Mem.(MB)<th>Function<th>Location</tr></thead>', $trace); $trace = preg_replace(['@</table>.*@s', "/align='center'/", '@(trace\\( \\)</td>.*?</tr>)(.*)</table>@s'], ['</table>', 'align=right', '$1</table>'], $trace); $prev = 0; $trace = preg_replace_callback('#<tr><td (.*?)>(.*?)</td><td (.*?)>(.*?)</td><td (.*?)>(.*?)</td><td (.*?)>(.*?)</td><td title=\'(.*?)\'(.*?)>(.*?)</td></tr>#', function ($m) use(&$prev) { $t = $m[4] * 1000; $s = $t - $prev; $d = number_format($s, 1); $dd = $s >= self::PROFILER_WARNING_TRESHOLD ? ' class=__alert' : ''; $prev = $t; $t = number_format($t, 1); $r = number_format($m[6] / 1048576, 3); $p = ErrorConsole::shortFileName($m[9]); $f = substr($m[11], 3); list($fn, $args) = explode('(', $m[8], 2); $info = preg_replace('/[\\w{}]+$/', '<b>$0</b>', $fn) . '(' . $args; return "<tr><th {$m['1']}>{$m['2']}<td {$m['3']}>{$t}<td align=right{$dd}>{$d}<td {$m['5']}>{$r}<td {$m['7']}>{$info}<td class='__type' title='{$p}'{$m['10']}>{$f}</tr>"; }, $trace); ini_set('xdebug.collect_params', $v); self::logger('trace')->write($trace); }
public function showCallLocation() { $namespace = Debug::libraryNamespace(); $base = __DIR__; $stack = debug_backtrace(0); $FNS = self::GLOBAL_LOG_FNS; // Discard frames of all functions that belong to this library. while (!empty($stack) && (isset($stack[0]['file']) && stripos($stack[0]['file'], $base) === 0 || isset($stack[0]['class']) && stripos($stack[0]['class'], $namespace) === 0 || isset($stack[0]['function']) && !isset($FNS[$stack[0]['function']]))) { array_shift($stack); } $trace = $stack ? $stack[0] : []; $path = isset($trace['file']) ? $trace['file'] : ''; $line = isset($trace['line']) ? $trace['line'] : ''; $shortPath = ErrorConsole::shortFileName($path); $shortPath = str_segmentsLast($shortPath, '/'); $location = empty($line) ? $shortPath : ErrorConsole::errorLink($path, $line, 1, "{$shortPath}:{$line}", 'hint--rounded hint--left', 'data-hint'); if ($path != '') { $path = <<<HTML <div class="__debug-location">At {$location}</div> HTML; } $this->write($path); return $this; }
/** * Shortcut to log a formatted exception on the provided logger. * * @param LoggerInterface $logger * @param Exception $exception */ public static function logException(LoggerInterface $logger, Exception $exception) { $logger->error(sprintf("%s, at %s(%s)", $exception->getMessage(), ErrorConsole::shortFileName($exception->getFile()), $exception->getLine())); }