|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
|
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Vaimo\ComposerPatches\Utils; |
|
7
|
|
|
|
|
8
|
|
|
class FileSystemUtils |
|
9
|
|
|
{ |
|
10
|
|
|
public function collectFilePathsRecursively($rootPath, $pattern) |
|
11
|
|
|
{ |
|
12
|
|
|
if (strpos($rootPath, '*') !== false || strpos($rootPath, '?') !== false) { |
|
13
|
|
|
$paths = $this->collectGlobs($rootPath, $pattern); |
|
14
|
|
|
|
|
15
|
|
|
return array_filter($paths, function ($item) { |
|
16
|
|
|
return is_file($item); |
|
17
|
|
|
}); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$paths = $this->collectPathsRecursively($rootPath, $pattern); |
|
21
|
|
|
|
|
22
|
|
|
return array_filter($paths, function ($item) { |
|
23
|
|
|
return is_file($item); |
|
24
|
|
|
}); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function collectGlobs($rootPath, $pattern) |
|
28
|
|
|
{ |
|
29
|
|
|
$iterator = new \GlobIterator($rootPath); |
|
30
|
|
|
|
|
31
|
|
|
$files = array(); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($iterator as $info) { |
|
34
|
|
|
if ($info->isDir()) { |
|
35
|
|
|
$files = array_merge($files, $this->collectPathsRecursively($info->getRealPath(), $pattern)); |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!$info->isFile() || !preg_match($pattern, $info->getRealPath())) { |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$path = $info->getRealPath(); |
|
44
|
|
|
$files[$path] = $path; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$sequence = array_keys($files); |
|
48
|
|
|
|
|
49
|
|
|
natsort($sequence); |
|
50
|
|
|
|
|
51
|
|
|
return array_replace( |
|
52
|
|
|
array_flip($sequence), |
|
53
|
|
|
$files |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function collectPathsRecursively($rootPath, $pattern) |
|
58
|
|
|
{ |
|
59
|
|
|
if (!is_dir($rootPath)) { |
|
60
|
|
|
return array(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$directoryIterator = new \RecursiveDirectoryIterator( |
|
64
|
|
|
$rootPath, |
|
65
|
|
|
\RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator); |
|
69
|
|
|
|
|
70
|
|
|
$filesIterator = new \RegexIterator( |
|
71
|
|
|
$recursiveIterator, |
|
72
|
|
|
$pattern, |
|
73
|
|
|
\RecursiveRegexIterator::GET_MATCH |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$files = array(); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($filesIterator as $info) { |
|
79
|
|
|
$path = reset($info); |
|
80
|
|
|
$files[substr($path, strlen($rootPath) + 1)] = $path; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$sequence = array_keys($files); |
|
84
|
|
|
|
|
85
|
|
|
natsort($sequence); |
|
86
|
|
|
|
|
87
|
|
|
return array_replace( |
|
88
|
|
|
array_flip($sequence), |
|
89
|
|
|
$files |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|