-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Grandt/1.0.1
Slight reformat of pathJoin
- Loading branch information
Showing
1 changed file
with
61 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
} | ||
} |