Skip to content

Commit

Permalink
Merge pull request #1 from Grandt/1.0.1
Browse files Browse the repository at this point in the history
Slight reformat of pathJoin
  • Loading branch information
Grandt committed Aug 7, 2014
2 parents 443bb6f + f629a42 commit 72cf09b
Showing 1 changed file with 61 additions and 58 deletions.
119 changes: 61 additions & 58 deletions RelativePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,69 @@
* @version 1.01
*/
class RelativePath {
const VERSION = 1.01;
const VERSION = 1.01;

/**
* Join $file to $dir path, and clean up any excess slashes.
*
* @param String $dir
* @param String $file
*/
public static function pathJoin($dir, $file) {
if (empty($dir) || empty($file)) {
return self::getRelativePath($dir . $file);
}
return self::getRelativePath($dir . '/' . $file);
}
/**
* Join $file to $dir path, and clean up any excess slashes.
*
* @author A. Grandt <[email protected]>
* @author Greg Kappatos
*
* @param string $dir
* @param string $file
*
* @return string Joined path, with the correct forward slash dir separator.
*/
public static function pathJoin($dir, $file) {
return \RelativePath::getRelativePath(
$dir . (empty($dir) || empty($file) ? '' : DIRECTORY_SEPARATOR) . $file
);
}

/**
* Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments.
* If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off.
* The returned path will not end in a "/".
*
* @param String $path The path to clean up
* @return String the clean path
*/
public static function getRelativePath($path) {
$path = preg_replace("#/+\.?/+#", "/", str_replace("\\", "/", $path));
$dirs = explode("/", rtrim(preg_replace('#^(\./)+#', '', $path), '/'));
$offset = 0;
$sub = 0;
$subOffset = 0;
$root = "";
/**
* Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments.
* If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off.
* The returned path will not end in a "/".
*
* @param String $path The path to clean up
* @return String the clean path
*/
public static function getRelativePath($path) {
$path = preg_replace("#/+\.?/+#", "/", str_replace("\\", "/", $path));
$dirs = explode("/", rtrim(preg_replace('#^(\./)+#', '', $path), '/'));
$offset = 0;
$sub = 0;
$subOffset = 0;
$root = "";

if (empty($dirs[0])) {
$root = "/";
$dirs = array_splice($dirs, 1);
} else if (preg_match("#[A-Za-z]:#", $dirs[0])) {
$root = strtoupper($dirs[0]) . "/";
$dirs = array_splice($dirs, 1);
}
if (empty($dirs[0])) {
$root = "/";
$dirs = array_splice($dirs, 1);
} else if (preg_match("#[A-Za-z]:#", $dirs[0])) {
$root = strtoupper($dirs[0]) . "/";
$dirs = array_splice($dirs, 1);
}

$newDirs = array();
foreach ($dirs as $dir) {
if ($dir !== "..") {
$subOffset--;
$newDirs[++$offset] = $dir;
} else {
$subOffset++;
if (--$offset < 0) {
$offset = 0;
if ($subOffset > $sub) {
$sub++;
}
}
}
}
$newDirs = array();
foreach ($dirs as $dir) {
if ($dir !== "..") {
$subOffset--;
$newDirs[++$offset] = $dir;
} else {
$subOffset++;
if (--$offset < 0) {
$offset = 0;
if ($subOffset > $sub) {
$sub++;
}
}
}
}

if (empty($root)) {
$root = str_repeat("../", $sub);
}
return $root . implode("/", array_slice($newDirs, 0, $offset));
}
}
?>
if (empty($root)) {
$root = str_repeat("../", $sub);
}
return $root . implode("/", array_slice($newDirs, 0, $offset));
}
}

0 comments on commit 72cf09b

Please sign in to comment.