private function updateThemeFiles($theme, $suffix = ‘html‘) { $dir = ‘themes/‘ . $theme; $themeDir = $dir; $tplFiles = []; $root_dir_tpl_files = cmf_scan_dir("$dir/*.$suffix"); foreach ($root_dir_tpl_files as $root_tpl_file) { $root_tpl_file = "$dir/$root_tpl_file"; $configFile = preg_replace("/\.$suffix$/", ‘.json‘, $root_tpl_file); $root_tpl_file_no_suffix = preg_replace("/\.$suffix$/", ‘‘, $root_tpl_file); if (is_file($root_tpl_file) && file_exists_case($configFile)) { array_push($tplFiles, $root_tpl_file_no_suffix); } } $subDirs = cmf_sub_dirs($dir); foreach ($subDirs as $dir) { $subDirTplFiles = cmf_scan_dir("$dir/*.$suffix"); foreach ($subDirTplFiles as $tplFile) { $tplFile = "$dir/$tplFile"; $configFile = preg_replace("/\.$suffix$/", ‘.json‘, $tplFile); $tplFileNoSuffix = preg_replace("/\.$suffix$/", ‘‘, $tplFile); if (is_file($tplFile) && file_exists_case($configFile)) { array_push($tplFiles, $tplFileNoSuffix); } } } foreach ($tplFiles as $tplFile) { $configFile = $tplFile . ".json"; $file = preg_replace(‘/^themes\/‘ . $theme . ‘\//‘, ‘‘, $tplFile); $file = strtolower($file); $config = json_decode(file_get_contents($configFile), true); $findFile = Db::name(‘theme_file‘)->where([‘theme‘ => $theme, ‘file‘ => $file])->find(); $isPublic = empty($config[‘is_public‘]) ? 0 : 1; $listOrder = empty($config[‘order‘]) ? 0 : floatval($config[‘order‘]); $configMore = empty($config[‘more‘]) ? [] : $config[‘more‘]; $more = $configMore; if (empty($findFile)) { Db::name(‘theme_file‘)->insert([ ‘theme‘ => $theme, ‘action‘ => $config[‘action‘], ‘file‘ => $file, ‘name‘ => $config[‘name‘], ‘more‘ => json_encode($more), ‘config_more‘ => json_encode($configMore), ‘description‘ => $config[‘description‘], ‘is_public‘ => $isPublic, ‘list_order‘ => $listOrder ]); } else { // 更新文件 $moreInDb = json_decode($findFile[‘more‘], true); $more = $this->updateThemeConfigMore($configMore, $moreInDb); Db::name(‘theme_file‘)->where([‘theme‘ => $theme, ‘file‘ => $file])->update([ ‘theme‘ => $theme, ‘action‘ => $config[‘action‘], ‘file‘ => $file, ‘name‘ => $config[‘name‘], ‘more‘ => json_encode($more), ‘config_more‘ => json_encode($configMore), ‘description‘ => $config[‘description‘], ‘is_public‘ => $isPublic, ‘list_order‘ => $listOrder ]); } } // 检查安装过的模板文件是否已经删除 $files = Db::name(‘theme_file‘)->where([‘theme‘ => $theme])->select(); foreach ($files as $themeFile) { $tplFile = $themeDir . ‘/‘ . $themeFile[‘file‘] . ‘.‘ . $suffix; $tplFileConfigFile = $themeDir . ‘/‘ . $themeFile[‘file‘] . ‘.json‘; if (!is_file($tplFile) || !file_exists_case($tplFileConfigFile)) { Db::name(‘theme_file‘)->where([‘theme‘ => $theme, ‘file‘ => $themeFile[‘file‘]])->delete(); } } }
/** * 替代scan_dir的方法 * @param string $pattern 检索模式 搜索模式 *.txt,*.doc; (同glog方法) * @param int $flags * @param $pattern * @return array */ function cmf_scan_dir($pattern, $flags = null) { $files = glob($pattern, $flags); //函数返回匹配指定模式的文件名或目录。该函数返回一个包含有匹配文件 / 目录的数组。如果出错返回 false。http://www.w3school.com.cn/php/func_filesystem_glob.asp if (empty($files)) { $files = []; } else { $files = array_map(‘basename‘, $files); //函数将用户自定义函数作用到数组中的每个值上,并返回用户自定义函数作用后的带有新值的数组。 basename 函数返回路径中的文件名部分 } return $files; //指定规则的文件的文件名数组,在更新模板时,返回的是所有.html的文件名。 }
时间: 2024-10-09 19:26:35