1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Schnittstabil\FinderByConfig; |
4
|
|
|
|
5
|
|
|
use function Schnittstabil\Get\getValue; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
|
8
|
|
|
class FinderByConfig |
9
|
|
|
{ |
10
|
|
|
protected function createFromArray(array $paths) |
11
|
|
|
{ |
12
|
|
|
$finder = new Finder(); |
13
|
|
|
$files = []; |
14
|
|
|
$dirs = []; |
15
|
|
|
|
16
|
|
|
foreach ($paths as $path) { |
17
|
|
|
if (is_file($path)) { |
18
|
|
|
$files[] = $path; |
19
|
|
|
continue; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$dirs[] = $path; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $finder->append($files)->in($dirs); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function setWithoutValueOptions($finder, $config) |
29
|
|
|
{ |
30
|
|
|
foreach ([ |
31
|
|
|
'directories', |
32
|
|
|
'files', |
33
|
|
|
'sortByName', |
34
|
|
|
'sortByType', |
35
|
|
|
'sortByAccessedTime', |
36
|
|
|
'sortByChangedTime', |
37
|
|
|
'sortByModifiedTime', |
38
|
|
|
'followLinks', |
39
|
|
|
] as $setter) { |
40
|
|
|
if (filter_var(getValue($setter, $config), FILTER_VALIDATE_BOOLEAN)) { |
41
|
|
|
$finder->$setter(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function setArrayOptions($finder, $config) |
47
|
|
|
{ |
48
|
|
|
foreach ([ |
49
|
|
|
'depth', |
50
|
|
|
'date', |
51
|
|
|
'name', |
52
|
|
|
'notName', |
53
|
|
|
'contains', |
54
|
|
|
'notContains', |
55
|
|
|
'path', |
56
|
|
|
'notPath', |
57
|
|
|
'size', |
58
|
|
|
'exclude', |
59
|
|
|
'addVCSPattern', |
60
|
|
|
] as $setter) { |
61
|
|
|
foreach ((array) getValue($setter, $config, []) as $value) { |
62
|
|
|
$finder->$setter($value); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function setStrictBoolOptions($finder, $config) |
68
|
|
|
{ |
69
|
|
|
foreach ([ |
70
|
|
|
'ignoreDotFiles', |
71
|
|
|
'ignoreVCS', |
72
|
|
|
'ignoreUnreadableDirs', |
73
|
|
|
] as $setter) { |
74
|
|
|
$value = getValue($setter, $config); |
75
|
|
|
if ($value !== null) { |
76
|
|
|
$finder->$setter(filter_var($value, FILTER_VALIDATE_BOOLEAN)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function createFromConfig($config) |
82
|
|
|
{ |
83
|
|
|
$finder = $this->createFromArray(getValue('in', $config)); |
84
|
|
|
$this->setWithoutValueOptions($finder, $config); |
85
|
|
|
$this->setArrayOptions($finder, $config); |
86
|
|
|
$this->setStrictBoolOptions($finder, $config); |
87
|
|
|
|
88
|
|
|
return $finder; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function __invoke($config) |
92
|
|
|
{ |
93
|
|
|
if (getValue('in', $config) === null) { |
94
|
|
|
return $this->createFromArray((array) $config); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->createFromConfig($config); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function createFinder($config) |
101
|
|
|
{ |
102
|
|
|
$finderByConfig = new static(); |
103
|
|
|
|
104
|
|
|
return $finderByConfig($config); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|