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 $root_tpl_file) {
$root_tpl_file = "$dir/$root_tpl_file";
$configFile = preg_replace("/\.$suffix$/", '.json', $root_tpl_file); //获取当前$root_tpl_file对应json文件名 例如 index.json
$root_tpl_file_no_suffix = preg_replace("/\.$suffix$/", '', $root_tpl_file); //获取对应文件名无后缀
if (is_file($root_tpl_file) && file_exists_case($configFile)) { //当前$root_tpl_file(例如:index.html)是个文件,并且index.json存在
array_push($tplFiles, $root_tpl_file_no_suffix); //把文件名存起来,不包含后缀。例如:index
}
}
$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) { //遍历所有文件json文件
$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)) { //在数据表theme_file没有找到该模板 则插入
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字段
$more = $this->updateThemeConfigMore($configMore, $moreInDb); //$configMore从index.json文件里读的json,$moreInDb从数据库里读的more字段的值
Db::name('theme_file')->where(['theme' => $theme, 'file' => $file])->update([
'theme' => $theme,
'action' => $config['action'],
'file' => $file,
'name' => $config['name'],
'more' => json_encode($more), //json文件里有并且从数据里取值以后,这里如果$configMore没有正确读取的话应该是返回了一个空值,没有保存成功
'config_more' => json_encode($configMore), //从index.json来的配置文件
'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的文件名,带扩展名 http://www.w3school.com.cn/php/func_filesystem_basename.asp。
}
 private function updateThemeConfigMore($configMore, $moreInDb)
{

//echo empty($configMore);
// print_r($configMore); 
// return;

if (!empty($configMore['vars'])) {
foreach ($configMore['vars'] as $mVarName => $mVar) {
if (isset($moreInDb['vars'][$mVarName]['value']) && $mVar['type'] == $moreInDb['vars'][$mVarName]['type']) {
$configMore['vars'][$mVarName]['value'] = $moreInDb['vars'][$mVarName]['value']; //数据库里有这个vars,并且类型一样,则更新这个值到配置文件数组里。 if (isset($moreInDb['vars'][$mVarName]['valueText'])) {
$configMore['vars'][$mVarName]['valueText'] = $moreInDb['vars'][$mVarName]['valueText'];
}
}
}
} if (!empty($configMore['widgets'])) {
foreach ($configMore['widgets'] as $widgetName => $widget) { if (isset($moreInDb['widgets'][$widgetName]['title'])) {
$configMore['widgets'][$widgetName]['title'] = $moreInDb['widgets'][$widgetName]['title'];
} if (isset($moreInDb['widgets'][$widgetName]['display'])) {
$configMore['widgets'][$widgetName]['display'] = $moreInDb['widgets'][$widgetName]['display'];
} if (!empty($widget['vars'])) {
foreach ($widget['vars'] as $widgetVarName => $widgetVar) { if (isset($moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['value']) && $widgetVar['type'] == $moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['type']) {
$configMore['widgets'][$widgetName]['vars'][$widgetVarName]['value'] = $moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['value']; if (isset($moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['valueText'])) {
$configMore['widgets'][$widgetName]['vars'][$widgetVarName]['valueText'] = $moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['valueText'];
}
} }
} }
} return $configMore;
}

问题关键在函数  updateThemeConfigMore($configMore, $moreInDb);   如果json文件有语法错误,$configMore没有正确读取。可能造成配置丢失。

在函数updateThemeConfigMore开头加如下代码输出json生成的数组

echo empty($configMore)."\n";
print_r($configMore); 
return;

模板更新,当json文件有语法错误(如少一个逗号)时,$configMore的值是空数组,empty($configMore)值返回1。

themes/w0s/portal/index
1 Array
(
)

解决方案 updateThemeFiles函数里增加对json转换结果的判断,转换成功再更新数据库。

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; //json没有转换成功就不更新数据库
if(!empty($config)){
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);
//echo "\n".$tplFile."\n";
$more = $this->updateThemeConfigMore($configMore, $moreInDb);
//echo empty($more)."\n"; 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
]); }
}
}

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

  1. 使用 c++ 模板显示实例化解决模板函数声明与实现分离的问题

    问题背景 开始正文之前,做一些背景铺垫,方便读者了解我的工程需求.我的项目是一个客户端消息分发中心,在连接上消息后台后,后台会不定时的给我推送一些消息,我再将它们转发给本机的其它桌面产品去做显示.后台 ...

  2. kafkaStream解析json出错导致程序中断的解决方法

    出错在 KStreamFlatMapValues 方法执行时,由于json异常数据无法解析,结果生成的值为null,报错信息如下: 2018-04-18 19:21:04,776 ERROR [app ...

  3. kafka没配置好,导致服务器重启之后,topic丢失,topic里面的消息也丢失

    转,原文:https://blog.csdn.net/zfszhangyuan/article/details/53389916 ----------------------------------- ...

  4. Linux内核中的GPIO系统之(3):pin controller driver代码分析

    一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datash ...

  5. Linux内核中的GPIO系统之(3):pin controller driver代码分析--devm_kzalloc使用【转】

    转自:http://www.wowotech.net/linux_kenrel/pin-controller-driver.html 一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道 ...

  6. 使用SonarCloud对.NET Core项目进行静态代码分析

    本文将介绍如何使用SonarCloud进行.NET Core项目的静态代码分析.SonarCloud是SonarQube提供的基于云的版本,特别针对于开源项目是免费的. 首先,在sonarcloud. ...

  7. flask模板结构组织(局部模板、宏、模板继承)

    模板结构组织 除了使用函数.过滤器等工具控制模板的输出外,jinja2还提供了一些工具来在宏观上组织模板内容. 局部模板 在Web程序中,我们通常会为每一类页面编写一个独立的模板.比如主页模板.用户资 ...

  8. Android Studio 配置快速生成模板代码

    前言 Android studio 有提供快速生成模板代码的功能,其实这个功能也可以自定义配置.此篇博客将讲解如何使用此功能 进入Settings 选择 Editor > Live Templa ...

  9. LibOpenCM3(二) 项目模板 Makefile分析

    目录 LibOpenCM3(一) Linux下命令行开发环境配置 LibOpenCM3(二) 项目模板 Makefile分析 LibOpenCM3 项目模板 项目模板地址: https://githu ...

随机推荐

  1. struts的logic标签和<bean:write/>详情

    https://baike.baidu.com/item/Logic%E6%A0%87%E7%AD%BE/2292947?fr=aladdin https://blog.csdn.net/applea ...

  2. win7设置管理员权限

    1.在运行中输入:secpol.msc 2.修改设置权限设置 3.在账户中, 将administrator启用并设置密码 将其他用户取消管理原权限,设置为user权限

  3. 数据结构之Hyperloglog

    前置知识 调和平均数 通常我们求一堆数的平均数 就是把一堆数加起来除以这堆数的数量,如 x1, x2, x3, x4, .... ,xn的平均数 H = (x1 + x2 + x3 + x4 + xn ...

  4. 常用的http网页错误代码表---------495引发的一个简单到爆,但基于国内环境只能呵呵呵的血案

    敲代码敲出了个网页错误代码 495. 然后,正常的跑去百度,看了一堆还是没有完整的网页错误代码,应该说国内的环境的网页错误代码表只有官方的那几个,那么只能FQ了. 去到谷歌,一查全是俄语,乐了,明白是 ...

  5. babel7中 corejs 和 corejs2 的区别

    babel7中 corejs 和 corejs2 的区别 最近在给项目升级 webpack4 和 babel7,有一些改变但是变化不大.具体过程可以参考这篇文章 webpack4:连奏中的进化.只是文 ...

  6. 基于JAVA的设计模式之单例模式

    概念 于大二上学期面向对象C++期中考试中有这么道题:一个Computer有多个USB插口,那么意味着这台电脑可以插多个鼠标,但是无论你如何拔插多少个鼠标,桌面上的鼠标一直只显示一个,且多个硬件鼠标都 ...

  7. Netty之WebSocket和四种IO介绍

    Netty简介 一.什么是netty? 高性能 事件驱动 异步非堵塞 基于NIO的客户端,服务器端编程框架 稳定性和伸缩性 二.Netty的使用场景 高性能领域   多线程并发领域   异步通信领域 ...

  8. nmap扫描开放端口

    nmap 192.168.1.1  -p1-65535 指定端口范围使用-p参数,如果不指定要扫描的端口,Nmap默认扫描从1到1024再加上nmap-services列出的端口 nmap-servi ...

  9. Godaddy虚拟主机新建mysql数据库 2019最新

    第一次用狗爹,完全摸不着路子. 网站本地已搭建,不知道数据库是在哪里上传. 百度搜索结果都是四五年前的旧内容,耽误时间. 还是问客服,Godaddy的客服确实不赖 godaddy虚拟主机如何新建数据库 ...

  10. Android中文件加密和解密的实现

    最近项目中需要用到加解密功能,言外之意就是不想让人家在反编译后通过不走心就能获取文件里一些看似有用的信息,但考虑到加解密的简单实现,这里并不使用AES或DES加解密 为了对android中assets ...