[Finder] Adds expandable globs support to shell adapters
This commit is contained in:
parent
fb7004baab
commit
6e0d93ef16
@ -154,6 +154,11 @@ abstract class AbstractFindAdapter extends AbstractAdapter
|
|||||||
foreach ($names as $i => $name) {
|
foreach ($names as $i => $name) {
|
||||||
$expr = Expression::create($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.
|
// Fixes 'not search' and 'full path matching' regex problems.
|
||||||
// - Jokers '.' are replaced by [^/].
|
// - Jokers '.' are replaced by [^/].
|
||||||
// - We add '[^/]*' before and after regex (if no ^|$ flags are present).
|
// - 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) {
|
foreach ($paths as $i => $path) {
|
||||||
$expr = Expression::create($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.
|
// Fixes 'not search' regex problems.
|
||||||
if ($expr->isRegex()) {
|
if ($expr->isRegex()) {
|
||||||
$regex = $expr->getRegex();
|
$regex = $expr->getRegex();
|
||||||
|
@ -122,6 +122,20 @@ class Expression implements ValueInterface
|
|||||||
return self::TYPE_GLOB === $this->value->getType();
|
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
|
* @return Regex
|
||||||
*/
|
*/
|
||||||
|
@ -81,6 +81,17 @@ class Glob implements ValueInterface
|
|||||||
return $this;
|
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 $strictLeadingDot
|
||||||
* @param bool $strictWildcardSlash
|
* @param bool $strictWildcardSlash
|
||||||
|
@ -106,6 +106,10 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
|||||||
$finder = $this->buildFinder($adapter);
|
$finder = $this->buildFinder($adapter);
|
||||||
$finder->name('~\\.php$~i');
|
$finder->name('~\\.php$~i');
|
||||||
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
|
$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('*.php');
|
||||||
$finder->notName('*.py');
|
$finder->notName('*.py');
|
||||||
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
|
$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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user