[Finder] made the in() method fluent as others as Finder now implements the IteratorAggregate interface

This commit is contained in:
Fabien Potencier 2010-05-04 11:34:29 +02:00
parent 376ca78346
commit aaeb48f744
2 changed files with 100 additions and 40 deletions

View File

@ -29,7 +29,7 @@ namespace Symfony\Components\Finder;
* @subpackage Components_Finder * @subpackage Components_Finder
* @author Fabien Potencier <fabien.potencier@symfony-project.com> * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/ */
class Finder class Finder implements \IteratorAggregate
{ {
protected $mode = 0; protected $mode = 0;
protected $names = array(); protected $names = array();
@ -42,6 +42,7 @@ class Finder
protected $followLinks = false; protected $followLinks = false;
protected $sort = false; protected $sort = false;
protected $ignoreVCS = true; protected $ignoreVCS = true;
protected $dirs = array();
/** /**
* Restricts the matching to directories only. * Restricts the matching to directories only.
@ -278,35 +279,64 @@ class Finder
/** /**
* Searches files and directories which match defined rules. * Searches files and directories which match defined rules.
* *
* @param string $dir A directory path * @param string|array $dirs A directory path or an array of directories
*
* @return Symfony\Components\Finder The current Finder instance
*
* @throws \InvalidArgumentException if one of the directory does not exist
*/
public function in($dirs)
{
if (!is_array($dirs))
{
$dirs = array($dirs);
}
foreach ($dirs as $dir)
{
if (!is_dir($dir))
{
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
}
}
$this->dirs = array_merge($this->dirs, $dirs);
return $this;
}
/**
* Returns an Iterator for the current Finder configuration.
*
* This method implements the IteratorAggregate interface.
* *
* @return \Iterator An iterator * @return \Iterator An iterator
* *
* @throws \InvalidArgumentException if the directory does not exist * @throws \LogicException if the in() method has not been called
*/ */
public function in($dir) public function getIterator()
{ {
if (is_array($dir)) if (0 === count($this->dirs))
{ {
$iterator = new \AppendIterator(); throw new \LogicException('You must call the in() method before iterating over a Finder.');
foreach ($dir as $d)
{
$iterator->append($this->searchInDirectory($d));
}
return $iterator;
} }
return $this->searchInDirectory($dir); if (1 === count($this->dirs))
{
return $this->searchInDirectory($this->dirs[0]);
}
$iterator = new \AppendIterator();
foreach ($this->dirs as $dir)
{
$iterator->append($this->searchInDirectory($dir));
}
return $iterator;
} }
protected function searchInDirectory($dir) protected function searchInDirectory($dir)
{ {
if (!is_dir($dir))
{
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
}
$flags = \FilesystemIterator::SKIP_DOTS; $flags = \FilesystemIterator::SKIP_DOTS;
if ($this->followLinks) if ($this->followLinks)

View File

@ -30,40 +30,40 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->directories()); $this->assertSame($finder, $finder->directories());
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
$finder = new Finder(); $finder = new Finder();
$finder->directories(); $finder->directories();
$finder->files(); $finder->files();
$finder->directories(); $finder->directories();
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testFiles() public function testFiles()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->files()); $this->assertSame($finder, $finder->files());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
$finder = new Finder(); $finder = new Finder();
$finder->files(); $finder->files();
$finder->directories(); $finder->directories();
$finder->files(); $finder->files();
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testMaxDepth() public function testMaxDepth()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->maxDepth(0)); $this->assertSame($finder, $finder->maxDepth(0));
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testMinDepth() public function testMinDepth()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->minDepth(1)); $this->assertSame($finder, $finder->minDepth(1));
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testMinMaxDepth() public function testMinMaxDepth()
@ -71,91 +71,91 @@ class FinderTest extends Iterator\RealIteratorTestCase
$finder = new Finder(); $finder = new Finder();
$finder->maxDepth(0); $finder->maxDepth(0);
$finder->minDepth(1); $finder->minDepth(1);
$this->assertIterator(array(), $finder->in(self::$tmpDir)); $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
} }
public function testName() public function testName()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->name('*.php')); $this->assertSame($finder, $finder->name('*.php'));
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
$finder = new Finder(); $finder = new Finder();
$finder->name('test.ph*'); $finder->name('test.ph*');
$finder->name('test.py'); $finder->name('test.py');
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testNotName() public function testNotName()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->notName('*.php')); $this->assertSame($finder, $finder->notName('*.php'));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
$finder = new Finder(); $finder = new Finder();
$finder->notName('*.php'); $finder->notName('*.php');
$finder->notName('*.py'); $finder->notName('*.py');
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)->getIterator());
$finder = new Finder(); $finder = new Finder();
$finder->name('test.ph*'); $finder->name('test.ph*');
$finder->name('test.py'); $finder->name('test.py');
$finder->notName('*.php'); $finder->notName('*.php');
$finder->notName('*.py'); $finder->notName('*.py');
$this->assertIterator(array(), $finder->in(self::$tmpDir)); $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSize() public function testSize()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500')); $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testExclude() public function testExclude()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->exclude('foo')); $this->assertSame($finder, $finder->exclude('foo'));
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testIgnoreVCS() public function testIgnoreVCS()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->ignoreVCS(false)); $this->assertSame($finder, $finder->ignoreVCS(false));
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->ignoreVCS(true)); $this->assertSame($finder, $finder->ignoreVCS(true));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByName() public function testSortByName()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->sortByName()); $this->assertSame($finder, $finder->sortByName());
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByType() public function testSortByType()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->sortByType()); $this->assertSame($finder, $finder->sortByType());
$this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSort() public function testSort()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); })); $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testFilter() public function testFilter()
{ {
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; })); $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testFollowLinks() public function testFollowLinks()
@ -167,7 +167,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$finder = new Finder(); $finder = new Finder();
$this->assertSame($finder, $finder->followLinks()); $this->assertSame($finder, $finder->followLinks());
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testIn() public function testIn()
@ -184,11 +184,41 @@ class FinderTest extends Iterator\RealIteratorTestCase
} }
$finder = new Finder(); $finder = new Finder();
$iterator = $finder->files()->name('*.php')->maxDepth(0)->in(array(self::$tmpDir, __DIR__)); $iterator = $finder->files()->name('*.php')->maxDepth(0)->in(array(self::$tmpDir, __DIR__))->getIterator();
$this->assertIterator(array(self::$tmpDir.'test.php', __DIR__.'/FinderTest.php', __DIR__.'/GlobTest.php', __DIR__.'/NumberCompareTest.php'), $iterator); $this->assertIterator(array(self::$tmpDir.'test.php', __DIR__.'/FinderTest.php', __DIR__.'/GlobTest.php', __DIR__.'/NumberCompareTest.php'), $iterator);
} }
public function testGetIterator()
{
$finder = new Finder();
try
{
$finder->getIterator();
$this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
}
catch (\Exception $e)
{
$this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
}
$finder = new Finder();
$dirs = array();
foreach ($finder->directories()->in(self::$tmpDir) as $dir)
{
$dirs[] = (string) $dir;
}
$this->assertEquals($this->toAbsolute(array('foo', 'toto')), $dirs, 'implements the \IteratorAggregate interface');
$finder = new Finder();
$this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
$finder = new Finder();
$a = iterator_to_array($finder->directories()->in(self::$tmpDir));
$this->assertEquals($this->toAbsolute(array('foo', 'toto')), array_values(array_map(function ($a) { return (string) $a; }, $a)), 'implements the \IteratorAggregate interface');
}
protected function toAbsolute($files) protected function toAbsolute($files)
{ {
$f = array(); $f = array();