Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bbb0d5e

Browse filesBrowse files
committed
feature #22239 [Finder] Glob wildcard while using double-star without ending slash (sroze)
This PR was squashed before being merged into the 3.3-dev branch (closes #22239). Discussion ---------- [Finder] Glob wildcard while using double-star without ending slash | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ø | License | MIT | Doc PR | ø This PR allows to use the `Glob::toRegex` method to dump a glob that will match "everything after" while using the double-star without leading slash. `/foo/**` will therefore match `/foo/a` or `/foo/a/b/c` or even `/foo/a/b/c/d.e`. The use-case for this change is an application allowing its users to match a list of files/folders from such glob pattern. Happy to add a line in the CHANGELOG and documentation if you feel that would make sense. Commits ------- 170b0ac [Finder] Glob wildcard while using double-star without ending slash
2 parents f7debe8 + 170b0ac commit bbb0d5e
Copy full SHA for bbb0d5e

File tree

3 files changed

+48
-3
lines changed
Filter options

3 files changed

+48
-3
lines changed

‎src/Symfony/Component/Finder/Glob.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Glob.php
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
6060

6161
$firstByte = '/' === $car;
6262

63-
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
64-
$car = $strictLeadingDot ? '/(?:(?=[^\.])[^/]++/)*' : '/(?:[^/]++/)*';
65-
$i += 3;
63+
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
64+
$car = '[^/]++/';
65+
if (!isset($glob[$i + 3])) {
66+
$car .= '?';
67+
}
68+
69+
if ($strictLeadingDot) {
70+
$car = '(?=[^\.])'.$car;
71+
}
72+
73+
$car = '/(?:'.$car.')*';
74+
$i += 2 + isset($glob[$i + 3]);
75+
6676
if ('/' === $delimiter) {
6777
$car = str_replace('/', '\\/', $car);
6878
}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.dot

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/GlobTest.php
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,38 @@ public function testGlobToRegexDoubleStarNonStrictDots()
5858

5959
$this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match);
6060
}
61+
62+
public function testGlobToRegexDoubleStarWithoutLeadingSlash()
63+
{
64+
$finder = new Finder();
65+
$finder->ignoreDotFiles(false);
66+
$regex = Glob::toRegex('/Fixtures/one/**');
67+
68+
foreach ($finder->in(__DIR__) as $k => $v) {
69+
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
70+
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
71+
$match[] = substr($k, 10 + strlen(__DIR__));
72+
}
73+
}
74+
sort($match);
75+
76+
$this->assertSame(array('one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match);
77+
}
78+
79+
public function testGlobToRegexDoubleStarWithoutLeadingSlashNotStrictLeadingDot()
80+
{
81+
$finder = new Finder();
82+
$finder->ignoreDotFiles(false);
83+
$regex = Glob::toRegex('/Fixtures/one/**', false);
84+
85+
foreach ($finder->in(__DIR__) as $k => $v) {
86+
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
87+
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
88+
$match[] = substr($k, 10 + strlen(__DIR__));
89+
}
90+
}
91+
sort($match);
92+
93+
$this->assertSame(array('one/.dot', 'one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match);
94+
}
6195
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.