-
Notifications
You must be signed in to change notification settings - Fork 824
/
ManifestFileFinder.php
304 lines (268 loc) · 9.19 KB
/
ManifestFileFinder.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace SilverStripe\Core\Manifest;
use RuntimeException;
use SilverStripe\Assets\FileFinder;
use SilverStripe\Dev\Deprecation;
/**
* An extension to the default file finder with some extra filters to facilitate
* autoload and template manifest generation:
* - Only modules with _config.php files are scanned.
* - If a _manifest_exclude file is present inside a directory it is ignored.
* - Assets and module language directories are ignored.
* - Module tests directories are skipped if either of these conditions is meant:
* - the `ignore_tests` option is not set to false.
* - the module PHP CI configuration matches one of the `ignored_ci_configs`
*/
class ManifestFileFinder extends FileFinder
{
const CONFIG_FILE = '_config.php';
const CONFIG_DIR = '_config';
const EXCLUDE_FILE = '_manifest_exclude';
const LANG_DIR = 'lang';
const TESTS_DIR = 'tests';
const VENDOR_DIR = 'vendor';
/**
* @deprecated 4.4.0:5.0.0 Use global `RESOURCES_DIR` instead.
*/
const RESOURCES_DIR = RESOURCES_DIR;
protected static $default_options = [
'include_themes' => false,
'ignore_tests' => true,
'min_depth' => 1,
'ignore_dirs' => ['node_modules'],
'ignored_ci_configs' => []
];
public function acceptDir($basename, $pathname, $depth)
{
// Skip if ignored
if ($this->isInsideIgnored($basename, $pathname, $depth)) {
return false;
}
// Keep searching inside vendor
$inVendor = $this->isInsideVendor($basename, $pathname, $depth);
if ($inVendor) {
// Skip nested vendor folders (e.g. vendor/silverstripe/framework/vendor)
if ($depth == 4 && basename($pathname ?? '') === self::VENDOR_DIR) {
return false;
}
// Keep searching if we could have a subdir module
if ($depth < 3) {
return true;
}
// Stop searching if we are in a non-module library
$libraryPath = $this->upLevels($pathname, $depth - 3);
$libraryBase = basename($libraryPath ?? '');
if (!$this->isDirectoryModule($libraryBase, $libraryPath, 3)) {
return false;
}
}
// Include themes
if ($this->getOption('include_themes') && $this->isInsideThemes($basename, $pathname, $depth)) {
return true;
}
// Skip if not in module
if (!$this->isInsideModule($basename, $pathname, $depth)) {
return false;
}
// Skip if test dir inside vendor module with unexpected CI Configuration
if ($depth > 3 && $basename === self::TESTS_DIR && $ignoredCIConfig = $this->getOption('ignored_ci_configs')) {
$ciLib = $this->findModuleCIPhpConfiguration($basename, $pathname, $depth);
if (in_array($ciLib, $ignoredCIConfig ?? [])) {
return false;
}
}
return parent::acceptDir($basename, $pathname, $depth);
}
/**
* Check if the given dir is, or is inside the vendor folder
*
* @param string $basename
* @param string $pathname
* @param int $depth
* @return bool
*/
public function isInsideVendor($basename, $pathname, $depth)
{
$base = basename($this->upLevels($pathname, $depth - 1) ?? '');
return $base === self::VENDOR_DIR;
}
/**
* Check if the given dir is, or is inside the themes folder
*
* @param string $basename
* @param string $pathname
* @param int $depth
* @return bool
*/
public function isInsideThemes($basename, $pathname, $depth)
{
$base = basename($this->upLevels($pathname, $depth - 1) ?? '');
return $base === THEMES_DIR;
}
/**
* Check if this folder or any parent is ignored
*
* @param string $basename
* @param string $pathname
* @param int $depth
* @return bool
*/
public function isInsideIgnored($basename, $pathname, $depth)
{
return $this->anyParents($basename, $pathname, $depth, function ($basename, $pathname, $depth) {
return $this->isDirectoryIgnored($basename, $pathname, $depth);
});
}
/**
* Check if this folder is inside any module
*
* @param string $basename
* @param string $pathname
* @param int $depth
* @return bool
*/
public function isInsideModule($basename, $pathname, $depth)
{
return $this->anyParents($basename, $pathname, $depth, function ($basename, $pathname, $depth) {
return $this->isDirectoryModule($basename, $pathname, $depth);
});
}
/**
* Check if any parents match the given callback
*
* @param string $basename
* @param string $pathname
* @param int $depth
* @param callable $callback
* @return bool
*/
protected function anyParents($basename, $pathname, $depth, $callback)
{
// Check all ignored dir up the path
while ($depth >= 0) {
$ignored = $callback($basename, $pathname, $depth);
if ($ignored) {
return true;
}
$pathname = dirname($pathname ?? '');
$basename = basename($pathname ?? '');
$depth--;
}
return false;
}
/**
* Check if the given dir is a module root (not a subdir)
*
* @param string $basename
* @param string $pathname
* @param string $depth
* @return bool
*/
public function isDirectoryModule($basename, $pathname, $depth)
{
// Depth can either be 0, 1, or 3 (if and only if inside vendor)
$inVendor = $this->isInsideVendor($basename, $pathname, $depth);
if ($depth > 0 && $depth !== ($inVendor ? 3 : 1)) {
return false;
}
// True if config file exists
if (file_exists($pathname . '/' . self::CONFIG_FILE)) {
return true;
}
// True if config dir exists
if (file_exists($pathname . '/' . self::CONFIG_DIR)) {
return true;
}
return false;
}
/**
* Get a parent path the given levels above
*
* @param string $pathname
* @param int $depth Number of parents to rise
* @return string
*/
protected function upLevels($pathname, $depth)
{
if ($depth < 0) {
return null;
}
while ($depth--) {
$pathname = dirname($pathname ?? '');
}
return $pathname;
}
/**
* Get all ignored directories
*
* @return array
*/
protected function getIgnoredDirs()
{
$ignored = [self::LANG_DIR, 'node_modules'];
if ($this->getOption('ignore_tests')) {
$ignored[] = self::TESTS_DIR;
}
return $ignored;
}
/**
* Check if the given directory is ignored
* @param string $basename
* @param string $pathname
* @param string $depth
* @return bool
*/
public function isDirectoryIgnored($basename, $pathname, $depth)
{
// Don't ignore root
if ($depth === 0) {
return false;
}
// Check if manifest-ignored is present
if (file_exists($pathname . '/' . self::EXCLUDE_FILE)) {
return true;
}
// Check if directory name is ignored
$ignored = $this->getIgnoredDirs();
if (in_array($basename, $ignored ?? [])) {
return true;
}
// Ignore these dirs in the root only
if ($depth === 1 && in_array($basename, [ASSETS_DIR, RESOURCES_DIR])) {
return true;
}
return false;
}
/**
* Find out the root of the current module and read the PHP CI configuration from tho composer file
*
* @param string $basename Name of the current folder
* @param string $pathname Full path the parent folder
* @param string $depth Depth of the current folder
*/
private function findModuleCIPhpConfiguration(string $basename, string $pathname, int $depth): string
{
if ($depth < 1) {
// We went all the way back to the root of the project
return Module::CI_UNKNOWN;
}
// We pop the current folder and use the next entry the pathname
$newBasename = basename($pathname ?? '');
$newPathname = dirname($pathname ?? '');
$newDepth = $depth - 1;
if ($this->isDirectoryModule($newBasename, $newPathname, $newDepth)) {
// We've reached the root of the module folder, we can read the PHP CI config now
$module = new Module($newPathname, $this->upLevels($newPathname, $newDepth));
$config = Deprecation::withNoReplacement(function () use ($module) {
return $module->getCIConfig();
});
if (empty($config['PHP'])) {
// This should never happen
throw new RuntimeException('Module::getCIConfig() did not return a PHP CI value');
}
return $config['PHP'];
}
// We haven't reach our module root yet ... let's look up one more level
return $this->findModuleCIPhpConfiguration($newBasename, $newPathname, $newDepth);
}
}