18
18
* @version 1.01
19
19
*/
20
20
class RelativePath {
21
- const VERSION = 1.01 ;
21
+ const VERSION = 1.01 ;
22
22
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
+ }
35
39
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 = "" ;
52
56
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
+ }
60
64
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
+ }
76
80
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