merged branch alquerci/ticket-3585-7834 (PR #8120)
This PR was squashed before being merged into the 2.1 branch (closes
Discussion
----------
[Finder] Fix iteration fails with non-rewindable streams
<table>
<tr>
<th>Q</th><th>A</th>
</tr>
<tr>
<td>Bug fix?</td><td>yes</td>
</tr>
<tr>
<td>New feature?</td><td>no</td>
</tr>
<tr>
<td>BC breaks?</td><td>no</td>
</tr>
<tr>
<td>Deprecations?</td><td>no</td>
</tr>
<tr>
<td>Tests pass?</td><td>yes</td>
</tr>
<tr>
<td>Fixed tickets</td><td>#3585, #7834</td>
</tr>
<tr>
<td>License?</td><td>MIT</td>
</tr>
</table>
- [x] Add a good detection of non seekable stream
- [x] Add some unit tests
But the iteration under ftp stream still not work properly. Edit: need
tests for that.
Commits
-------
169c0b9
[Finder] Fix iteration fails with non-rewindable streams
This commit is contained in:
commit
e9f43bf675
|
@ -30,7 +30,14 @@ abstract class FilterIterator extends \FilterIterator
|
|||
{
|
||||
$iterator = $this;
|
||||
while ($iterator instanceof \OuterIterator) {
|
||||
if ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
|
||||
$innerIterator = $iterator->getInnerIterator();
|
||||
|
||||
if ($innerIterator instanceof RecursiveDirectoryIterator) {
|
||||
if ($innerIterator->isRewindable()) {
|
||||
$innerIterator->next();
|
||||
$innerIterator->rewind();
|
||||
}
|
||||
} elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
|
||||
$iterator->getInnerIterator()->next();
|
||||
$iterator->getInnerIterator()->rewind();
|
||||
}
|
||||
|
|
|
@ -20,6 +20,11 @@ use Symfony\Component\Finder\SplFileInfo;
|
|||
*/
|
||||
class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
|
||||
{
|
||||
/**
|
||||
* @var Boolean
|
||||
*/
|
||||
private $rewindable;
|
||||
|
||||
public function __construct($path, $flags)
|
||||
{
|
||||
if ($flags & (self::CURRENT_AS_PATHNAME | self::CURRENT_AS_SELF)) {
|
||||
|
@ -39,11 +44,41 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
|
|||
return new SplFileInfo(parent::current()->getPathname(), $this->getSubPath(), $this->getSubPathname());
|
||||
}
|
||||
|
||||
/**
|
||||
* Do nothing for non rewindable stream
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
if (false === $this->isRewindable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// @see https://bugs.php.net/bug.php?id=49104
|
||||
parent::next();
|
||||
|
||||
parent::rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the stream is rewindable.
|
||||
*
|
||||
* @return Boolean true when the stream is rewindable, false otherwise
|
||||
*/
|
||||
public function isRewindable()
|
||||
{
|
||||
if (null !== $this->rewindable) {
|
||||
return $this->rewindable;
|
||||
}
|
||||
|
||||
if (false !== $stream = @opendir($this->getPath())) {
|
||||
$infos = stream_get_meta_data($stream);
|
||||
closedir($stream);
|
||||
|
||||
if ($infos['seekable']) {
|
||||
return $this->rewindable = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->rewindable = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -485,4 +485,21 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
|||
$this->assertIterator($expected, $finder);
|
||||
$this->assertIteratorInForeach($expected, $finder);
|
||||
}
|
||||
|
||||
public function testNonSeekableStream()
|
||||
{
|
||||
try {
|
||||
$i = Finder::create()->in('ftp://ftp.mozilla.org/')->depth(0)->getIterator();
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
$this->markTestSkipped(sprintf('Unsupported stream "%s".', 'ftp'));
|
||||
}
|
||||
|
||||
$contains = array(
|
||||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README',
|
||||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html',
|
||||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub',
|
||||
);
|
||||
|
||||
$this->assertIteratorInForeach($contains, $i);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Finder\Tests\Iterator;
|
||||
|
||||
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
|
||||
|
||||
class RecursiveDirectoryIteratorTest extends IteratorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getPaths
|
||||
*
|
||||
* @param string $path
|
||||
* @param Boolean $seekable
|
||||
* @param Boolean $supports
|
||||
* @param string $message
|
||||
*/
|
||||
public function testRewind($path, $seekable, $contains, $message = null)
|
||||
{
|
||||
try {
|
||||
$i = new RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
$this->markTestSkipped(sprintf('Unsupported stream "%s".', $path));
|
||||
}
|
||||
|
||||
$i->rewind();
|
||||
|
||||
$this->assertTrue(true, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getPaths
|
||||
*
|
||||
* @param string $path
|
||||
* @param Boolean $seekable
|
||||
* @param Boolean $supports
|
||||
* @param string $message
|
||||
*/
|
||||
public function testSeek($path, $seekable, $contains, $message = null)
|
||||
{
|
||||
try {
|
||||
$i = new RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
$this->markTestSkipped(sprintf('Unsupported stream "%s".', $path));
|
||||
}
|
||||
|
||||
$actual = array();
|
||||
|
||||
$i->seek(0);
|
||||
$actual[] = $i->getPathname();
|
||||
|
||||
$i->seek(1);
|
||||
$actual[] = $i->getPathname();
|
||||
|
||||
$i->seek(2);
|
||||
$actual[] = $i->getPathname();
|
||||
|
||||
$this->assertEquals($contains, $actual);
|
||||
}
|
||||
|
||||
public function getPaths()
|
||||
{
|
||||
$data = array();
|
||||
|
||||
// ftp
|
||||
$contains = array(
|
||||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'README',
|
||||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'index.html',
|
||||
'ftp://ftp.mozilla.org'.DIRECTORY_SEPARATOR.'pub',
|
||||
);
|
||||
$data[] = array('ftp://ftp.mozilla.org/', false, $contains);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
Reference in New Issue