-
Notifications
You must be signed in to change notification settings - Fork 59
/
FilepathLoader.php
53 lines (43 loc) · 1.65 KB
/
FilepathLoader.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
<?php
namespace SilverStripe\GraphQL\Schema\BulkLoader;
use SilverStripe\Control\Director;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Path;
/**
* Loads classes by fuzzy match (glob), relative to the root e.g. `src/*.model.php`
*/
class FilepathLoader extends AbstractBulkLoader
{
const IDENTIFIER = 'filepathLoader';
public static function getIdentifier(): string
{
return self::IDENTIFIER;
}
public function collect(Collection $collection): Collection
{
$newCollection = parent::collect($collection);
$includedFiles = [];
$excludedFiles = [];
foreach ($this->includeList as $include) {
$resolvedDir = ModuleResourceLoader::singleton()->resolvePath($include);
$absGlob = Director::is_absolute($include) ? $include : Path::join(BASE_PATH, $resolvedDir);
foreach (glob(Path::join($absGlob) ?? '') as $path) {
$includedFiles[$path] = true;
}
}
foreach ($this->excludeList as $exclude) {
$resolvedDir = ModuleResourceLoader::singleton()->resolvePath($exclude);
$absGlob = Director::is_absolute($exclude) ? $exclude : Path::join(BASE_PATH, $resolvedDir);
foreach (glob(Path::join($absGlob) ?? '') as $path) {
$excludedFiles[$path] = true;
}
}
foreach ($collection->getFiles() as $file) {
$isIncluded = isset($includedFiles[$file]) && !isset($excludedFiles[$file]);
if (!$isIncluded) {
$newCollection->removeFile($file);
}
}
return $newCollection;
}
}