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