From 170b0ace2c05a19c3937a15475aa7f082a86c544 Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Sun, 2 Apr 2017 11:10:36 +0100 Subject: [PATCH] [Finder] Glob wildcard while using double-star without ending slash --- src/Symfony/Component/Finder/Glob.php | 16 +++++++-- .../Component/Finder/Tests/Fixtures/one/.dot | 1 + .../Component/Finder/Tests/GlobTest.php | 34 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Finder/Tests/Fixtures/one/.dot diff --git a/src/Symfony/Component/Finder/Glob.php b/src/Symfony/Component/Finder/Glob.php index 1fd508f2581f..df8b86fa5f1a 100644 --- a/src/Symfony/Component/Finder/Glob.php +++ b/src/Symfony/Component/Finder/Glob.php @@ -60,9 +60,19 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS $firstByte = '/' === $car; - if ($firstByte && $strictWildcardSlash && isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) { - $car = $strictLeadingDot ? '/(?:(?=[^\.])[^/]++/)*' : '/(?:[^/]++/)*'; - $i += 3; + if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) { + $car = '[^/]++/'; + if (!isset($glob[$i + 3])) { + $car .= '?'; + } + + if ($strictLeadingDot) { + $car = '(?=[^\.])'.$car; + } + + $car = '/(?:'.$car.')*'; + $i += 2 + isset($glob[$i + 3]); + if ('/' === $delimiter) { $car = str_replace('/', '\\/', $car); } diff --git a/src/Symfony/Component/Finder/Tests/Fixtures/one/.dot b/src/Symfony/Component/Finder/Tests/Fixtures/one/.dot new file mode 100644 index 000000000000..1028065f8a33 --- /dev/null +++ b/src/Symfony/Component/Finder/Tests/Fixtures/one/.dot @@ -0,0 +1 @@ +.dot \ No newline at end of file diff --git a/src/Symfony/Component/Finder/Tests/GlobTest.php b/src/Symfony/Component/Finder/Tests/GlobTest.php index e9928a9a6873..d48ac1fc5452 100755 --- a/src/Symfony/Component/Finder/Tests/GlobTest.php +++ b/src/Symfony/Component/Finder/Tests/GlobTest.php @@ -58,4 +58,38 @@ public function testGlobToRegexDoubleStarNonStrictDots() $this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match); } + + public function testGlobToRegexDoubleStarWithoutLeadingSlash() + { + $finder = new Finder(); + $finder->ignoreDotFiles(false); + $regex = Glob::toRegex('/Fixtures/one/**'); + + 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/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match); + } + + public function testGlobToRegexDoubleStarWithoutLeadingSlashNotStrictLeadingDot() + { + $finder = new Finder(); + $finder->ignoreDotFiles(false); + $regex = Glob::toRegex('/Fixtures/one/**', 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('one/.dot', 'one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match); + } }