merged branch jfsimon/issue-6586 (PR #7190)

This PR was squashed before being merged into the master branch (closes #7190).

Commits
-------

6e0d93e [Finder] Adds expandable globs support to shell adapters

Discussion
----------

[Finder] Adds expandable globs support to shell adapters

As expandable globs, i mean glob following `*.{a,b}` syntax.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #6586
This commit is contained in:
Fabien Potencier 2013-02-26 17:28:03 +01:00
commit 6c966c7565
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());
}
/**