[Finder] Adds expandable globs support to shell adapters

This commit is contained in:
Jean-François Simon 2013-02-26 11:03:08 +01:00 committed by Fabien Potencier
parent fb7004baab
commit 6e0d93ef16
4 changed files with 45 additions and 0 deletions

View File

@ -154,6 +154,11 @@ abstract class AbstractFindAdapter extends AbstractAdapter
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).
@ -197,6 +202,11 @@ abstract class AbstractFindAdapter extends AbstractAdapter
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();

View File

@ -122,6 +122,20 @@ class Expression implements ValueInterface
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
*/

View File

@ -81,6 +81,17 @@ class Glob implements ValueInterface
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

View File

@ -106,6 +106,10 @@ class FinderTest extends Iterator\RealIteratorTestCase
$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());
}
/**
@ -128,6 +132,12 @@ class FinderTest extends Iterator\RealIteratorTestCase
$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());
}
/**