brainexploded/fstools

dev-master 2016-12-08 12:32 UTC

This package is not auto-updated.

Last update: 2024-09-28 21:01:06 UTC


README

FSTraverser

这是用于递归遍历目录并对每个找到的文件应用回调的类。

使用示例

$tr = new FSTraverser(
    // root dir
    '/home/user/lol',
    // callback
    function($path, $entry) {
        echo $entry, PHP_EOL;
    }
);
$tr->setExcludeExtensions(['php', 'js']);

$tr->go();

在这个例子中,我们打印所有不是php或js的文件。

或者

$tr = new FSTraverser(
    // root dir
    '/home/user/lol',
    // callback
    function($path, $entry) {
        echo $entry, PHP_EOL;
    },
    // exclude nodes
    ['.git', 'README.md'],
    // exclude extensions (have no point in this case, because allowed extensions are setted)
    ['zip', 'gz'],
    // allowed extensions (process only files with this extension)
    ['js', 'twig'],
    // maximal depth
    5
);

$tr->go();

在这个例子中,我们遍历lol目录,避免遍历.git目录和README.md文件,只处理js和twig文件,并且不遍历所有超过5级的节点。