[Finder] Check PHP version before applying a workaround for a PHP bug

This commit is contained in:
Alex Bogomazov 2015-12-24 01:45:26 +03:00 committed by Fabien Potencier
parent 5d63c554e8
commit cba206ab18
4 changed files with 27 additions and 18 deletions

View File

@ -12,9 +12,10 @@
namespace Symfony\Component\Finder\Iterator; namespace Symfony\Component\Finder\Iterator;
/** /**
* This iterator just overrides the rewind method in order to correct a PHP bug. * This iterator just overrides the rewind method in order to correct a PHP bug,
* which existed before version 5.5.23/5.6.7.
* *
* @see https://bugs.php.net/bug.php?id=49104 * @see https://bugs.php.net/68557
* *
* @author Alex Bogomazov * @author Alex Bogomazov
*/ */
@ -28,18 +29,19 @@ abstract class FilterIterator extends \FilterIterator
*/ */
public function rewind() public function rewind()
{ {
if (PHP_VERSION_ID > 50607 || (PHP_VERSION_ID > 50523 && PHP_VERSION_ID < 50600)) {
parent::rewind();
return;
}
$iterator = $this; $iterator = $this;
while ($iterator instanceof \OuterIterator) { while ($iterator instanceof \OuterIterator) {
$innerIterator = $iterator->getInnerIterator(); $innerIterator = $iterator->getInnerIterator();
if ($innerIterator instanceof RecursiveDirectoryIterator) { if ($innerIterator instanceof \FilesystemIterator) {
if ($innerIterator->isRewindable()) { $innerIterator->next();
$innerIterator->next(); $innerIterator->rewind();
$innerIterator->rewind();
}
} elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
$iterator->getInnerIterator()->next();
$iterator->getInnerIterator()->rewind();
} }
$iterator = $iterator->getInnerIterator(); $iterator = $iterator->getInnerIterator();
} }

View File

@ -118,8 +118,10 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
return; return;
} }
// @see https://bugs.php.net/bug.php?id=49104 // @see https://bugs.php.net/68557
parent::next(); if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) {
parent::next();
}
parent::rewind(); parent::rewind();
} }

View File

@ -458,7 +458,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
* Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator * Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
* with inner FilesystemIterator in an invalid state. * with inner FilesystemIterator in an invalid state.
* *
* @see https://bugs.php.net/bug.php?id=49104 * @see https://bugs.php.net/68557
*/ */
public function testMultipleLocations() public function testMultipleLocations()
{ {
@ -468,8 +468,12 @@ class FinderTest extends Iterator\RealIteratorTestCase
); );
// it is expected that there are test.py test.php in the tmpDir // it is expected that there are test.py test.php in the tmpDir
$finder = $this->buildFinder(); $finder = new Finder();
$finder->in($locations)->depth('< 1')->name('test.php'); $finder->in($locations)
// the default flag IGNORE_DOT_FILES fixes the problem indirectly
// so we set it to false for better isolation
->ignoreDotFiles(false)
->depth('< 1')->name('test.php');
$this->assertCount(1, $finder); $this->assertCount(1, $finder);
} }
@ -479,7 +483,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
* AppendIterator which does an unnecessary rewind which leaves * AppendIterator which does an unnecessary rewind which leaves
* FilterIterator with inner FilesystemIterator in an invalid state. * FilterIterator with inner FilesystemIterator in an invalid state.
* *
* @see https://bugs.php.net/bug.php?id=49104 * @see https://bugs.php.net/68557
*/ */
public function testMultipleLocationsWithSubDirectories() public function testMultipleLocationsWithSubDirectories()
{ {

View File

@ -43,8 +43,9 @@ class FilterIteratorTest extends RealIteratorTestCase
++$c; ++$c;
} }
// This would fail with \FilterIterator but works with Symfony\Component\Finder\Iterator\FilterIterator // This would fail in php older than 5.5.23/5.6.7 with \FilterIterator
// see https://bugs.php.net/bug.php?id=49104 // but works with Symfony\Component\Finder\Iterator\FilterIterator
// see https://bugs.php.net/68557
$this->assertEquals(1, $c); $this->assertEquals(1, $c);
} }
} }