setWebRoot(FSC::$app['config']['content_directory']); $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3); $scanResults = $scanner->getScanResults(); //获取目录 $menus = $scanner->getMenus(); $titles = array(); $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]; $breadcrumbs = $this->getBreadcrumbs($scanResults, $subcate); //获取当前目录下的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); //TODO: 根据tag获取相关数据,并传给视图;调整视图兼容tag的数据结构 $pageTitle = $defaultTitle = !empty($titles) ? $titles[0]['name'] : FSC::$app['config']['site_name']; if (!empty($subcate)) { $pageTitle = "{$subcate['directory']}相关视频,来自{$defaultTitle}"; if (!empty($subcate['title'])) { $pageTitle = "{$subcate['title']},来自{$defaultTitle}"; } } $viewName = '//site/index'; //共享视图 $params = compact('cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', 'tags'); return $this->render($viewName, $params, $pageTitle); } //根据目录结构以及当前目录获取面包屑 protected function getBreadcrumbs($menus, $subcate) { $breads = array(); if (!empty($subcate['directory'])) { array_push($breads, [ 'id' => $subcate['id'], 'name' => $subcate['directory'], 'url' => $subcate['path'], ]); }else { //如果是文件 array_push($breads, [ 'id' => $subcate['id'], 'name' => $subcate['filename'], 'url' => $subcate['path'], ]); } $parent = !empty($menus[$subcate['pid']]) ? $menus[$subcate['pid']] : null; while($parent) { array_unshift($breads, [ 'id' => $parent['id'], 'name' => $parent['directory'], 'url' => $parent['path'], ]); $parent = !empty($parent['pid']) && !empty($menus[$parent['pid']]) ? $menus[$parent['pid']] : null; } return $breads; } }