vr-shopxo-source/application/service/BaseConfigHandleService.php

204 lines
6.8 KiB
PHP
Raw Normal View History

2020-09-25 15:08:23 +00:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
/**
* 基础配置处理服务层
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-25
* @desc description
*/
class BaseConfigHandleService
{
/**
* 运行入口
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-25
* @desc description
* @param [array] $params [输入参数]
*/
public static function Run($params = [])
{
// session配置
$ret = self::SessionHandle($params);
if($ret['code'] != 0)
{
return $ret;
}
// cache配置
$ret = self::CacheHandle($params);
if($ret['code'] != 0)
{
return $ret;
}
}
/**
* session配置处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-25
* @desc description
* @param [array] $params [输入参数]
*/
public static function SessionHandle($params = [])
{
if(MyC('common_session_is_use_cache') == 1)
{
$config = [
// 使用redis
'type' => 'redis',
// 连接地址
2020-09-26 10:27:39 +00:00
'host' => MyC('common_cache_session_redis_host', '127.0.0.1', true),
2020-09-25 15:08:23 +00:00
// 端口号
2020-09-26 10:27:39 +00:00
'port' => MyC('common_cache_session_redis_port', 6379, true),
2020-09-25 15:08:23 +00:00
// 密码
2020-09-26 10:27:39 +00:00
'password' => MyC('common_cache_session_redis_password', '', true),
2020-09-25 15:08:23 +00:00
// 全局缓存有效期、默认3600秒
2020-09-26 10:27:39 +00:00
'expire' => MyC('common_cache_session_redis_expire', 3600, true),
2020-09-25 15:08:23 +00:00
// 缓存前缀
2020-09-26 10:27:39 +00:00
'prefix' => MyC('common_cache_session_redis_prefix', 'shopxo', true),
2020-09-25 15:08:23 +00:00
];
} else {
$config = [
// session_id
2020-09-25 15:37:04 +00:00
'id' => '',
2020-09-25 15:08:23 +00:00
// SESSION_ID的提交变量,解决flash上传跨域
2020-09-25 15:37:04 +00:00
'var_session_id' => '',
2020-09-25 15:08:23 +00:00
// SESSION 前缀
2020-09-25 15:37:04 +00:00
'prefix' => 'shopxo',
2020-09-25 15:08:23 +00:00
// 驱动方式 支持redis memcache memcached
2020-09-25 15:37:04 +00:00
'type' => '',
// 过期时间(默认3600秒)
'expire' => 3600,
2020-09-25 15:08:23 +00:00
// 是否自动开启 SESSION
2020-09-25 15:37:04 +00:00
'auto_start' => true,
2020-09-25 15:08:23 +00:00
];
}
2020-09-25 15:37:04 +00:00
2020-09-25 15:08:23 +00:00
// 配置文件
2020-09-25 15:37:04 +00:00
$file_dir = ROOT.'config'.DS;
$file_name = 'session.php';
2020-09-25 15:08:23 +00:00
// 保存文件
2020-09-25 15:37:04 +00:00
return self::ConfigFileSave($file_dir, $file_name, $config, 'Session配置');
2020-09-25 15:08:23 +00:00
}
/**
* cache配置处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-25
* @desc description
* @param [array] $params [输入参数]
*/
public static function CacheHandle($params = [])
{
// 是否使用缓存
if(MyC('common_data_is_use_cache') == 1)
{
$config = [
// 使用redis
'type' => 'redis',
// 连接地址
2020-09-26 10:27:39 +00:00
'host' => MyC('common_cache_data_redis_host', '127.0.0.1', true),
2020-09-25 15:08:23 +00:00
// 端口号
2020-09-26 10:27:39 +00:00
'port' => MyC('common_cache_data_redis_port', 6379, true),
2020-09-25 15:08:23 +00:00
// 密码
2020-09-26 10:27:39 +00:00
'password' => MyC('common_cache_data_redis_password', '', true),
2020-09-25 15:08:23 +00:00
// 全局缓存有效期0为永久有效
2020-09-26 10:27:39 +00:00
'expire' => MyC('common_cache_data_redis_expire', 0, true),
2020-09-25 15:08:23 +00:00
// 缓存前缀
2020-09-26 10:27:39 +00:00
'prefix' => MyC('common_cache_data_redis_prefix', 'shopxo', true),
2020-09-25 15:08:23 +00:00
];
} else {
$config = [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => '',
// 缓存前缀
'prefix' => 'shopxo',
// 缓存有效期 0表示永久缓存
'expire' => 0,
];
}
// 配置文件
2020-09-25 15:37:04 +00:00
$file_dir = ROOT.'config'.DS;
$file_name = 'cache.php';
2020-09-25 15:08:23 +00:00
// 保存文件
2020-09-25 15:37:04 +00:00
return self::ConfigFileSave($file_dir, $file_name, $config, '缓存配置');
2020-09-25 15:08:23 +00:00
}
/**
* 配置文件保存
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-09-25
* @desc description
2020-09-25 15:37:04 +00:00
* @param [string] $file_dir [文件路径]
* @param [string] $file_name [文件名称]
* @param [array] $config [配置信息]
* @param [string] $name [描述名称]
2020-09-25 15:08:23 +00:00
*/
2020-09-25 15:37:04 +00:00
private static function ConfigFileSave($file_dir, $file_name, $config, $name)
2020-09-25 15:08:23 +00:00
{
// 是否有写权限
2020-09-25 15:37:04 +00:00
$config_file = $file_dir.$file_name;
if(file_exists($config_file))
2020-09-25 15:08:23 +00:00
{
2020-09-25 15:37:04 +00:00
if(!is_writable($config_file))
{
return DataReturn($name.'文件没有操作权限'.'['.$config_file.']', -10);
}
} else {
if(!is_dir($file_dir))
{
return DataReturn($name.'路径不存在'.'['.$file_dir.']', -11);
}
if(!is_writable($file_dir))
{
return DataReturn($name.'路径没有操作权限'.'['.$file_dir.']', -12);
}
2020-09-25 15:08:23 +00:00
}
// 生成配置文件
$ret = @file_put_contents($config_file, "<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
// {$name}\nreturn ".var_export($config, true).";\n?>");
if($ret === false)
{
2020-09-25 15:37:04 +00:00
return DataReturn($name.'处理失败['.$config_file.']', -100);
2020-09-25 15:08:23 +00:00
}
return DataReturn('处理成功', 0);
}
}
?>