Skip to content

Commit 6d3ed7a

Browse files
committed
merged branch jakzal/finder-glob-support (PR symfony#6531)
This PR was merged into the master branch. Commits ------- 29b9611 [Finder] Added support for GLOB patterns in the directories passed to the in() method. Discussion ---------- [Finder] Added support for wildcard characters (GLOB patterns) Added support for wildcard characters in the paths passed to the *in()* method. Each pattern has to resolve to at least one directory, otherwise exception is thrown (just like when path to an invalid directory is passed). Example usage: ```php $finder = new \Symfony\Component\Finder\Finder(); $files = $finder->files() ->name('validators.en.*') ->in(array( 'src/Symfony/*/*/Resources/translations', )); foreach ($files as $file) { var_dump($file->getRealPath()); } ``` Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: symfony#5450 Todo: - License of the code: MIT Documentation PR: symfony/symfony-docs#2089
2 parents 1734266 + 29b9611 commit 6d3ed7a

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/Symfony/Component/Finder/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* added Finder::path() and Finder::notPath() methods
88
* added finder adapters to improve performance on specific platforms
9+
* added support for wildcard characters (glob patterns) in the paths passed
10+
to Finder::in()
911

1012
2.1.0
1113
-----

src/Symfony/Component/Finder/Finder.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,21 +598,25 @@ public function followLinks()
598598
*
599599
* @return Finder The current Finder instance
600600
*
601-
* @throws \InvalidArgumentException if one of the directory does not exist
601+
* @throws \InvalidArgumentException if one of the directories does not exist
602602
*
603603
* @api
604604
*/
605605
public function in($dirs)
606606
{
607-
$dirs = (array) $dirs;
607+
$resolvedDirs = array();
608608

609-
foreach ($dirs as $dir) {
610-
if (!is_dir($dir)) {
609+
foreach ((array) $dirs as $dir) {
610+
if (is_dir($dir)) {
611+
$resolvedDirs[] = $dir;
612+
} elseif ($glob = glob($dir, GLOB_ONLYDIR)) {
613+
$resolvedDirs = array_merge($resolvedDirs, $glob);
614+
} else {
611615
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
612616
}
613617
}
614618

615-
$this->dirs = array_merge($this->dirs, $dirs);
619+
$this->dirs = array_merge($this->dirs, $resolvedDirs);
616620

617621
return $this;
618622
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,27 @@ public function testIn($adapter)
311311
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php'), $iterator);
312312
}
313313

314+
/**
315+
* @dataProvider getAdaptersTestData
316+
*/
317+
public function testInWithGlob($adapter)
318+
{
319+
$finder = $this->buildFinder($adapter);
320+
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
321+
322+
$this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
323+
}
324+
325+
/**
326+
* @dataProvider getAdaptersTestData
327+
* @expectedException \InvalidArgumentException
328+
*/
329+
public function testInWithNonDirectoryGlob($adapter)
330+
{
331+
$finder = $this->buildFinder($adapter);
332+
$finder->in(__DIR__.'/Fixtures/A/a*');
333+
}
334+
314335
/**
315336
* @dataProvider getAdaptersTestData
316337
*/

0 commit comments

Comments
 (0)