File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -37,6 +37,9 @@ class Finder implements \IteratorAggregate
3737
3838 /** @var \Closure[] */
3939 private array $ descentFilters = [];
40+
41+ /** @var array<string|self> */
42+ private array $ appends = [];
4043 private bool $ childFirst = false ;
4144
4245 /** @var ?callable */
@@ -187,6 +190,20 @@ public function sortByName(): static
187190 }
188191
189192
193+ /**
194+ * Adds the specified paths or appends a new finder that returns.
195+ */
196+ public function append (string |array |null $ paths = null ): static
197+ {
198+ if ($ paths === null ) {
199+ return $ this ->appends [] = new static ;
200+ }
201+
202+ $ this ->appends = array_merge ($ this ->appends , (array ) $ paths );
203+ return $ this ;
204+ }
205+
206+
190207 /********************* filtering ****************d*g**/
191208
192209
@@ -306,6 +323,15 @@ public function getIterator(): \Generator
306323 foreach ($ plan as $ dir => $ searches ) {
307324 yield from $ this ->traverseDir ($ dir , $ searches );
308325 }
326+
327+ foreach ($ this ->appends as $ item ) {
328+ if ($ item instanceof self) {
329+ yield from $ item ->getIterator ();
330+ } else {
331+ $ item = FileSystem::platformSlashes ($ item );
332+ yield $ item => new FileInfo ($ item );
333+ }
334+ }
309335 }
310336
311337
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * Test: Nette\Utils\Finder append.
5+ */
6+
7+ declare (strict_types=1 );
8+
9+ use Nette \Utils \FileSystem ;
10+ use Nette \Utils \Finder ;
11+ use Tester \Assert ;
12+
13+
14+ require __DIR__ . '/../bootstrap.php ' ;
15+
16+
17+ test ('append finder ' , function () {
18+ ($ finder = new Finder )
19+ ->files ('file.txt ' )
20+ ->in ('fixtures.finder ' )
21+ ->append ()
22+ ->directories ('subdir* ' )
23+ ->from ('fixtures.finder ' )
24+ ->append ()
25+ ->files ('file.txt ' )
26+ ->from ('fixtures.finder/*/subdir* ' );
27+
28+ $ ds = DIRECTORY_SEPARATOR ;
29+ Assert::same ([
30+ "fixtures.finder {$ ds }file.txt " ,
31+ "fixtures.finder {$ ds }subdir " ,
32+ "fixtures.finder {$ ds }subdir {$ ds }subdir2 " ,
33+ "fixtures.finder {$ ds }subdir {$ ds }subdir2 {$ ds }file.txt " ,
34+ ], array_keys ($ finder ->collect ()));
35+ });
36+
37+ test ('append files ' , function () {
38+ ($ finder = new Finder )
39+ ->append (__FILE__ )
40+ ->append (FileSystem::unixSlashes (__DIR__ ));
41+
42+ Assert::equal ([
43+ __FILE__ => new Nette \Utils \FileInfo (__FILE__ ),
44+ __DIR__ => new Nette \Utils \FileInfo (__DIR__ ),
45+ ], $ finder ->collect ());
46+ });
You can’t perform that action at this time.
0 commit comments