月度归档:2023年10月

PHP生成器递归遍历目录

    private function fullScanDir($dir): \Generator
    {
        $list = scandir($dir);
        foreach ($list as $filename) {
            if ($filename === '.' || $filename === '..') continue;
            if ($filename === '.git') continue;
            $aDir = sprintf("%s/%s", $dir, $filename);
            if (is_dir($aDir)) {
                foreach ($this->fullScanDir($aDir) as $_filename) {
                    yield $_filename;
                }
            } else {
                yield $aDir;
            }
        }
    }