[Finder] simplified LimitDepthFilterIterator
This commit is contained in:
parent
aaeb48f744
commit
02858c4c3d
@ -346,16 +346,16 @@ class Finder implements \IteratorAggregate
|
||||
|
||||
$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)
|
||||
{
|
||||
$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)
|
||||
{
|
||||
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
|
||||
|
@ -21,22 +21,18 @@ namespace Symfony\Components\Finder\Iterator;
|
||||
class LimitDepthFilterIterator extends \FilterIterator
|
||||
{
|
||||
protected $minDepth = 0;
|
||||
protected $maxDepth = INF;
|
||||
protected $baseDir;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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 $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->maxDepth = (double) $maxDepth;
|
||||
|
||||
$iterator->setMaxDepth(INF === $maxDepth ? -1 : $maxDepth);
|
||||
|
||||
parent::__construct($iterator);
|
||||
}
|
||||
@ -48,20 +44,6 @@ class LimitDepthFilterIterator extends \FilterIterator
|
||||
*/
|
||||
public function accept()
|
||||
{
|
||||
$fileinfo = $this->getInnerIterator()->current();
|
||||
|
||||
$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;
|
||||
return $this->getInnerIterator()->getDepth() >= $this->minDepth;
|
||||
}
|
||||
}
|
||||
|
@ -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')),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user