function testrelativePath() { $this->assertEquals('tests/File', File::relativePath('/usr/share/pear/tests/File', '/usr/share/pear', '/')); $this->assertEquals('../etc', File::relativePath('/etc', '/usr', '/')); $this->assertEquals('D:\\Data', File::relativePath('D:\\Data', 'C:\\Data', '\\')); if (OS_WINDOWS) { $this->assertEquals('data\\dir', File::relativePath('/var/data/dir', '/var')); } else { $this->assertEquals('data/dir', File::relativePath('/var/data/dir', '/var')); } $this->assertEquals('../', File::relativePath('data', 'data/dir', '/')); }
/** * Render a nice output of a backtrace from an exception * * <b>The skip parameter</b> * - To skip a plain function, simply specify the function name. i.e. "__errorhandler" * - To skip a class or instance method, specify "Class::methodName" * * @param Exception * @param bool Include call trace in the output * @param bool Return nicely formatted HTML instead of plain text * @return string * @see format() */ public static function formatTrace(Exception $e, $html = true, $skip = null) { $trace = $e->getTrace(); $traceLen = count($trace); $str = ''; if ($e instanceof PHPException) { $skip = is_array($skip) ? array_merge($skip, array('PHPException::rethrow')) : array('PHPException::rethrow'); } if ($traceLen > 0) { if ($html) { $str .= "<div class=\"trace\"><pre>"; } if ($skip) { $traceTmp = $trace; $trace = array(); foreach ($traceTmp as $i => $ti) { if (in_array($ti['function'], $skip)) { continue; } if (isset($ti['type'])) { if (in_array($ti['class'] . '::' . $ti['function'], $skip)) { continue; } } $trace[] = $ti; } } $noSpace = strlen(strval($traceLen)); foreach ($trace as $i => $ti) { $args = '()'; if (isset($ti['args'])) { $argsCnt = count($ti['args']); if ($argsCnt > 0) { $args = '(' . $argsCnt . ')'; } } $str .= sprintf(" % {$noSpace}s ", $traceLen - $i); if (isset($ti['type'])) { $str .= $ti['class'] . $ti['type']; } else { $str .= '::'; } $str .= $ti['function'] . $args; if (isset($ti['line'])) { $str .= ' on line ' . $ti['line']; } if (isset($ti['file'])) { $file = File::relativePath($ti['file'], @$_SERVER['DOCUMENT_ROOT']); if ($file[0] != '/') { $file = '/' . $file; } $str .= ' in ' . $file; } $str .= "\n"; } $str .= $html ? "</pre></div>\n" : "\n"; } return trim($str, "\n") . "\n"; }
/** * Get real path (works with non-existant paths) * * @static * @access public * @return string * @param string $path * @param string $separator */ function realpath($path, $separator = DIRECTORY_SEPARATOR) { if (!strlen($path)) { return $separator; } $drive = ''; if (OS_WINDOWS) { $path = preg_replace('/[\\\\\\/]/', $separator, $path); if (preg_match('/([a-zA-Z]\\:)(.*)/', $path, $matches)) { $drive = $matches[1]; $path = $matches[2]; } else { $cwd = getcwd(); $drive = substr($cwd, 0, 2); if ($path[0] !== $separator[0]) { $path = substr($cwd, 3) . $separator . $path; } } } elseif ($path[0] !== $separator) { $cwd = getcwd(); $path = $cwd . $separator . File::relativePath($path, $cwd, $separator); } $dirStack = array(); foreach (explode($separator, $path) as $dir) { if (!strlen($dir) || $dir == '.') { continue; } if ($dir == '..') { array_pop($dirStack); } else { $dirStack[] = $dir; } } return $drive . $separator . implode($separator, $dirStack); }
* @subpackage base */ #onPHPError( $errno, $str, $file, $line, &$context ) if ($errno == E_WARNING || $errno == E_USER_WARNING) { throw new PHPException($str, $errno, $file, $line); } global $__original_errhandler; if ($__original_errhandler) { return; $__original_errhandler($errno, $str, $file, $line, $context); } $fileLine = "on line {$line} in "; if (isset($_SERVER['DOCUMENT_ROOT'])) { $fileLine .= File::relativePath($file, $_SERVER['DOCUMENT_ROOT']); } elseif (isset($GLOBALS['argv'][0])) { $fileLine .= File::relativePath($file, dirname($GLOBALS['argv'][0])); } else { $fileLine .= $file; } switch ($errno) { case E_PARSE: case E_USER_ERROR: case E_ERROR: break; case E_NOTICE: case E_USER_NOTICE: if (PHP::isCLI()) { IO::writeError("{$GLOBALS['argv'][0]}: WARNING: {$str} {$fileLine}\n"); } else { error_log("WARNING: {$str} {$fileLine}"); }
/** * Convert absolute path to a relative path, based in <samp>$relativeToBase</samp> * * <b>Example</b> * <code> * print Utils::relativePath('/absolute/path/to/foo.bar', '/absolute/path'); * prints "to/foo.bar" * </code> * * @param string * @param string * @return string * @deprecated Use {@link File::relativePath()} instead */ public static function relativePath($absolutePath, $relativeToBase) { return File::relativePath($absolutePath, $relativeToBase); }