1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arubacao\AssetCdn; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder as SymfonyFinder; |
6
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
7
|
|
|
|
8
|
|
|
class Finder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The config repository instance. |
12
|
|
|
* |
13
|
|
|
* @var Config |
14
|
|
|
*/ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Config $config |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Config $config) |
21
|
|
|
{ |
22
|
|
|
$this->config = $config; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get all of the files from the given directory (recursive). |
27
|
|
|
* |
28
|
|
|
* @return \Symfony\Component\Finder\SplFileInfo[] |
29
|
|
|
*/ |
30
|
|
|
public function getFiles() |
31
|
|
|
{ |
32
|
|
|
$pathFinder = $this->excluded(); |
33
|
|
|
$nameFinder = clone $pathFinder; |
34
|
|
|
|
35
|
|
|
$includedPaths = $this->includedPaths($pathFinder); |
36
|
|
|
$includedNames = $this->includedNames($nameFinder); |
37
|
|
|
|
38
|
|
|
return $this->mergeFileInfos($includedPaths, $includedNames); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function getBaseFinder(): SymfonyFinder |
42
|
|
|
{ |
43
|
|
|
return SymfonyFinder::create() |
44
|
|
|
->files() |
45
|
|
|
->in($this->config->getPublicPath()) |
46
|
|
|
->ignoreDotFiles($this->config->ignoreDotFiles()) |
47
|
|
|
->ignoreVCS($this->config->ignoreVCS()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Symfony\Component\Finder\Finder $pathFinder |
52
|
|
|
* @return \Symfony\Component\Finder\SplFileInfo[] |
53
|
|
|
*/ |
54
|
|
|
private function includedPaths(SymfonyFinder $pathFinder): array |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Include directories. |
58
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#location |
59
|
|
|
*/ |
60
|
|
|
$includedPaths = $this->config->getIncludedPaths(); |
61
|
|
|
foreach ($includedPaths as $path) { |
62
|
|
|
$pathFinder->path($path); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Include Files. |
67
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#file-name |
68
|
|
|
*/ |
69
|
|
|
$includedFiles = $this->config->getIncludedFiles(); |
70
|
|
|
foreach ($includedFiles as $file) { |
71
|
|
|
$pathFinder->path($file); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (empty($includedPaths) && empty($includedFiles)) { |
75
|
|
|
$pathFinder->notPath(''); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return iterator_to_array( |
79
|
|
|
$pathFinder, |
80
|
|
|
false |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param \Symfony\Component\Finder\Finder $nameFinder |
86
|
|
|
* @return \Symfony\Component\Finder\SplFileInfo[] |
87
|
|
|
*/ |
88
|
|
|
private function includedNames(SymfonyFinder $nameFinder): array |
89
|
|
|
{ |
90
|
|
|
/** |
91
|
|
|
* Include Extensions. |
92
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#file-name |
93
|
|
|
*/ |
94
|
|
|
$includedExtensions = $this->config->getIncludedExtensions(); |
95
|
|
|
foreach ($includedExtensions as $extension) { |
96
|
|
|
$nameFinder->name($extension); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Include Patterns - globs, strings, or regexes. |
101
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#file-name |
102
|
|
|
*/ |
103
|
|
|
$includedPatterns = $this->config->getIncludedPatterns(); |
104
|
|
|
foreach ($includedPatterns as $pattern) { |
105
|
|
|
$nameFinder->name($pattern); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (empty($includedExtensions) && empty($includedPatterns)) { |
109
|
|
|
$nameFinder->notPath(''); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return iterator_to_array( |
113
|
|
|
$nameFinder, |
114
|
|
|
false |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function excluded() |
119
|
|
|
{ |
120
|
|
|
$finder = $this->getBaseFinder(); |
121
|
|
|
|
122
|
|
|
/* |
123
|
|
|
* Exclude directories |
124
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#location |
125
|
|
|
*/ |
126
|
|
|
$finder->exclude($this->config->getExcludedPaths()); |
127
|
|
|
|
128
|
|
|
/* |
129
|
|
|
* Exclude Files |
130
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#file-name |
131
|
|
|
*/ |
132
|
|
|
foreach ($this->config->getExcludedFiles() as $file) { |
133
|
|
|
$finder->notPath($file); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/* |
137
|
|
|
* Exclude Extensions |
138
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#file-name |
139
|
|
|
*/ |
140
|
|
|
foreach ($this->config->getExcludedExtensions() as $pattern) { |
141
|
|
|
$finder->notName($pattern); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/* |
145
|
|
|
* Exclude Patterns - globs, strings, or regexes |
146
|
|
|
* @see http://symfony.com/doc/current/components/finder.html#file-name |
147
|
|
|
*/ |
148
|
|
|
foreach ($this->config->getExcludedPatterns() as $pattern) { |
149
|
|
|
$finder->notName($pattern); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $finder; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo[] $includedPaths |
157
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo[] $includedNames |
158
|
|
|
* @return \Symfony\Component\Finder\SplFileInfo[] |
159
|
|
|
*/ |
160
|
|
|
private function mergeFileInfos(array $includedPaths, array $includedNames): array |
161
|
|
|
{ |
162
|
|
|
return collect(array_merge($includedPaths, $includedNames)) |
163
|
|
|
->unique(function (SplFileInfo $file) { |
164
|
|
|
return $file->getPathname(); |
165
|
|
|
}) |
166
|
|
|
->values() |
167
|
|
|
->toArray(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|