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

Skip to content

Commit 6c966c7

Browse files
committed
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
2 parents fb7004b + 6e0d93e commit 6c966c7

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
154154
foreach ($names as $i => $name) {
155155
$expr = Expression::create($name);
156156

157+
// Find does not support expandable globs ("*.{a,b}" syntax).
158+
if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
159+
$expr = Expression::create($expr->getGlob()->toRegex(false));
160+
}
161+
157162
// Fixes 'not search' and 'full path matching' regex problems.
158163
// - Jokers '.' are replaced by [^/].
159164
// - We add '[^/]*' before and after regex (if no ^|$ flags are present).
@@ -197,6 +202,11 @@ private function buildPathsFiltering(Command $command, $dir, array $paths, $not
197202
foreach ($paths as $i => $path) {
198203
$expr = Expression::create($path);
199204

205+
// Find does not support expandable globs ("*.{a,b}" syntax).
206+
if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
207+
$expr = Expression::create($expr->getGlob()->toRegex(false));
208+
}
209+
200210
// Fixes 'not search' regex problems.
201211
if ($expr->isRegex()) {
202212
$regex = $expr->getRegex();

src/Symfony/Component/Finder/Expression/Expression.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ public function isGlob()
122122
return self::TYPE_GLOB === $this->value->getType();
123123
}
124124

125+
/**
126+
* @throws \LogicException
127+
*
128+
* @return Glob
129+
*/
130+
public function getGlob()
131+
{
132+
if (self::TYPE_GLOB !== $this->value->getType()) {
133+
throw new \LogicException('Regex cant be transformed to glob.');
134+
}
135+
136+
return $this->value;
137+
}
138+
125139
/**
126140
* @return Regex
127141
*/

src/Symfony/Component/Finder/Expression/Glob.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ public function append($expr)
8181
return $this;
8282
}
8383

84+
/**
85+
* Tests if glob is expandable ("*.{a,b}" syntax).
86+
*
87+
* @return bool
88+
*/
89+
public function isExpandable()
90+
{
91+
return false !== strpos($this->pattern, '{')
92+
&& false !== strpos($this->pattern, '}');
93+
}
94+
8495
/**
8596
* @param bool $strictLeadingDot
8697
* @param bool $strictWildcardSlash

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public function testName($adapter)
106106
$finder = $this->buildFinder($adapter);
107107
$finder->name('~\\.php$~i');
108108
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
109+
110+
$finder = $this->buildFinder($adapter);
111+
$finder->name('test.p{hp,y}');
112+
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
109113
}
110114

111115
/**
@@ -128,6 +132,12 @@ public function testNotName($adapter)
128132
$finder->notName('*.php');
129133
$finder->notName('*.py');
130134
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
135+
136+
$finder = $this->buildFinder($adapter);
137+
$finder->name('test.ph*');
138+
$finder->name('test.py');
139+
$finder->notName('*.p{hp,y}');
140+
$this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
131141
}
132142

133143
/**

0 commit comments

Comments
 (0)