本文介绍了循环多个RecursiveDirectoryIterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下PHP:

$directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/tests/phpunit/' );

/**
 * @var SplFileInfo $fileInfo
 */
foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
    if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) {
        $files[] = $fileInfo->getPathname();
    }
}

这将创建一个数组,其中包含文件的路径在/ tests / phpunit中使用Test.php。工作得很好,很好。但是现在我还想在/ tests / integration中注册东西。 (/ tests中有更多的东西,所以我不能简单地浏览整个目录。)我可以复制代码,但那是蹩脚的。所以现在我想弄清楚如何循环两个RecursiveDirectoryIterator实例。这样做是否可行?

This creates an array with the paths to the files ending with Test.php in /tests/phpunit. Works nice and well. However now I also want to register stuff in /tests/integration. (There are more things in /tests, so I cannot simply go through that whole directory.) I could copy the code, but that’s be lame. So now I'm trying to figure out how to loop over two RecursiveDirectoryIterator instances. Is this possible in a sane way?

推荐答案

要迭代迭代器,你可以使用

To iterate over iterators you can use AppendIterator class.

$directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/tests/phpunit/' );
$directoryIterator1 = new RecursiveDirectoryIterator( __DIR__ . '/tests/integration/' );
$iterator = new AppendIterator();
$iterator->append(new RecursiveIteratorIterator( $directoryIterator ));
$iterator->append(new RecursiveIteratorIterator( $directoryIterator1 ));

/**
 * @var SplFileInfo $fileInfo
 */
foreach ($iterator as $fileInfo) {
    if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) {
        $files[] = $fileInfo->getPathname();
    }
}

这篇关于循环多个RecursiveDirectoryIterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 20:55