-
Notifications
You must be signed in to change notification settings - Fork 1
/
Filesystem.php
277 lines (241 loc) · 8.26 KB
/
Filesystem.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Filesystem;
use Cake\Core\Exception\CakeException;
use CallbackFilterIterator;
use FilesystemIterator;
use Iterator;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
use SplFileInfo;
/**
* This provides convenience wrappers around common filesystem queries.
*
* This is an internal helper class that should not be used in application code
* as it provides no guarantee for compatibility.
*
* @internal
*/
class Filesystem
{
/**
* Directory type constant
*
* @var string
*/
public const TYPE_DIR = 'dir';
/**
* Find files / directories (non-recursively) in given directory path.
*
* @param string $path Directory path.
* @param mixed $filter If string will be used as regex for filtering using
* `RegexIterator`, if callable will be as callback for `CallbackFilterIterator`.
* @param int|null $flags Flags for FilesystemIterator::__construct();
* @return \Iterator
*/
public function find(string $path, $filter = null, ?int $flags = null): Iterator
{
$flags = $flags ?? FilesystemIterator::KEY_AS_PATHNAME
| FilesystemIterator::CURRENT_AS_FILEINFO
| FilesystemIterator::SKIP_DOTS;
$directory = new FilesystemIterator($path, $flags);
if ($filter === null) {
return $directory;
}
return $this->filterIterator($directory, $filter);
}
/**
* Find files/ directories recursively in given directory path.
*
* @param string $path Directory path.
* @param mixed $filter If string will be used as regex for filtering using
* `RegexIterator`, if callable will be as callback for `CallbackFilterIterator`.
* Hidden directories (starting with dot e.g. .git) are always skipped.
* @param int|null $flags Flags for FilesystemIterator::__construct();
* @return \Iterator
*/
public function findRecursive(string $path, $filter = null, ?int $flags = null): Iterator
{
$flags = $flags ?? FilesystemIterator::KEY_AS_PATHNAME
| FilesystemIterator::CURRENT_AS_FILEINFO
| FilesystemIterator::SKIP_DOTS;
$directory = new RecursiveDirectoryIterator($path, $flags);
$dirFilter = new RecursiveCallbackFilterIterator(
$directory,
function (SplFileInfo $current) {
if ($current->getFilename()[0] === '.' && $current->isDir()) {
return false;
}
return true;
}
);
$flatten = new RecursiveIteratorIterator(
$dirFilter,
RecursiveIteratorIterator::CHILD_FIRST
);
if ($filter === null) {
return $flatten;
}
return $this->filterIterator($flatten, $filter);
}
/**
* Wrap iterator in additional filtering iterator.
*
* @param \Iterator $iterator Iterator
* @param mixed $filter Regex string or callback.
* @return \Iterator
*/
protected function filterIterator(Iterator $iterator, $filter): Iterator
{
if (is_string($filter)) {
return new RegexIterator($iterator, $filter);
}
return new CallbackFilterIterator($iterator, $filter);
}
/**
* Dump contents to file.
*
* @param string $filename File path.
* @param string $content Content to dump.
* @return void
* @throws \Cake\Core\Exception\CakeException When dumping fails.
*/
public function dumpFile(string $filename, string $content): void
{
$dir = dirname($filename);
if (!is_dir($dir)) {
$this->mkdir($dir);
}
$exists = file_exists($filename);
if ($this->isStream($filename)) {
// phpcs:ignore
$success = @file_put_contents($filename, $content);
} else {
// phpcs:ignore
$success = @file_put_contents($filename, $content, LOCK_EX);
}
if ($success === false) {
throw new CakeException(sprintf('Failed dumping content to file `%s`', $dir));
}
if (!$exists) {
chmod($filename, 0666 & ~umask());
}
}
/**
* Create directory.
*
* @param string $dir Directory path.
* @param int $mode Octal mode passed to mkdir(). Defaults to 0755.
* @return void
* @throws \Cake\Core\Exception\CakeException When directory creation fails.
*/
public function mkdir(string $dir, int $mode = 0755): void
{
if (is_dir($dir)) {
return;
}
$old = umask(0);
// phpcs:ignore
if (@mkdir($dir, $mode, true) === false) {
umask($old);
throw new CakeException(sprintf('Failed to create directory "%s"', $dir));
}
umask($old);
}
/**
* Delete directory along with all it's contents.
*
* @param string $path Directory path.
* @return bool
* @throws \Cake\Core\Exception\CakeException If path is not a directory.
*/
public function deleteDir(string $path): bool
{
if (!file_exists($path)) {
return true;
}
if (!is_dir($path)) {
throw new CakeException(sprintf('"%s" is not a directory', $path));
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
$result = true;
foreach ($iterator as $fileInfo) {
$isWindowsLink = DIRECTORY_SEPARATOR === '\\' && $fileInfo->getType() === 'link';
if ($fileInfo->getType() === self::TYPE_DIR || $isWindowsLink) {
// phpcs:ignore
$result = $result && @rmdir($fileInfo->getPathname());
unset($fileInfo);
continue;
}
// phpcs:ignore
$result = $result && @unlink($fileInfo->getPathname());
// possible inner iterators need to be unset too in order for locks on parents to be released
unset($fileInfo);
}
// unsetting iterators helps releasing possible locks in certain environments,
// which could otherwise make `rmdir()` fail
unset($iterator);
// phpcs:ignore
$result = $result && @rmdir($path);
return $result;
}
/**
* Copies directory with all it's contents.
*
* @param string $source Source path.
* @param string $destination Destination path.
* @return bool
*/
public function copyDir(string $source, string $destination): bool
{
$destination = (new SplFileInfo($destination))->getPathname();
if (!is_dir($destination)) {
$this->mkdir($destination);
}
$iterator = new FilesystemIterator($source);
$result = true;
foreach ($iterator as $fileInfo) {
if ($fileInfo->isDir()) {
$result = $result && $this->copyDir(
$fileInfo->getPathname(),
$destination . DIRECTORY_SEPARATOR . $fileInfo->getFilename()
);
} else {
// phpcs:ignore
$result = $result && @copy(
$fileInfo->getPathname(),
$destination . DIRECTORY_SEPARATOR . $fileInfo->getFilename()
);
}
}
return $result;
}
/**
* Check whether the given path is a stream path.
*
* @param string $path Path.
* @return bool
*/
public function isStream(string $path): bool
{
return strpos($path, '://') !== false;
}
}