|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Api; |
|
4
|
|
|
|
|
5
|
|
|
class FindFiles |
|
6
|
|
|
{ |
|
7
|
|
|
protected $myMu = null; |
|
8
|
|
|
|
|
9
|
|
|
protected $defaultOptions = ''; |
|
10
|
|
|
|
|
11
|
|
|
protected static $inst = null; |
|
12
|
|
|
|
|
13
|
|
|
//generic search settings |
|
14
|
|
|
|
|
15
|
|
|
private $needToFillFileCache = true; |
|
16
|
|
|
|
|
17
|
|
|
private $searchPath = ''; |
|
18
|
|
|
|
|
19
|
|
|
private $relevantFolders = []; |
|
20
|
|
|
|
|
21
|
|
|
private $defaultIgnoreFolderArray = [ |
|
22
|
|
|
'.svn', |
|
23
|
|
|
'.git', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
private $ignoreFolderArray = []; |
|
27
|
|
|
|
|
28
|
|
|
private $extensions = ['php', 'ss', 'yml', 'yaml', 'json', 'js', 'md']; |
|
29
|
|
|
|
|
30
|
|
|
private $findAllExts = false; |
|
31
|
|
|
|
|
32
|
|
|
private $recursive = true; |
|
33
|
|
|
|
|
34
|
|
|
private $ignoreHiddenFilesAndFolders = true; |
|
35
|
|
|
|
|
36
|
|
|
// files |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* array of all the files we are searching |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $fileArray = []; |
|
43
|
|
|
|
|
44
|
|
|
private $flatFileArray = []; |
|
45
|
|
|
|
|
46
|
|
|
public static function inst(): self |
|
47
|
|
|
{ |
|
48
|
|
|
if (self::$inst === null) { |
|
49
|
|
|
self::$inst = new FindFiles(); |
|
50
|
|
|
} |
|
51
|
|
|
return self::$inst; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets folders to ignore |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setIgnoreFolderArray(?array $ignoreFolderArray = []): FindFiles |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->ignoreFolderArray === $ignoreFolderArray) { |
|
60
|
|
|
//do nothing |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->ignoreFolderArray = $ignoreFolderArray; |
|
63
|
|
|
$this->resetFileCache(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets folders to ignore |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addToIgnoreFolderArray(?array $ignoreFolderArray = []): FindFiles |
|
73
|
|
|
{ |
|
74
|
|
|
$oldIgnoreFolderArray = $this->ignoreFolderArray; |
|
75
|
|
|
$this->ignoreFolderArray = array_unique( |
|
76
|
|
|
array_merge( |
|
77
|
|
|
$ignoreFolderArray, |
|
78
|
|
|
$this->defaultIgnoreFolderArray |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
if ($oldIgnoreFolderArray !== $this->ignoreFolderArray) { |
|
82
|
|
|
$this->resetFileCache(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* remove ignore folders |
|
90
|
|
|
*/ |
|
91
|
|
|
public function resetIgnoreFolderArray(): FindFiles |
|
92
|
|
|
{ |
|
93
|
|
|
$this->ignoreFolderArray = []; |
|
94
|
|
|
$this->resetFileCache(); |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Sets extensions to look |
|
101
|
|
|
* @param bool $boolean - optional |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setFindAllExts(bool $boolean = true): FindFiles |
|
104
|
|
|
{ |
|
105
|
|
|
$this->findAllExts = $boolean; |
|
106
|
|
|
$this->resetFileCache(); |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setSearchPath(string $pathLocation): FindFiles |
|
112
|
|
|
{ |
|
113
|
|
|
if ($pathLocation !== $this->searchPath) { |
|
114
|
|
|
$this->searchPath = $pathLocation; |
|
115
|
|
|
$this->resetFileCache(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets extensions to look |
|
123
|
|
|
* @param array $extensions - optional |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setExtensions(?array $extensions = []): FindFiles |
|
126
|
|
|
{ |
|
127
|
|
|
$this->extensions = $extensions; |
|
128
|
|
|
if (count($this->extensions)) { |
|
129
|
|
|
$this->findAllExts = false; |
|
130
|
|
|
} else { |
|
131
|
|
|
$this->findAllExts = true; |
|
132
|
|
|
} |
|
133
|
|
|
$this->resetFileCache(); |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function setRecursive(bool $bool): FindFiles |
|
139
|
|
|
{ |
|
140
|
|
|
$this->recursive = $bool; |
|
141
|
|
|
$this->resetFileCache(); |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function setIgnoreHiddenFilesAndFolders(bool $bool): FindFiles |
|
147
|
|
|
{ |
|
148
|
|
|
$this->ignoreHiddenFilesAndFolders = $bool; |
|
149
|
|
|
$this->resetFileCache(); |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* string is error! |
|
156
|
|
|
* @return array|string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getFlatFileArray() |
|
159
|
|
|
{ |
|
160
|
|
|
if ($this->needToFillFileCache) { |
|
161
|
|
|
$myArray = []; |
|
162
|
|
|
if ($this->searchPath) { |
|
163
|
|
|
if (file_exists($this->searchPath)) { |
|
164
|
|
|
if (is_file($this->searchPath)) { |
|
165
|
|
|
$this->flatFileArray = [$this->searchPath]; |
|
166
|
|
|
} else { |
|
167
|
|
|
$multiDimensionalArray = $this->getFileArray($this->searchPath); |
|
168
|
|
|
foreach ($multiDimensionalArray as $folder => $arrayOfFiles) { |
|
169
|
|
|
if (count($arrayOfFiles)) { |
|
170
|
|
|
$this->relevantFolders[$folder] = $folder; |
|
171
|
|
|
} |
|
172
|
|
|
foreach ($arrayOfFiles as $file) { |
|
173
|
|
|
$myArray[$file] = $file; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
// //flatten it! |
|
177
|
|
|
// $this->flatFileArray = new \RecursiveIteratorIterator( |
|
178
|
|
|
// new \RecursiveArrayIterator($multiDimensionalArray) |
|
179
|
|
|
// ); |
|
180
|
|
|
// print_r($this->flatFileArray); |
|
181
|
|
|
} |
|
182
|
|
|
} else { |
|
183
|
|
|
return 'SKIPPED: can not find: ' . $this->searchPath . "\n"; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
$this->flatFileArray = array_values($myArray); |
|
187
|
|
|
$this->needToFillFileCache = false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $this->flatFileArray; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* loads all the applicable files |
|
195
|
|
|
* @param string $path (e.g. "." or "/var/www/mysite.co.nz") |
|
196
|
|
|
* @param boolean $runningInnerLoop - is the method calling itself? |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function getFileArray($path, $runningInnerLoop = false) |
|
199
|
|
|
{ |
|
200
|
|
|
if ($runningInnerLoop || $this->needToFillFileCache) { |
|
201
|
|
|
$dir = opendir($path); |
|
202
|
|
|
if ($dir) { |
|
|
|
|
|
|
203
|
|
|
$keepGoing = true; |
|
204
|
|
|
while ($keepGoing) { |
|
205
|
|
|
$file = readdir($dir); |
|
206
|
|
|
if (! $file) { |
|
207
|
|
|
$keepGoing = false; |
|
208
|
|
|
continue; |
|
209
|
|
|
} |
|
210
|
|
|
$fullPath = $path . '/' . $file; |
|
211
|
|
|
if (($file === '.') || |
|
212
|
|
|
($file === '..') || |
|
213
|
|
|
($fullPath === __FILE__) || |
|
214
|
|
|
($path === '.' && basename(__FILE__) === $file)) { |
|
215
|
|
|
continue; |
|
216
|
|
|
} |
|
217
|
|
|
//ignore hidden files and folders |
|
218
|
|
|
if ($this->ignoreHiddenFilesAndFolders && substr($file, 0, 1) === '.') { |
|
219
|
|
|
continue; |
|
220
|
|
|
} |
|
221
|
|
|
//ignore folders with _manifest_exclude in them! |
|
222
|
|
|
if ($file === '_manifest_exclude') { |
|
223
|
|
|
$this->ignoreFolderArray[] = $path; |
|
224
|
|
|
unset($this->fileArray[$path]); |
|
225
|
|
|
break; |
|
226
|
|
|
} |
|
227
|
|
|
if (filetype($fullPath) === 'dir') { |
|
228
|
|
|
$conditionA = (in_array($file, $this->ignoreFolderArray, true) && |
|
229
|
|
|
($path === '.' || $path === $this->searchPath)); |
|
230
|
|
|
$conditionB = in_array($path, $this->ignoreFolderArray, true); |
|
231
|
|
|
if ($conditionA || $conditionB) { |
|
232
|
|
|
continue; |
|
233
|
|
|
} |
|
234
|
|
|
if ($this->recursive) { |
|
235
|
|
|
$this->getFileArray($fullPath, $runningInnerLoop = true); //recursive traversing here |
|
236
|
|
|
} |
|
237
|
|
|
} elseif ($this->matchedExtension($file)) { //checks extension if we need to search this file |
|
238
|
|
|
if (filesize($fullPath)) { |
|
239
|
|
|
$this->fileArray[$path][] = $fullPath; //search file data |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
} //End of while |
|
243
|
|
|
closedir($dir); |
|
244
|
|
|
} else { |
|
245
|
|
|
user_error('Could not find: ' . $path); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return $this->fileArray; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
//FIND FILES |
|
253
|
|
|
|
|
254
|
|
|
protected function resetFileCache() |
|
255
|
|
|
{ |
|
256
|
|
|
$this->fileArray = []; |
|
257
|
|
|
$this->flatFileArray = []; |
|
258
|
|
|
$this->needToFillFileCache = true; |
|
259
|
|
|
//cleanup other data |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Finds extension of a file |
|
264
|
|
|
* @param string $file |
|
265
|
|
|
*/ |
|
266
|
|
|
private function findExtension($file): string |
|
267
|
|
|
{ |
|
268
|
|
|
$fileArray = explode('.', $file) ?? []; |
|
269
|
|
|
|
|
270
|
|
|
return (string) array_pop($fileArray); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Checks if a file extension is one of the extensions we are going to search |
|
275
|
|
|
* @param string $file |
|
276
|
|
|
* @return boolean |
|
277
|
|
|
*/ |
|
278
|
|
|
private function matchedExtension($file) |
|
279
|
|
|
{ |
|
280
|
|
|
$fileExtension = $this->findExtension($file); |
|
281
|
|
|
if ($this->findAllExts) { |
|
282
|
|
|
return true; |
|
283
|
|
|
} elseif (in_array('*', $this->extensions, true)) { |
|
284
|
|
|
return true; |
|
285
|
|
|
} elseif (in_array($fileExtension, $this->extensions, true)) { |
|
286
|
|
|
return true; |
|
287
|
|
|
} |
|
288
|
|
|
return false; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|