[Finder] simplified LimitDepthFilterIterator

This commit is contained in:
Fabien Potencier 2010-05-04 12:02:07 +02:00
parent aaeb48f744
commit 02858c4c3d
3 changed files with 9 additions and 67 deletions

View File

@ -346,16 +346,16 @@ class Finder implements \IteratorAggregate
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST); $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
if ($this->mindepth > 0 || $this->maxdepth < INF)
{
$iterator = new Iterator\LimitDepthFilterIterator($iterator, $this->mindepth, $this->maxdepth);
}
if ($this->mode) if ($this->mode)
{ {
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode); $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
} }
if ($this->mindepth > 0 || $this->maxdepth < INF)
{
$iterator = new Iterator\LimitDepthFilterIterator($iterator, $dir, $this->mindepth, $this->maxdepth);
}
if ($this->exclude) if ($this->exclude)
{ {
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude); $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);

View File

@ -21,22 +21,18 @@ namespace Symfony\Components\Finder\Iterator;
class LimitDepthFilterIterator extends \FilterIterator class LimitDepthFilterIterator extends \FilterIterator
{ {
protected $minDepth = 0; protected $minDepth = 0;
protected $maxDepth = INF;
protected $baseDir;
/** /**
* Constructor. * Constructor.
* *
* @param \Iterator $iterator The Iterator to filter * @param \Iterator $iterator The Iterator to filter
* @param string $baseDir The base directory for the depth comparison
* @param integer $minDepth The minimum depth * @param integer $minDepth The minimum depth
* @param integer $maxDepth The maximum depth
*/ */
public function __construct(\Iterator $iterator, $baseDir, $minDepth, $maxDepth) public function __construct(\RecursiveIteratorIterator $iterator, $minDepth, $maxDepth)
{ {
$this->baseDir = new \SplFileInfo($baseDir);
$this->minDepth = (integer) $minDepth; $this->minDepth = (integer) $minDepth;
$this->maxDepth = (double) $maxDepth;
$iterator->setMaxDepth(INF === $maxDepth ? -1 : $maxDepth);
parent::__construct($iterator); parent::__construct($iterator);
} }
@ -48,20 +44,6 @@ class LimitDepthFilterIterator extends \FilterIterator
*/ */
public function accept() public function accept()
{ {
$fileinfo = $this->getInnerIterator()->current(); return $this->getInnerIterator()->getDepth() >= $this->minDepth;
$depth = substr_count($fileinfo->getPath(), DIRECTORY_SEPARATOR) - substr_count($this->baseDir->getPathname(), DIRECTORY_SEPARATOR);
if ($depth > $this->maxDepth)
{
return false;
}
if ($depth < $this->minDepth)
{
return false;
}
return true;
} }
} }

View File

@ -1,40 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Components\Finder\Iterator;
use Symfony\Components\Finder\Iterator\LimitDepthFilterIterator;
require_once __DIR__.'/RealIteratorTestCase.php';
class LimitDepthFilterIteratorTest extends RealIteratorTestCase
{
/**
* @dataProvider getAcceptData
*/
public function testAccept($baseDir, $minDepth, $maxDepth, $expected)
{
$inner = new Iterator(self::$files);
$iterator = new LimitDepthFilterIterator($inner, $baseDir, $minDepth, $maxDepth);
$this->assertIterator($expected, $iterator);
}
public function getAcceptData()
{
return array(
array(sys_get_temp_dir().'/symfony2_finder', 0, INF, array(sys_get_temp_dir().'/symfony2_finder/.git', sys_get_temp_dir().'/symfony2_finder/test.py', sys_get_temp_dir().'/symfony2_finder/foo', sys_get_temp_dir().'/symfony2_finder/foo/bar.tmp', sys_get_temp_dir().'/symfony2_finder/test.php', sys_get_temp_dir().'/symfony2_finder/toto')),
array(sys_get_temp_dir().'/symfony2_finder', 0, 0, array(sys_get_temp_dir().'/symfony2_finder/.git', sys_get_temp_dir().'/symfony2_finder/test.py', sys_get_temp_dir().'/symfony2_finder/foo', sys_get_temp_dir().'/symfony2_finder/test.php', sys_get_temp_dir().'/symfony2_finder/toto')),
array(sys_get_temp_dir().'/symfony2_finder', 1, 1, array(sys_get_temp_dir().'/symfony2_finder/foo/bar.tmp')),
);
}
}