8000 [Finder] Add double-star matching to Glob::toRegex() · symfony/symfony@8b28afa · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b28afa

Browse files
[Finder] Add double-star matching to Glob::toRegex()
1 parent 2697cee commit 8b28afa

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

src/Symfony/Component/Finder/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added double-star matching to Glob::toRegex()
8+
49
3.0.0
510
-----
611

src/Symfony/Component/Finder/Glob.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
5454
$sizeGlob = strlen($glob);
5555
for ($i = 0; $i < $sizeGlob; ++$i) {
5656
$car = $glob[$i];
57-
if ($firstByte) {
58-
if ($strictLeadingDot && '.' !== $car) {
59-
$regex .= '(?=[^\.])';
60-
}
61-
62-
$firstByte = false;
57+
if ($firstByte && $strictLeadingDot && '.' !== $car) {
58+
$regex .= '(?=[^\.])';
6359
}
6460

65-
if ('/' === $car) {
66-
$firstByte = true;
61+
$firstByte = '/' === $car;
62+
63+
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
64+
$car = $strictLeadingDot ? '/((?=[^\.])[^/]+/)*' : '/([^/]+/)*';
65+
$i += 3;
66+
if ('/' === $delimiter) {
67+
$car = str_replace('/', '\\/', $car);
68+
}
6769
}
6870

6971
if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {

src/Symfony/Component/Finder/Tests/Fixtures/.dot/a

Whitespace-only changes.

src/Symfony/Component/Finder/Tests/Fixtures/.dot/b/c.neon

Whitespace-only changes.

src/Symfony/Component/Finder/Tests/Fixtures/.dot/b/d.neon

Whitespace-only changes.

src/Symfony/Component/Finder/Tests/GlobTest.php

Lines changed: 35 additions & 0 deletions
5A4D
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Finder\Tests;
1313

14+
use Symfony\Component\Finder\Finder;
1415
use Symfony\Component\Finder\Glob;
1516

1617
class GlobTest extends \PHPUnit_Framework_TestCase
@@ -22,4 +23,38 @@ public function testGlobToRegexDelimiters()
2223
$this->assertEquals('^\.[^/]*$', Glob::toRegex('.*', true, true, ''));
2324
$this->assertEquals('/^\.[^/]*$/', Glob::toRegex('.*', true, true, '/'));
2425
}
26+
27+
public function testGlobToRegexDoubleStarStrictDots()
28+
{
29+
$finder = new Finder();
30+
$finder->ignoreDotFiles(false);
31+
$regex = Glob::toRegex('/**/*.neon');
32+
33+
foreach ($finder->in(__DIR__) as $k => $v) {
34+
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
35+
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
36+
$match[] = substr($k, 10 + strlen(__DIR__));
37+
}
38+
}
39+
sort($match);
40+
41+
$this->assertSame(array('one/b/c.neon', 'one/b/d.neon'), $match);
42+
}
43+
44+
public function testGlobToRegexDoubleStarNonStrictDots()
45+
{
46+
$finder = new Finder();
47+
$finder->ignoreDotFiles(false);
48+
$regex = Glob::toRegex('/**/*.neon', false);
49+
50+
foreach ($finder->in(__DIR__) as $k => $v) {
51+
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
52+
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
53+
$match[] = substr($k, 10 + strlen(__DIR__));
54+
}
55+
}
56+
sort($match);
57+
58+
$this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match);
59+
}
2560
}

0 commit comments

Comments
 (0)
0