setWebRoot(FSC::$app['config']['content_directory']); $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); $scanResults = $scanner->getScanResults(); //获取目录 $menus = $scanner->getMenus(); $titles = array(); $htmlReadme = ''; $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { if (!empty($readmeFile['sort'])) { $menus_sorted = explode("\n", $readmeFile['sort']); } $titles = $scanner->getMDTitles($readmeFile['id']); $Parsedown = new Parsedown(); $content = file_get_contents($readmeFile['realpath']); $htmlReadme = $Parsedown->text($content); $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } //排序 $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); if (!empty($sortedTree)) { $menus = $sortedTree['menus']; $dirTree = $sortedTree['dirTree']; } //默认显示的目录 $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId]; //获取当前目录下的readme $cateReadmeFile = $scanner->getDefaultReadme($cateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); $content = file_get_contents($cateReadmeFile['realpath']); $htmlCateReadme = $Parsedown->text($content); $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); } //获取tags分类 $tags = $this->getTags($dirTree); $pageTitle = $defaultTitle = !empty($titles) ? $titles[0]['name'] : FSC::$app['config']['site_name']; if (!empty($subcate)) { $pageTitle = "{$subcate['directory']},来自{$defaultTitle}"; } if (!empty($readmeFile['title'])) { $pageTitle = "{$readmeFile['title']},来自{$defaultTitle}"; } $viewName = 'index'; $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'tags'); return $this->render($viewName, $params, $pageTitle); } //获取tag分类 protected function getTags($dirTree) { $tags = array(); $tagDir = null; $tagSaveDirName = str_replace('/', '', FSC::$app['config']['tajian']['tag_dir']); foreach($dirTree as $id => $item) { if (!empty($item['directory']) && $item['directory'] == $tagSaveDirName) { $tagDir = $item; break; } } if (!empty($tagDir) && !empty($tagDir['files'])) { foreach($tagDir['files'] as $id => $item) { if (empty($item['realpath'])) { //如果是txt描述文件 $tag = $this->getTagItem($item); $tags[$tag['id']] = $tag; } } } return $tags; } protected function getTagItem($tagFile) { $tag = array(); foreach($tagFile as $name => $item) { if ($name == 'id') { $tag['id'] = $item; }else { $tag['name'] = $name; $tag['files'] = explode("\n", $item); } } return $tag; } }