Issue #2671708 by alexpott, damiankloip: Add a RegexDirectoryIterator
parent
82296bf660
commit
912d539dd0
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Component\FileSystem\RegexDirectoryIterator.
|
||||
*/
|
||||
|
||||
namespace Drupal\Component\FileSystem;
|
||||
|
||||
/**
|
||||
* Iterates over files whose names match a regular expression in a directory.
|
||||
*/
|
||||
class RegexDirectoryIterator extends \FilterIterator {
|
||||
|
||||
/**
|
||||
* The regular expression to match.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $regex;
|
||||
|
||||
/**
|
||||
* RegexDirectoryIterator constructor.
|
||||
*
|
||||
* @param string $path
|
||||
* The path to scan.
|
||||
* @param string $regex
|
||||
* The regular expression to match, including delimiters. For example,
|
||||
/\.yml$/ would list only files ending in .yml.
|
||||
*/
|
||||
public function __construct($path, $regex) {
|
||||
// Use FilesystemIterator to not iterate over the the . and .. directories.
|
||||
$iterator = new \FilesystemIterator($path);
|
||||
parent::__construct($iterator);
|
||||
$this->regex = $regex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \FilterIterator::accept().
|
||||
*/
|
||||
public function accept() {
|
||||
/** @var \SplFileInfo $file_info */
|
||||
$file_info = $this->getInnerIterator()->current();
|
||||
return $file_info->isFile() && preg_match($this->regex, $file_info->getFilename());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\Component\FileSystem\RegexDirectoryIteratorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\Component\FileSystem;
|
||||
|
||||
use Drupal\Component\FileSystem\RegexDirectoryIterator;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\Component\FileSystem\RegexDirectoryIterator
|
||||
* @group FileSystem
|
||||
*/
|
||||
class RegexDirectoryIteratorTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::accept
|
||||
* @dataProvider providerTestRegexDirectoryIterator
|
||||
*/
|
||||
public function testRegexDirectoryIterator(array $directory, $regex, array $expected) {
|
||||
vfsStream::setup('root', NULL, $directory);
|
||||
$iterator = new RegexDirectoryIterator(vfsStream::url('root'), $regex);
|
||||
|
||||
// Create an array of filenames to assert against.
|
||||
$file_list = array_map(function(\SplFileInfo $file) {
|
||||
return $file->getFilename();
|
||||
}, array_values(iterator_to_array($iterator)));
|
||||
|
||||
$this->assertSame($expected, $file_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for self::testRegexDirectoryIterator().
|
||||
*/
|
||||
public function providerTestRegexDirectoryIterator() {
|
||||
return [
|
||||
[
|
||||
[
|
||||
'1.yml' => '',
|
||||
],
|
||||
'/\.yml$/',
|
||||
[
|
||||
'1.yml',
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
'1.yml' => '',
|
||||
'2.yml' => '',
|
||||
'3.txt' => '',
|
||||
],
|
||||
'/\.yml$/',
|
||||
[
|
||||
'1.yml',
|
||||
'2.yml',
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
'1.yml' => '',
|
||||
'2.yml' => '',
|
||||
'3.txt' => '',
|
||||
],
|
||||
'/\.txt/',
|
||||
[
|
||||
'3.txt',
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
'1.yml' => '',
|
||||
// Ensure we don't recurse in directories even if that match the
|
||||
// regex.
|
||||
'2.yml' => [
|
||||
'3.yml' => '',
|
||||
'4.yml' => '',
|
||||
],
|
||||
'3.txt' => '',
|
||||
],
|
||||
'/\.yml$/',
|
||||
[
|
||||
'1.yml',
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
'1.yml' => '',
|
||||
'2.yml' => '',
|
||||
'3.txt' => '',
|
||||
],
|
||||
'/^\d/',
|
||||
[
|
||||
'1.yml',
|
||||
'2.yml',
|
||||
'3.txt'
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
'1.yml' => '',
|
||||
'2.yml' => '',
|
||||
'3.txt' => '',
|
||||
],
|
||||
'/^\D/',
|
||||
[],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue