Skip to content

Commit ed2a76d

Browse files
committed
implement globstar
1 parent 502fd07 commit ed2a76d

File tree

9 files changed

+129
-2
lines changed

9 files changed

+129
-2
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"php-di/php-di": "^6.4",
1010
"dusank/knapsack": "^10.0",
1111
"php-di/slim-bridge": "^3.4",
12-
"symfony/console": "^5.4"
12+
"symfony/console": "^5.4",
13+
"symfony/finder": "^5.4"
1314
},
1415
"license": "MIT",
1516
"autoload": {

composer.lock

+64-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions.php

+19
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@
55

66
use DusanKasan\Knapsack\Collection;
77
use RuntimeException;
8+
use SplFileInfo;
9+
use Symfony\Component\Finder\Finder;
810

911
/** @return string[] */
1012
function findFiles(string $root, string ...$patterns): iterable {
13+
$globstar = '**/';
14+
1115
$root = rtrim($root, '/');
1216
foreach ($patterns as $pattern) {
17+
// php glob does not support globstar to match any directory depth. Let’s hack around it
18+
if (str_contains($pattern, $globstar)) {
19+
[$directory, $name] = explode($globstar, $pattern, 2);
20+
$finder = Finder::create()
21+
->in($root.'/'.ltrim($directory, '/'))
22+
->name($name)
23+
->files()
24+
->sortByName();
25+
yield from map(
26+
array_values(iterator_to_array($finder)),
27+
static fn (SplFileInfo $file) => $file->getRealPath(),
28+
);
29+
continue;
30+
}
31+
1332
$result = glob($root.'/'.ltrim($pattern, '/'));
1433
if (false === $result) {
1534
throw new RuntimeException('Invalid pattern: '.$pattern);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

tests/ServicesBuilderTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace WonderNetwork\SlimKernel;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function WonderNetwork\SlimKernel\Collection\collection;
8+
use function WonderNetwork\SlimKernel\Collection\map;
9+
10+
final class ServicesBuilderTest extends TestCase {
11+
public function testSimpleGlob(): void {
12+
$this->assertFiles('*.php', ['alpha.php', 'bravo.php']);
13+
}
14+
15+
public function testRecursiveGlob(): void {
16+
$this->assertFiles(
17+
'**/*.php',
18+
[
19+
'alpha.php',
20+
'bravo.php',
21+
'charlie/delta.php',
22+
'charlie/echo/foxtrot.php',
23+
],
24+
);
25+
}
26+
27+
/**
28+
* @param string $pattern
29+
* @param string[] $expected
30+
*/
31+
private function assertFiles(string $pattern, array $expected): void {
32+
$basePath = __DIR__.'/Resources/ServicesBuilder/';
33+
$sut = new ServicesBuilder($basePath);
34+
$actual = collection($sut->glob($pattern))->toArray();
35+
$expected = map($expected, static fn (string $path) => $basePath.$path);
36+
37+
self::assertSame($expected, $actual);
38+
}
39+
}

0 commit comments

Comments
 (0)