[Finder] Added support for GLOB patterns in the directories passed to the in() method.
Pattern has to resolve to at least one directory, otherwise exception is thrown (just like when path to an invalid directory is passed).
This commit is contained in:
parent
ef0497441e
commit
29b961107f
@ -6,6 +6,8 @@ CHANGELOG
|
|||||||
|
|
||||||
* added Finder::path() and Finder::notPath() methods
|
* added Finder::path() and Finder::notPath() methods
|
||||||
* added finder adapters to improve performance on specific platforms
|
* added finder adapters to improve performance on specific platforms
|
||||||
|
* added support for wildcard characters (glob patterns) in the paths passed
|
||||||
|
to Finder::in()
|
||||||
|
|
||||||
2.1.0
|
2.1.0
|
||||||
-----
|
-----
|
||||||
|
@ -598,21 +598,25 @@ class Finder implements \IteratorAggregate, \Countable
|
|||||||
*
|
*
|
||||||
* @return Finder The current Finder instance
|
* @return Finder The current Finder instance
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException if one of the directory does not exist
|
* @throws \InvalidArgumentException if one of the directories does not exist
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function in($dirs)
|
public function in($dirs)
|
||||||
{
|
{
|
||||||
$dirs = (array) $dirs;
|
$resolvedDirs = array();
|
||||||
|
|
||||||
foreach ($dirs as $dir) {
|
foreach ((array) $dirs as $dir) {
|
||||||
if (!is_dir($dir)) {
|
if (is_dir($dir)) {
|
||||||
|
$resolvedDirs[] = $dir;
|
||||||
|
} elseif ($glob = glob($dir, GLOB_ONLYDIR)) {
|
||||||
|
$resolvedDirs = array_merge($resolvedDirs, $glob);
|
||||||
|
} else {
|
||||||
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
|
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->dirs = array_merge($this->dirs, $dirs);
|
$this->dirs = array_merge($this->dirs, $resolvedDirs);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -311,6 +311,27 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
|||||||
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php'), $iterator);
|
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php'), $iterator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getAdaptersTestData
|
||||||
|
*/
|
||||||
|
public function testInWithGlob($adapter)
|
||||||
|
{
|
||||||
|
$finder = $this->buildFinder($adapter);
|
||||||
|
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
|
||||||
|
|
||||||
|
$this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getAdaptersTestData
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testInWithNonDirectoryGlob($adapter)
|
||||||
|
{
|
||||||
|
$finder = $this->buildFinder($adapter);
|
||||||
|
$finder->in(__DIR__.'/Fixtures/A/a*');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getAdaptersTestData
|
* @dataProvider getAdaptersTestData
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user