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\Service; |
11
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
12
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Filesystem. |
16
|
|
|
* |
17
|
|
|
* This class extends Symfony's Filesystem to provide convenience functions |
18
|
|
|
*/ |
19
|
|
|
class Filesystem extends \Symfony\Component\Filesystem\Filesystem |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Build an absolute file or directory path separated by the OS specific directory separator. |
23
|
|
|
* |
24
|
|
|
* @param string ...$pathFragments |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
49 |
|
public function absolutePath($pathFragments) |
29
|
|
|
{ |
30
|
49 |
|
if ($pathFragments instanceof FilesystemPath) |
31
|
|
|
{ |
32
|
|
|
$pathFragments = (string)$pathFragments; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
49 |
|
if ($this->isAbsolutePath($pathFragments)) |
36
|
|
|
{ |
37
|
|
|
return $pathFragments; |
38
|
|
|
} |
39
|
|
|
|
40
|
49 |
|
$args = func_get_args(); |
41
|
49 |
|
array_unshift($args, Service::getWorkingDirectory()); |
42
|
|
|
|
43
|
49 |
|
return implode(DIRECTORY_SEPARATOR, $args); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Build a file or directory path separated by the OS specific directory separator. |
48
|
|
|
* |
49
|
|
|
* @param string ...$pathFragments |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
44 |
|
public function appendPath($pathFragments) |
|
|
|
|
54
|
|
|
{ |
55
|
44 |
|
return implode(DIRECTORY_SEPARATOR, func_get_args()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Copy a file or folder recursively. |
60
|
|
|
* |
61
|
|
|
* @param string $originFile The original filename |
62
|
|
|
* @param string $targetFile The target filename |
63
|
|
|
* @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten |
64
|
|
|
* |
65
|
|
|
* @throws FileNotFoundException When originFile doesn't exist |
66
|
|
|
* @throws IOException When copy fails |
67
|
|
|
*/ |
68
|
|
|
public function copy($originFile, $targetFile, $overwriteNewerFiles = false) |
69
|
|
|
{ |
70
|
|
|
if ($this->isDir($originFile)) |
71
|
|
|
{ |
72
|
|
|
if (!$this->isDir($targetFile)) |
73
|
|
|
{ |
74
|
|
|
mkdir($targetFile, 0755, true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$dir = dir($originFile); |
78
|
|
|
|
79
|
|
|
while (false !== $entry = $dir->read()) |
80
|
|
|
{ |
81
|
|
|
// Skip pointers |
82
|
|
|
if ($entry == '.' || $entry == '..') |
83
|
|
|
{ |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->copy("$originFile/$entry", "$targetFile/$entry", true); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$dir->close(); |
91
|
|
|
} |
92
|
|
|
else |
93
|
|
|
{ |
94
|
|
|
parent::copy($originFile, $targetFile, $overwriteNewerFiles); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create an instance of stakx's File object with relative path information. |
100
|
|
|
* |
101
|
|
|
* @param string $filePath |
102
|
|
|
* |
103
|
|
|
* @return File |
104
|
|
|
*/ |
105
|
|
|
public function createFileObject($filePath) |
106
|
|
|
{ |
107
|
|
|
return new File($this->absolutePath($filePath)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Strip the current working directory from an absolute path. |
112
|
|
|
* |
113
|
|
|
* @param string $path An absolute path |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
32 |
|
public function getRelativePath($path) |
118
|
|
|
{ |
119
|
32 |
|
return str_replace(Service::getWorkingDirectory() . DIRECTORY_SEPARATOR, '', $path); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the name of a given file without the extension. |
124
|
|
|
* |
125
|
|
|
* @param string $filePath A file path |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
3 |
|
public function getBaseName($filePath) |
130
|
|
|
{ |
131
|
3 |
|
return pathinfo($filePath, PATHINFO_FILENAME); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the name of a given file. |
136
|
|
|
* |
137
|
|
|
* @param string $filePath A file path |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
1 |
|
public function getFileName($filePath) |
142
|
|
|
{ |
143
|
1 |
|
return pathinfo($filePath, PATHINFO_BASENAME); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the parent directory of a given file. |
148
|
|
|
* |
149
|
|
|
* @param string $filePath A file path |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
15 |
|
public function getFolderPath($filePath) |
154
|
|
|
{ |
155
|
15 |
|
return pathinfo($filePath, PATHINFO_DIRNAME); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the extension of a given file. |
160
|
|
|
* |
161
|
|
|
* @param string $filename A file path |
162
|
|
|
* |
163
|
|
|
* @return string The extension of the file |
164
|
|
|
*/ |
165
|
35 |
|
public function getExtension($filename) |
166
|
|
|
{ |
167
|
35 |
|
return pathinfo($filename, PATHINFO_EXTENSION); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the MIME type of a file. |
172
|
|
|
* |
173
|
|
|
* @param string $filePath |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getMimeType($filePath) |
178
|
|
|
{ |
179
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
180
|
|
|
$mimeType = finfo_file($finfo, $filePath); |
181
|
|
|
finfo_close($finfo); |
182
|
|
|
|
183
|
|
|
return $mimeType; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Check whether or not if a given path is a directory. |
188
|
|
|
* |
189
|
|
|
* @param string $folderPath |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function isDir($folderPath) |
194
|
|
|
{ |
195
|
|
|
return is_dir($folderPath); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check whether or not a given path is a file. |
200
|
|
|
* |
201
|
|
|
* @param string $filePath |
202
|
|
|
* |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public function isFile($filePath) |
206
|
|
|
{ |
207
|
|
|
return is_file($filePath); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Check whether a given file path is a symlink. |
212
|
|
|
* |
213
|
|
|
* @param string $filePath |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function isSymlink($filePath) |
218
|
|
|
{ |
219
|
|
|
return is_link($filePath); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the full path to the file without the extension. |
224
|
|
|
* |
225
|
|
|
* @param string $filename A file path |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
3 |
|
public function removeExtension($filename) |
230
|
|
|
{ |
231
|
3 |
|
return $this->appendPath( |
232
|
3 |
|
$this->getFolderPath($filename), |
233
|
3 |
|
$this->getBaseName($filename) |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
6 |
|
public function path($path) |
238
|
|
|
{ |
239
|
6 |
|
return new FilesystemPath($path); |
240
|
|
|
} |
241
|
|
|
|
242
|
2 |
|
public function getInternalResource($name) |
243
|
|
|
{ |
244
|
2 |
|
$path = new FilesystemPath(__DIR__ . '/../Resources/' . $name); |
245
|
|
|
|
246
|
2 |
|
return file_get_contents($path); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* A vfsStream friendly way of getting the realpath() of something. |
251
|
|
|
* |
252
|
|
|
* @param string $path |
253
|
|
|
* |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
180 |
|
public function realpath($path) |
257
|
|
|
{ |
258
|
180 |
|
return $this->isVFS($path) ? $path : realpath($path); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Check whether a given path is on the virtual filesystem. |
263
|
|
|
* |
264
|
|
|
* @param string $path |
265
|
|
|
* |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
186 |
|
public function isVFS($path) |
269
|
|
|
{ |
270
|
186 |
|
return substr($path, 0, 6) == 'vfs://'; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|