thinkcmf5更新模板代码

 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-07-30 06:22:52

thinkcmf5更新模板代码的相关文章

thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题

private function updateThemeFiles($theme, $suffix = 'html') { $dir = 'themes/' . $theme; $themeDir = $dir; $tplFiles = []; $root_dir_tpl_files = cmf_scan_dir("$dir/*.$suffix"); //默认情况下返回 模板目录(w0s目录)下的所有html文件名数组 foreach ($root_dir_tpl_files as $

HDU 3966Aragorn's Story(树链剖分)点权更新模板

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4434    Accepted Submission(s): 1212 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

模板代码 - 列表和下拉刷新

*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0; } /* HEAD

inputstream和outputstream读写数据模板代码

//读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(buffer,0,len); }

线段树 + 区间更新 + 模板 ---- poj 3468

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59798   Accepted: 18237 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

通过Git WebHooks+脚本实现自动更新发布代码

需求: 写一个自动化部署脚本,能根据git push动作自动更新发布代码(补丁文件(或更新文件,以下简称补丁)) 解决方案: 通过git webhooks.python脚本.Linux Shell脚本完成,所有动作均自动完成,人工部分仅限于发布补丁文件之前 git webhooks实现触发执行脚本 python脚本用于接收git webhooks的post数据并根据这些数据判断是否执行shell脚本 Linux Shell脚本用于实现部署.备份.回滚等操作 具体步骤(方案实现): 实现原理: 用

hdu 2063 过山车 二分图的最大匹配模板。。感觉模板代码很简洁

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13092    Accepted Submission(s): 5746 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做pa

通过Git WebHooks+脚本实现自动更新发布代码之shell脚本

前文讲述了<通过Git WebHooks+脚本实现自动更新发布代码>,里面提供了一种自动更新发布代码的脚本编写思路.本文的脚本与前文中的思路不太不同.本脚本以capistrano中的一些思想和理念为依据,用简单好理解的shell脚本实现capistrano原本实现的自动化部署部分. 脚本的一些特点和功能: 解决脚本的符号链接问题,准确获取脚本工作目录(从tomcat脚本中学到): 颜色显示,不同级别的信息用不同的颜色显示(共两种方案,前一种从一位不知名的国外工程师处得到,后一种从lnmp1.2