1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Filesystem; |
4
|
|
|
|
5
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A cross-platform filesystem path wrapper. |
9
|
|
|
* |
10
|
|
|
* This class is a wrapper for handling file paths in stakx in a cross-platform way. Give it a Windows path and append |
11
|
|
|
* a Unix style path and it'll just work. |
12
|
|
|
*/ |
13
|
|
|
final class FilesystemPath |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
private $absolutePath; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $originalPath; |
19
|
|
|
/** @var bool */ |
20
|
|
|
private $isWindows; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $filePath |
24
|
|
|
* @param string $dirSep |
25
|
|
|
*/ |
26
|
24 |
|
public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR) |
27
|
|
|
{ |
28
|
24 |
|
$this->originalPath = $filePath; |
29
|
24 |
|
$this->isWindows = ($dirSep === '\\'); |
30
|
|
|
|
31
|
24 |
|
if ($this->isWindows) |
32
|
|
|
{ |
33
|
2 |
|
$filePath = $this->unixifyPath($filePath); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
24 |
|
$this->absolutePath = (fs::isAbsolutePath($filePath)) ? $filePath : fs::absolutePath($filePath); |
|
|
|
|
37
|
24 |
|
} |
38
|
|
|
|
39
|
10 |
|
public function __toString() |
40
|
|
|
{ |
41
|
10 |
|
return $this->getAbsolutePath(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Append a path to a directory path. |
46
|
|
|
* |
47
|
|
|
* @param string $append The path to append |
48
|
|
|
*/ |
49
|
5 |
|
public function appendToPath($append) |
50
|
|
|
{ |
51
|
5 |
|
if ($this->isFile(false)) |
52
|
|
|
{ |
53
|
1 |
|
throw new \InvalidArgumentException("Appending to a file's path is not possible"); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
$this->absolutePath = $this->buildPath($this->absolutePath, $this->unixifyPath($append)); |
57
|
4 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Generate a path based off this file path. |
61
|
|
|
* |
62
|
|
|
* This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that. |
63
|
|
|
* |
64
|
|
|
* @param string $append |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
12 |
|
public function generatePath($append) |
69
|
|
|
{ |
70
|
12 |
|
return $this->buildPath($this->absolutePath, $this->unixifyPath($append)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the absolute path of the file path. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
10 |
|
public function getAbsolutePath() |
79
|
|
|
{ |
80
|
10 |
|
if ($this->isWindows) |
81
|
|
|
{ |
82
|
2 |
|
return str_replace('/', '\\', $this->absolutePath); |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
return $this->absolutePath; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether the given path is a directory. |
90
|
|
|
* |
91
|
|
|
* @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory. |
92
|
|
|
* When set to false, this function will guess based on the path ending in a directory |
93
|
|
|
* separator. |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
18 |
|
public function isDir($checkExistence = true) |
98
|
|
|
{ |
99
|
18 |
|
$absPath = $this->absolutePath; |
100
|
|
|
|
101
|
18 |
|
if ($checkExistence) |
102
|
|
|
{ |
103
|
12 |
|
return file_exists($absPath) && is_dir($absPath); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
return (substr($absPath, -1, 1) == '/'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check whether the given path is a file. |
111
|
|
|
* |
112
|
|
|
* @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When |
113
|
|
|
* set to false, this function will guess based on the path ending in a directory |
114
|
|
|
* separator. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
5 |
|
public function isFile($checkExistence = true) |
119
|
|
|
{ |
120
|
5 |
|
$absPath = $this->absolutePath; |
121
|
|
|
|
122
|
5 |
|
if ($checkExistence) |
123
|
|
|
{ |
124
|
|
|
return file_exists($absPath) && is_file($absPath); |
125
|
|
|
} |
126
|
|
|
|
127
|
5 |
|
return (!$this->isDir($checkExistence)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Build a path from multiple strings. |
132
|
|
|
* |
133
|
|
|
* This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use. |
134
|
|
|
* The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
16 |
View Code Duplication |
private function buildPath() |
|
|
|
|
139
|
|
|
{ |
140
|
16 |
|
$paths = []; |
141
|
|
|
|
142
|
16 |
|
foreach (func_get_args() as $arg) |
143
|
|
|
{ |
144
|
16 |
|
if ($arg !== '') |
145
|
|
|
{ |
146
|
16 |
|
$paths[] = $arg; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
16 |
|
return preg_replace('#(?<!:)/+#', '/', join('/', $paths)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Convert a Windows path into a blasphemous Unix path. |
155
|
|
|
* |
156
|
|
|
* @param string $filePath |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
16 |
|
private function unixifyPath($filePath) |
161
|
|
|
{ |
162
|
16 |
|
return str_replace('\\', '/', $filePath); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|