Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Finder] Adds expandable globs support to shell adapters #7190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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