Skip to content

Commit

Permalink
Adds expandable globs support to shell adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon authored and fabpot committed Feb 26, 2013
1 parent 287dbbe commit 13b8ce0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php
Expand Up @@ -154,6 +154,11 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
foreach ($names as $i => $name) {
$expr = Expression::create($name);

// Find does not support expandable globs ("*.{a,b}" syntax).
if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
$expr = Expression::create($expr->getGlob()->toRegex(false));
}

// Fixes 'not search' and 'full path matching' regex problems.
// - Jokers '.' are replaced by [^/].
// - We add '[^/]*' before and after regex (if no ^|$ flags are present).
Expand Down Expand Up @@ -197,6 +202,11 @@ private function buildPathsFiltering(Command $command, $dir, array $paths, $not
foreach ($paths as $i => $path) {
$expr = Expression::create($path);

// Find does not support expandable globs ("*.{a,b}" syntax).
if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
$expr = Expression::create($expr->getGlob()->toRegex(false));
}

// Fixes 'not search' regex problems.
if ($expr->isRegex()) {
$regex = $expr->getRegex();
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Finder/Expression/Expression.php
Expand Up @@ -122,6 +122,20 @@ public function isGlob()
return self::TYPE_GLOB === $this->value->getType();
}

/**
* @throws \LogicException
*
* @return Glob
*/
public function getGlob()
{
if (self::TYPE_GLOB !== $this->value->getType()) {
throw new \LogicException('Regex cant be transformed to glob.');
}

return $this->value;
}

/**
* @return Regex
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Finder/Expression/Glob.php
Expand Up @@ -81,6 +81,17 @@ public function append($expr)
return $this;
}

/**
* Tests if glob is expandable ("*.{a,b}" syntax).
*
* @return bool
*/
public function isExpandable()
{
return false !== strpos($this->pattern, '{')
&& false !== strpos($this->pattern, '}');
}

/**
* @param bool $strictLeadingDot
* @param bool $strictWildcardSlash
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Expand Up @@ -106,6 +106,10 @@ public function testName($adapter)
$finder = $this->buildFinder($adapter);
$finder->name('~\\.php$~i');
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());

$finder = $this->buildFinder($adapter);
$finder->name('test.p{hp,y}');
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
}

/**
Expand All @@ -128,6 +132,12 @@ public function testNotName($adapter)
$finder->notName('*.php');
$finder->notName('*.py');
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());

$finder = $this->buildFinder($adapter);
$finder->name('test.ph*');
$finder->name('test.py');
$finder->notName('*.p{hp,y}');
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
}

/**
Expand Down

0 comments on commit 13b8ce0

Please sign in to comment.