Permalink
Browse files

feature #21572 [Finder] Add double-star matching to Glob::toRegex() (…

…nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Finder] Add double-star matching to Glob::toRegex()

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Adds ant-style `/**/` matching to `Glob::toRegex()`.

Commits
-------

8b28afa [Finder] Add double-star matching to Glob::toRegex()
  • Loading branch information...
2 parents bcd897c + 8b28afa commit c43b85e0c6d477e8039fbe4584392507b7ef5068 @fabpot fabpot committed Feb 9, 2017
@@ -1,6 +1,11 @@
CHANGELOG
=========
+3.3.0
+-----
+
+ * added double-star matching to Glob::toRegex()
+
3.0.0
-----
@@ -54,16 +54,18 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
$sizeGlob = strlen($glob);
for ($i = 0; $i < $sizeGlob; ++$i) {
$car = $glob[$i];
- if ($firstByte) {
- if ($strictLeadingDot && '.' !== $car) {
- $regex .= '(?=[^\.])';
- }
-
- $firstByte = false;
+ if ($firstByte && $strictLeadingDot && '.' !== $car) {
+ $regex .= '(?=[^\.])';
}
- if ('/' === $car) {
- $firstByte = true;
+ $firstByte = '/' === $car;
+
+ if ($firstByte && $strictWildcardSlash && isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
+ $car = $strictLeadingDot ? '/((?=[^\.])[^/]+/)*' : '/([^/]+/)*';
+ $i += 3;
+ if ('/' === $delimiter) {
+ $car = str_replace('/', '\\/', $car);
+ }
}
if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
@@ -11,6 +11,7 @@
namespace Symfony\Component\Finder\Tests;
+use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Glob;
class GlobTest extends \PHPUnit_Framework_TestCase
@@ -22,4 +23,38 @@ public function testGlobToRegexDelimiters()
$this->assertEquals('^\.[^/]*$', Glob::toRegex('.*', true, true, ''));
$this->assertEquals('/^\.[^/]*$/', Glob::toRegex('.*', true, true, '/'));
}
+
+ public function testGlobToRegexDoubleStarStrictDots()
+ {
+ $finder = new Finder();
+ $finder->ignoreDotFiles(false);
+ $regex = Glob::toRegex('/**/*.neon');
+
+ foreach ($finder->in(__DIR__) as $k => $v) {
+ $k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
+ if (preg_match($regex, substr($k, strlen(__DIR__)))) {
+ $match[] = substr($k, 10 + strlen(__DIR__));
+ }
+ }
+ sort($match);
+
+ $this->assertSame(array('one/b/c.neon', 'one/b/d.neon'), $match);
+ }
+
+ public function testGlobToRegexDoubleStarNonStrictDots()
+ {
+ $finder = new Finder();
+ $finder->ignoreDotFiles(false);
+ $regex = Glob::toRegex('/**/*.neon', false);
+
+ foreach ($finder->in(__DIR__) as $k => $v) {
+ $k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
+ if (preg_match($regex, substr($k, strlen(__DIR__)))) {
+ $match[] = substr($k, 10 + strlen(__DIR__));
+ }
+ }
+ sort($match);
+
+ $this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match);
+ }
}

0 comments on commit c43b85e

Please sign in to comment.