Skip to content

Commit 72cf09b

Browse files
committed
Merge pull request #1 from Grandt/1.0.1
Slight reformat of pathJoin
2 parents 443bb6f + f629a42 commit 72cf09b

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

RelativePath.php

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,66 +18,69 @@
1818
* @version 1.01
1919
*/
2020
class RelativePath {
21-
const VERSION = 1.01;
21+
const VERSION = 1.01;
2222

23-
/**
24-
* Join $file to $dir path, and clean up any excess slashes.
25-
*
26-
* @param String $dir
27-
* @param String $file
28-
*/
29-
public static function pathJoin($dir, $file) {
30-
if (empty($dir) || empty($file)) {
31-
return self::getRelativePath($dir . $file);
32-
}
33-
return self::getRelativePath($dir . '/' . $file);
34-
}
23+
/**
24+
* Join $file to $dir path, and clean up any excess slashes.
25+
*
26+
* @author A. Grandt <[email protected]>
27+
* @author Greg Kappatos
28+
*
29+
* @param string $dir
30+
* @param string $file
31+
*
32+
* @return string Joined path, with the correct forward slash dir separator.
33+
*/
34+
public static function pathJoin($dir, $file) {
35+
return \RelativePath::getRelativePath(
36+
$dir . (empty($dir) || empty($file) ? '' : DIRECTORY_SEPARATOR) . $file
37+
);
38+
}
3539

36-
/**
37-
* Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments.
38-
* If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off.
39-
* The returned path will not end in a "/".
40-
*
41-
* @param String $path The path to clean up
42-
* @return String the clean path
43-
*/
44-
public static function getRelativePath($path) {
45-
$path = preg_replace("#/+\.?/+#", "/", str_replace("\\", "/", $path));
46-
$dirs = explode("/", rtrim(preg_replace('#^(\./)+#', '', $path), '/'));
47-
48-
$offset = 0;
49-
$sub = 0;
50-
$subOffset = 0;
51-
$root = "";
40+
/**
41+
* Clean up a path, removing any unnecessary elements such as /./, // or redundant ../ segments.
42+
* If the path starts with a "/", it is deemed an absolute path and any /../ in the beginning is stripped off.
43+
* The returned path will not end in a "/".
44+
*
45+
* @param String $path The path to clean up
46+
* @return String the clean path
47+
*/
48+
public static function getRelativePath($path) {
49+
$path = preg_replace("#/+\.?/+#", "/", str_replace("\\", "/", $path));
50+
$dirs = explode("/", rtrim(preg_replace('#^(\./)+#', '', $path), '/'));
51+
52+
$offset = 0;
53+
$sub = 0;
54+
$subOffset = 0;
55+
$root = "";
5256

53-
if (empty($dirs[0])) {
54-
$root = "/";
55-
$dirs = array_splice($dirs, 1);
56-
} else if (preg_match("#[A-Za-z]:#", $dirs[0])) {
57-
$root = strtoupper($dirs[0]) . "/";
58-
$dirs = array_splice($dirs, 1);
59-
}
57+
if (empty($dirs[0])) {
58+
$root = "/";
59+
$dirs = array_splice($dirs, 1);
60+
} else if (preg_match("#[A-Za-z]:#", $dirs[0])) {
61+
$root = strtoupper($dirs[0]) . "/";
62+
$dirs = array_splice($dirs, 1);
63+
}
6064

61-
$newDirs = array();
62-
foreach ($dirs as $dir) {
63-
if ($dir !== "..") {
64-
$subOffset--;
65-
$newDirs[++$offset] = $dir;
66-
} else {
67-
$subOffset++;
68-
if (--$offset < 0) {
69-
$offset = 0;
70-
if ($subOffset > $sub) {
71-
$sub++;
72-
}
73-
}
74-
}
75-
}
65+
$newDirs = array();
66+
foreach ($dirs as $dir) {
67+
if ($dir !== "..") {
68+
$subOffset--;
69+
$newDirs[++$offset] = $dir;
70+
} else {
71+
$subOffset++;
72+
if (--$offset < 0) {
73+
$offset = 0;
74+
if ($subOffset > $sub) {
75+
$sub++;
76+
}
77+
}
78+
}
79+
}
7680

77-
if (empty($root)) {
78-
$root = str_repeat("../", $sub);
79-
}
80-
return $root . implode("/", array_slice($newDirs, 0, $offset));
81-
}
82-
}
83-
?>
81+
if (empty($root)) {
82+
$root = str_repeat("../", $sub);
83+
}
84+
return $root . implode("/", array_slice($newDirs, 0, $offset));
85+
}
86+
}

0 commit comments

Comments
 (0)