diff --git a/src/Symfony/Component/Finder/Expression/Glob.php b/src/Symfony/Component/Finder/Expression/Glob.php index 3023ceea69a3..14e8bad4fa4f 100644 --- a/src/Symfony/Component/Finder/Expression/Glob.php +++ b/src/Symfony/Component/Finder/Expression/Glob.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Finder\Expression; +use Symfony\Component\Finder\Glob as FinderGlob; + /** * @author Jean-François Simon */ @@ -100,58 +102,8 @@ public function isExpandable() */ public function toRegex($strictLeadingDot = true, $strictWildcardSlash = true) { - $firstByte = true; - $escaping = false; - $inCurlies = 0; - $regex = ''; - $sizeGlob = strlen($this->pattern); - for ($i = 0; $i < $sizeGlob; $i++) { - $car = $this->pattern[$i]; - if ($firstByte) { - if ($strictLeadingDot && '.' !== $car) { - $regex .= '(?=[^\.])'; - } - - $firstByte = false; - } - - if ('/' === $car) { - $firstByte = true; - } - - if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) { - $regex .= "\\$car"; - } elseif ('*' === $car) { - $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*'); - } elseif ('?' === $car) { - $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.'); - } elseif ('{' === $car) { - $regex .= $escaping ? '\\{' : '('; - if (!$escaping) { - ++$inCurlies; - } - } elseif ('}' === $car && $inCurlies) { - $regex .= $escaping ? '}' : ')'; - if (!$escaping) { - --$inCurlies; - } - } elseif (',' === $car && $inCurlies) { - $regex .= $escaping ? ',' : '|'; - } elseif ('\\' === $car) { - if ($escaping) { - $regex .= '\\\\'; - $escaping = false; - } else { - $escaping = true; - } - - continue; - } else { - $regex .= $car; - } - $escaping = false; - } - - return new Regex('^'.$regex.'$'); + $regex = FinderGlob::toRegex($this->pattern, $strictLeadingDot, $strictWildcardSlash, ''); + + return new Regex($regex); } } diff --git a/src/Symfony/Component/Finder/Glob.php b/src/Symfony/Component/Finder/Glob.php index c2030c9232fd..01066f61c76f 100644 --- a/src/Symfony/Component/Finder/Glob.php +++ b/src/Symfony/Component/Finder/Glob.php @@ -41,10 +41,11 @@ class Glob * @param string $glob The glob pattern * @param bool $strictLeadingDot * @param bool $strictWildcardSlash + * @param string $delimiter Optional delimiter * * @return string regex The regexp */ - public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true) + public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#') { $firstByte = true; $escaping = false; @@ -98,6 +99,6 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS $escaping = false; } - return '#^'.$regex.'$#'; + return $delimiter.'^'.$regex.'$'.$delimiter; } } diff --git a/src/Symfony/Component/Finder/Tests/FinderTest.php b/src/Symfony/Component/Finder/Tests/FinderTest.php index 4d196840a5f1..7e588857d481 100644 --- a/src/Symfony/Component/Finder/Tests/FinderTest.php +++ b/src/Symfony/Component/Finder/Tests/FinderTest.php @@ -306,7 +306,7 @@ public function testIn($adapter) $finder = $this->buildFinder($adapter); $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator(); - $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php'), $iterator); + $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator); } /** diff --git a/src/Symfony/Component/Finder/Tests/GlobTest.php b/src/Symfony/Component/Finder/Tests/GlobTest.php new file mode 100755 index 000000000000..22ee37338980 --- /dev/null +++ b/src/Symfony/Component/Finder/Tests/GlobTest.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Finder\Tests; + +use Symfony\Component\Finder\Glob; + +class GlobTest extends \PHPUnit_Framework_TestCase +{ + + public function testGlobToRegexDelimiters() + { + $this->assertEquals(Glob::toRegex('.*'), '#^\.[^/]*$#'); + $this->assertEquals(Glob::toRegex('.*', true, true, ''), '^\.[^/]*$'); + $this->assertEquals(Glob::toRegex('.*', true, true, '/'), '/^\.[^/]*$/'); + } + +}