Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for "Globstar" pattern #83

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
use function array_filter;
use function array_map;
use function array_merge;
use function array_unique;
use function array_values;
use function glob;
use function is_dir;
use function is_string;
use function realpath;
use function sort;
use function stripos;
use function substr;
use AppendIterator;
use FilesystemIterator;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -92,7 +96,7 @@ private function resolveWildcards(array $paths): array
$_paths = [[]];

foreach ($paths as $path) {
if ($locals = glob($path, GLOB_ONLYDIR)) {
if ($locals = $this->globstar($path)) {
$_paths[] = array_map('\realpath', $locals);
} else {
// @codeCoverageIgnoreStart
Expand All @@ -103,4 +107,48 @@ private function resolveWildcards(array $paths): array

return array_values(array_filter(array_merge(...$_paths)));
}

/**
* @see https://gist.github.com/funkjedi/3feee27d873ae2297b8e2370a7082aad
*
* @return list<string>
*/
private function globstar(string $pattern)
{
if (stripos($pattern, '**') === false) {
$files = glob($pattern, GLOB_ONLYDIR);
} else {
$position = stripos($pattern, '**');
$rootPattern = substr($pattern, 0, $position - 1);
$restPattern = substr($pattern, $position + 2);

$patterns = [$rootPattern . $restPattern];
$rootPattern .= '/*';

while ($dirs = glob($rootPattern, GLOB_ONLYDIR)) {
$rootPattern .= '/*';

foreach ($dirs as $dir) {
$patterns[] = $dir . $restPattern;
}
}

$files = [];

foreach ($patterns as $pat) {
$files = array_merge($files, $this->globstar($pat));
}
}

if ($files !== false) {
$files = array_unique($files);
sort($files);

return $files;
}

// @codeCoverageIgnoreStart
return [];
// @codeCoverageIgnoreEnd
}
}
9 changes: 9 additions & 0 deletions tests/fixture/a/c/d/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
9 changes: 9 additions & 0 deletions tests/fixture/b/e/g/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
9 changes: 9 additions & 0 deletions tests/fixture/b/e/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
9 changes: 9 additions & 0 deletions tests/fixture/b/f/h/i/PrefixSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-file-iterator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
35 changes: 35 additions & 0 deletions tests/unit/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ public static function provider(): array
$fixtureDirectoryRealpath . '/a/c/d/Prefix.php',
$fixtureDirectoryRealpath . '/a/c/d/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/a/c/d/Suffix.php',
$fixtureDirectoryRealpath . '/a/c/d/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/i/PrefixSuffix.php',
],
__DIR__ . '/../fixture',
'',
Expand All @@ -59,6 +63,8 @@ public static function provider(): array
$fixtureDirectoryRealpath . '/b/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/i/PrefixSuffix.php',
],
__DIR__ . '/../fixture',
'',
Expand All @@ -76,11 +82,15 @@ public static function provider(): array
$fixtureDirectoryRealpath . '/a/c/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/a/c/d/Prefix.php',
$fixtureDirectoryRealpath . '/a/c/d/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/a/c/d/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/i/PrefixSuffix.php',
],
__DIR__ . '/../fixture',
'',
Expand All @@ -95,11 +105,15 @@ public static function provider(): array
$fixtureDirectoryRealpath . '/a/c/Suffix.php',
$fixtureDirectoryRealpath . '/a/c/d/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/a/c/d/Suffix.php',
$fixtureDirectoryRealpath . '/a/c/d/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/i/PrefixSuffix.php',
],
__DIR__ . '/../fixture',
'Suffix.php',
Expand All @@ -115,12 +129,33 @@ public static function provider(): array
$fixtureDirectoryRealpath . '/a/c/d/Prefix.php',
$fixtureDirectoryRealpath . '/a/c/d/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/a/c/d/Suffix.php',
$fixtureDirectoryRealpath . '/a/c/d/i/PrefixSuffix.php',
],
__DIR__ . '/../fixture/*/c',
'',
'',
[],
],
'globstar, filter prefix: no, filter suffix: no, excludes: none' => [
[

$fixtureDirectoryRealpath . '/a/c/d/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/g/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/e/i/PrefixSuffix.php',
$fixtureDirectoryRealpath . '/b/f/h/i/PrefixSuffix.php',
],
__DIR__ . '/../fixture/**/i',
'',
'',
[],
],
'invalid path, filter prefix: no, filter suffix: no, excludes: none' => [
[],
__DIR__ . '/../fixture/**/this/path/does/not/exists',
'',
'',
[],
],
];
}

Expand Down