blob: 0db583d7e16c8a89ad3225f68e9e42ccd2f6c6c1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<?php
namespace PlasmaConduit;
class Path {
/**
* This function takes a variable amount of strings and joins
* them together so that they form a valid file path.
*
* @param {String ...} $peice - The peices of the file path
* @returns {String} - The final file path
*/
static public function join() {
$peices = array_filter(func_get_args(), function($value) {
return $value;
});
return self::normalize(implode("/", $peices));
}
/**
* This function takes a valid file path and nomalizes it into
* the simplest form possible.
*
* @param {String} $path - The path to normalize
* @returns {String} - The normailized path
*/
static public function normalize($path) {
if (!strlen($path)) {
return ".";
}
$isAbsolute = $path[0];
$trailingSlash = $path[strlen($path) - 1];
$up = 0;
$peices = array_values(array_filter(explode("/", $path), function($n) {
return !!$n;
}));
for ($i = count($peices) - 1; $i >= 0; $i--) {
$last = $peices[$i];
if ($last == ".") {
array_splice($peices, $i, 1);
} else if ($last == "..") {
array_splice($peices, $i, 1);
$up++;
} else if ($up) {
array_splice($peices, $i, 1);
$up--;
}
}
$path = implode("/", $peices);
if (!$path && !$isAbsolute) {
$path = ".";
}
if ($path && $trailingSlash == "/") {
$path .= "/";
}
return ($isAbsolute == "/" ? "/" : "") . $path;
}
}
|