根据地图数组创建目录树

时间:2019-09-30 11:19:48

标签: php iterator

我的任务是创建一个目录树,其中一些目录文件需要过滤。 我认为创建一个映射是一个好主意,我可以在该映射中为每个目录指定是否应用过滤。

protected $templatesMap = array(
    'config' => true,
    'html' => [
        'pages' => false,
        'header' => true,
        'index' => false,
        'main' => true,
        'product' => true,
        'reviews' => false,
    ],
    'html_mobile' => [
        'banner' => true,
        'pages' => false
    ]
);

这是我的代码:

protected function getDirectoriesTree()
{
    $templateTree = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('design/theme_dir_name/'), RecursiveIteratorIterator::CHILD_FIRST);
    /** @var RecursiveDirectoryIterator $fileInfo */
    foreach ($iterator as $fileInfo) {
        $path = $fileInfo->isDir() ? array($fileInfo->getFilename() => array()) : array($fileInfo->getFilename());
        for ($depth = $iterator->getDepth() - 1; $depth >= 0; $depth--) {
            $dirName = $iterator->getSubIterator($depth)->current()->getFilename();
            if (!in_array($dirName, ['.', '..'])) {
                /**
                 * 
                 * My problem is here! 
                 * I do not know, how to check that if file must be filtered or not, 
                 * based on $this->templatesMap.
                 * 
                 */
                $path = [$dirName => $path];
            }
        }
        $templateTree = array_merge_recursive($templateTree, $path);
    }
    return $templateTree;
}

请帮助我在每次迭代时检查地图的目标项目。或者,如果是个坏主意,还有什么替代方法?

0 个答案:

没有答案