diff --git a/src/Symfony/Components/Finder/Finder.php b/src/Symfony/Components/Finder/Finder.php index bc053774ef..38c3f15147 100644 --- a/src/Symfony/Components/Finder/Finder.php +++ b/src/Symfony/Components/Finder/Finder.php @@ -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); diff --git a/src/Symfony/Components/Finder/Iterator/LimitDepthFilterIterator.php b/src/Symfony/Components/Finder/Iterator/LimitDepthFilterIterator.php index 1befe53923..be1b252e2b 100644 --- a/src/Symfony/Components/Finder/Iterator/LimitDepthFilterIterator.php +++ b/src/Symfony/Components/Finder/Iterator/LimitDepthFilterIterator.php @@ -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; } } diff --git a/tests/Symfony/Tests/Components/Finder/Iterator/LimitDepthFilterIteratorTest.php b/tests/Symfony/Tests/Components/Finder/Iterator/LimitDepthFilterIteratorTest.php deleted file mode 100644 index 3b77e06f55..0000000000 --- a/tests/Symfony/Tests/Components/Finder/Iterator/LimitDepthFilterIteratorTest.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * 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')), - ); - } -}