Skip to content

Commit

Permalink
Merge pull request #61 from zobo/feat-skip-errors
Browse files Browse the repository at this point in the history
feat: add $skipErrors option to GlobIterator
  • Loading branch information
Ocramius authored May 24, 2022
2 parents 287cba1 + 0a42fed commit 3c17f7d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Iterator/GlobIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ class GlobIterator extends IteratorIterator
*
* @param string $glob The glob pattern.
* @param int $flags A bitwise combination of the flag constants in
* {@link Glob}.
* {@link Glob}.
* @param bool $skipErrors Add the RecursiveIteratorIterator::CATCH_GET_CHILD
* to internal RecursiveIteratorIterator and prevent throwing
* Exception on errors like access denied.
*/
public function __construct($glob, $flags = 0)
public function __construct($glob, $flags = 0, bool $skipErrors = false)
{
$basePath = Glob::getBasePath($glob, $flags);

Expand Down Expand Up @@ -75,7 +78,8 @@ public function __construct($glob, $flags = 0)
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME
| RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::SELF_FIRST
RecursiveIteratorIterator::SELF_FIRST,
($skipErrors ? RecursiveIteratorIterator::CATCH_GET_CHILD : 0)
),
GlobFilterIterator::FILTER_VALUE,
$flags
Expand Down
79 changes: 79 additions & 0 deletions tests/Iterator/GlobIteratorErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the webmozart/glob package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Webmozart\Glob\Tests\Iterator;

use Symfony\Component\Filesystem\Filesystem;
use Webmozart\Glob\Iterator\GlobIterator;
use Webmozart\Glob\Test\TestUtil;

/**
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class GlobIteratorErrorTest extends \PHPUnit\Framework\TestCase
{
private $tempDir;

protected function setUp(): void
{
$this->tempDir = TestUtil::makeTempDir('webmozart-glob', __CLASS__);

$filesystem = new Filesystem();
$filesystem->mirror(__DIR__.'/../Fixtures', $this->tempDir);
}

protected function tearDown(): void
{
$filesystem = new Filesystem();
chmod($this->tempDir.'/js', 0777);
$filesystem->remove($this->tempDir);
}

public function testIterateError()
{
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$this->markTestSkipped('chmod tests only on Linux.');

return;
}
$this->assertTrue(function_exists('posix_getuid'));
if (posix_getuid() === 0) {
$this->markTestSkipped('Current user is root, cannot test errors because root can read everything.');

return;
}
$this->expectException(\UnexpectedValueException::class);
chmod($this->tempDir.'/js', 0111);
$iterator = new GlobIterator($this->tempDir.'/**/*.css');
iterator_to_array($iterator);
}

public function testIterateWithoutError()
{
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$this->markTestSkipped('chmod tests only on Linux.');

return;
}
$this->assertTrue(function_exists('posix_getuid'));
if (posix_getuid() === 0) {
$this->markTestSkipped('Current user is root, cannot test errors because root can read everything.');

return;
}
chmod($this->tempDir.'/js', 0111);
$iterator = new GlobIterator($this->tempDir.'/**/*.css', 0, true);
$this->assertNotEmpty(iterator_to_array($iterator));
}

}

0 comments on commit 3c17f7d

Please sign in to comment.