作者:Lofanm
项目:thin
public function testFill()
{
$model = new Model('', $this->getConfig());
$data = ['username' => '', 'nickname' => 'nickname', 'phone' => ' 123456', 'hobby' => ['1', '2'], 'cityid' => '1', 'a' => 'a', 'b' => 'b'];
$auto = ['user' => ['__option__' => ['value_fill' => 'username,password,phone', 'exists_fill' => 'nickname'], 'username' => ['strtolower', 'callback'], 'password' => ['md5', 'callback'], 'nickname' => [[&$this, 'fillName'], 'callback', 'cn_'], 'phone' => function ($value, $data) {
return trim($value);
}, 'hobby' => ['', 'serialize'], 'cityid' => ['1', 'ignore'], 'address' => ['address'], 'integral' => 0, ['reg_time', 'time', 'callback'], ['login_time', function ($value, $data) {
return $data['reg_time'];
}], 'ab' => ['a,b', 'serialize']]];
\think\Config::set('auto', $auto);
$result = $model->auto('user')->create($data);
$data['nickname'] = 'cn_nickname';
$data['phone'] = '123456';
$data['hobby'] = serialize($data['hobby']);
$data['address'] = 'address';
$data['integral'] = 0;
$data['reg_time'] = time();
$data['login_time'] = $data['reg_time'];
$data['ab'] = serialize(['a' => 'a', 'b' => 'b']);
unset($data['cityid'], $data['a'], $data['b']);
$this->assertEquals($data, $result);
// 测试带.和*的键名
$data = ['name' => ['a' => 'a', 'b' => 'b'], 'goods' => [0 => [0 => ['item' => 'item', 'price' => ''], 1 => ['item' => 'item2', 'price' => '']]]];
$test = ['name.*' => 'name', 'goods.*.*.price' => 100];
$result = $model->auto($test)->create($data);
$data['name']['a'] = $data['name']['b'] = 'name';
$data['goods'][0][0]['price'] = 100;
$data['goods'][0][1]['price'] = 100;
$this->assertEquals($data, $result);
}
作者:cjang
项目:cwm
/**
* 检查菜单权限
* @return boolean
*/
public static function checkAuth($uid, $node)
{
$adminUsers = \think\Config::get('administrator');
// 当前用户不是超级用户,要做权限验证
if (!in_array($uid, $adminUsers)) {
// 根据控制器 去验证
$nodeId = Db::name('Menu')->where('url', $node)->value('id');
if ($nodeId) {
$nodes = session('user_auth_nodes');
if (!$nodes) {
// 获取当前用户的授权节点
$gIds = self::getGroupIds($uid);
if ($gIds) {
$nodes = Db::name('Auth')->where('id', 'in', $gIds)->column('rules');
$nodes = implode($nodes, ',');
$nodes = trim($nodes, ',');
$nodes = explode(',', $nodes);
session('user_auth_nodes', $nodes);
} else {
return false;
}
}
if (in_array($nodeId, $nodes)) {
return true;
} else {
return false;
}
} else {
// 不存在的节点 不验证
return true;
}
} else {
return true;
}
}
作者:GDdar
项目:cic
/**
* 自动初始化缓存
* @access public
* @return void
*/
public static function init()
{
if (is_null(self::$handler)) {
// 自动初始化缓存
self::connect(Config::get('cache'));
}
}
作者:Dragonbu
项目:god-s_plac
protected function buildCacheContent($module)
{
$content = '';
$path = realpath(APP_PATH . $module) . DS;
// 加载模块配置
$config = \think\Config::load(CONF_PATH . $module . 'config' . CONF_EXT);
// 加载应用状态配置
if ($module && $config['app_status']) {
$config = \think\Config::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT);
}
// 读取扩展配置文件
if ($module && $config['extra_config_list']) {
foreach ($config['extra_config_list'] as $name => $file) {
$filename = CONF_PATH . $module . $file . CONF_EXT;
\think\Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME));
}
}
// 加载别名文件
if (is_file(CONF_PATH . $module . 'alias' . EXT)) {
$content .= '\\think\\Loader::addClassMap(' . var_export(include CONF_PATH . $module . 'alias' . EXT, true) . ');' . PHP_EOL;
}
// 加载行为扩展文件
if (is_file(CONF_PATH . $module . 'tags' . EXT)) {
$content .= '\\think\\Hook::import(' . var_export(include CONF_PATH . $module . 'tags' . EXT, true) . ');' . PHP_EOL;
}
// 加载公共文件
if (is_file($path . 'common' . EXT)) {
$content .= substr(php_strip_whitespace($path . 'common' . EXT), 5) . PHP_EOL;
}
$content .= '\\think\\Config::set(' . var_export(\think\Config::get(), true) . ');';
return $content;
}
作者:livingviru
项目:framewor
/**
* 切换缓存类型 需要配置 cache.type 为 complex
* @access public
* @param string $name 缓存标识
* @return \think\cache\Driver
*/
public static function store($name)
{
if ('complex' == Config::get('cache.type')) {
self::connect(Config::get('cache.' . $name), strtolower($name));
}
return self::$handler;
}
作者:minow
项目:smartthin
/**
* 被调用接口
* 载入Smarty模板引擎,获得相关内容
*/
public function fetch($templateFile, $var)
{
// 载入Smarty文件
include_once CORE_PATH . 'Views/Smarty/Smarty.class.php';
// 实例化
$tpl = new SmartyEngine();
// 是否开启缓存, 模板目录, 编译目录, 缓存目录
$tpl->caching = Config::get('TMPL_CACHE_ON');
$tpl->template_dir = THEME_PATH;
$tpl->compile_dir = CACHE_PATH;
$tpl->cache_dir = TEMP_PATH;
$tpl->debugging = false;
$tpl->left_delimiter = '{{';
$tpl->right_delimiter = '}}';
// 自定义配置
if (C('TMPL_ENGINE_CONFIG')) {
$config = C('TMPL_ENGINE_CONFIG');
foreach ($config as $key => $val) {
$tpl->{$key} = $val;
}
}
// 输出
$tpl->assign($var);
$tpl->display($templateFile);
}
作者:xuyi591
项目:ipensof
/**
* 发送数据到客户端
* @access public
* @param mixed $data 数据
* @param string $type 返回类型
* @param bool $return 是否返回数据
* @return mixed
*/
public function send($data = [], $type = '', $return = false)
{
if ('' == $type) {
$type = $this->type ?: (IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type'));
}
$type = strtolower($type);
$data = $data ?: $this->data;
if (!headers_sent() && isset($this->contentType[$type])) {
header('Content-Type:' . $this->contentType[$type] . '; charset=utf-8');
}
if (is_callable($this->transform)) {
$data = call_user_func_array($this->transform, [$data]);
} else {
switch ($type) {
case 'json':
// 返回JSON数据格式到客户端 包含状态信息
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
break;
case 'jsonp':
// 返回JSON数据格式到客户端 包含状态信息
$handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler');
$data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');';
break;
}
}
APP_HOOK && Hook::listen('return_data', $data);
if ($return) {
return $data;
}
echo $data;
$this->isExit() && exit;
}
作者:yuhongji
项目:thin
function C($name = '', $value = null, $range = '')
{
if (is_null($value) && is_string($name)) {
return \think\Config::get($name, $range);
} else {
return \think\Config::set($name, $value, $range);
}
}
作者:php-angula
项目:thinkphp
public function __construct($config = [])
{
$this->config = ['tpl_path' => App::$modulePath . 'view' . DS, 'tpl_suffix' => Config::get('view.engine_config.suffix') ?: '.html', 'tpl_cache_path' => CACHE_PATH, 'tpl_cache_suffix' => Config::get('view.engine_config.cache') ?: '.php', 'attr' => 'php-'];
$this->template = new \Angular($this->config);
// 初始化模板编译存储器
$storage = Config::get('compile_type') ?: '\\think\\template\\driver\\File';
$this->storage = new $storage();
}
作者:livingviru
项目:cyfthin
/**
* 架构函数 初始化视图类 并采用内置模板引擎
* @access public
*/
public function initView()
{
// 模板引擎参数
if (is_null($this->view)) {
$this->view = \think\View::instance(Config::get());
// 只能这样写,不然view会冲突
}
}
作者:GDdar
项目:cic
/**
* session初始化
* @param array $config
* @return void
* @throws \think\Exception
*/
public static function init(array $config = [])
{
if (empty($config)) {
$config = Config::get('session');
}
// 记录初始化信息
App::$debug && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info');
$isDoStart = false;
if (isset($config['use_trans_sid'])) {
ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
}
// 启动session
if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
ini_set('session.auto_start', 0);
$isDoStart = true;
}
if (isset($config['prefix'])) {
self::$prefix = $config['prefix'];
}
if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
session_id($_REQUEST[$config['var_session_id']]);
} elseif (isset($config['id']) && !empty($config['id'])) {
session_id($config['id']);
}
if (isset($config['name'])) {
session_name($config['name']);
}
if (isset($config['path'])) {
session_save_path($config['path']);
}
if (isset($config['domain'])) {
ini_set('session.cookie_domain', $config['domain']);
}
if (isset($config['expire'])) {
ini_set('session.gc_maxlifetime', $config['expire']);
ini_set('session.cookie_lifetime', $config['expire']);
}
if (isset($config['use_cookies'])) {
ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
}
if (isset($config['cache_limiter'])) {
session_cache_limiter($config['cache_limiter']);
}
if (isset($config['cache_expire'])) {
session_cache_expire($config['cache_expire']);
}
if (!empty($config['type'])) {
// 读取session驱动
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
// 检查驱动类
if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
throw new ClassNotFoundException('error session handler:' . $class, $class);
}
}
if ($isDoStart) {
session_start();
}
}
作者:GDdar
项目:cic
/**
* 调试输出接口
* @access public
* @param Response $response Response对象
* @param array $log 日志信息
* @return bool
*/
public function output(Response $response, array $log = [])
{
$request = Request::instance();
$contentType = $response->getHeader('Content-Type');
$accept = $request->header('accept');
if (strpos($accept, 'application/json') === 0 || $request->isAjax()) {
return false;
} elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
return false;
}
// 获取基本信息
$runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME;
$reqs = number_format(1 / $runtime, 2);
$mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
// 页面Trace信息
if (isset($_SERVER['HTTP_HOST'])) {
$uri = $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$uri = 'cmd:' . implode(' ', $_SERVER['argv']);
}
$base = ['请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri, '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()), '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ', '缓存信息' => Cache::$readTimes . ' reads,' . Cache::$writeTimes . ' writes', '配置加载' => count(Config::get())];
if (session_id()) {
$base['会话信息'] = 'SESSION_ID=' . session_id();
}
$info = Debug::getFile(true);
// 页面Trace信息
$trace = [];
foreach ($this->config['trace_tabs'] as $name => $title) {
$name = strtolower($name);
switch ($name) {
case 'base':
// 基本信息
$trace[$title] = $base;
break;
case 'file':
// 文件信息
$trace[$title] = $info;
break;
default:
// 调试信息
if (strpos($name, '|')) {
// 多组信息
$names = explode('|', $name);
$result = [];
foreach ($names as $name) {
$result = array_merge($result, isset($log[$name]) ? $log[$name] : []);
}
$trace[$title] = $result;
} else {
$trace[$title] = isset($log[$name]) ? $log[$name] : '';
}
}
}
// 调用Trace页面模板
ob_start();
include $this->config['trace_file'];
return ob_get_clean();
}
作者:dingyi-Histor
项目:ime-daimaduan.c
/**
* 返回封装后的API数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param string $msg 提示信息
* @param integer $code 返回的code
* @param string $url 重定向地址
* @param integer $wait 跳转等待时间
* @return void
*/
public function result($data = '', $msg = '', $code = 0, $url = '', $wait = 0)
{
$result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait];
if ('html' == Config::get('default_return_type')) {
return $this->fetch(Config::get('dispatch_jump_tmpl'), $result);
} else {
return $result;
}
}
作者:cnzi
项目:thin
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return bool
*/
public function save(array $log = [])
{
if (IS_AJAX || IS_CLI || IS_API || 'html' != Config::get('default_return_type')) {
// ajax cli api方式下不输出
return false;
}
// 获取基本信息
$runtime = microtime(true) - START_TIME;
$reqs = number_format(1 / $runtime, 2);
$runtime = number_format($runtime, 6);
$mem = number_format((memory_get_usage() - START_MEM) / 1024, 2);
// 页面Trace信息
$base = ['请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ] 内存消耗:{$mem}kb 文件加载:" . count(get_included_files()), '查询信息' => \think\Db::$queryTimes . ' queries ' . \think\Db::$executeTimes . ' writes ', '缓存信息' => \think\Cache::$readTimes . ' reads,' . \think\Cache::$writeTimes . ' writes', '配置加载' => count(Config::get())];
if (session_id()) {
$base['会话信息'] = 'SESSION_ID=' . session_id();
}
$info = Debug::getFile(true);
// 获取调试日志
$debug = [];
foreach ($log as $line) {
$debug[$line['type']][] = $line['msg'];
}
// 页面Trace信息
$trace = [];
foreach ($this->config['trace_tabs'] as $name => $title) {
$name = strtolower($name);
switch ($name) {
case 'base':
// 基本信息
$trace[$title] = $base;
break;
case 'file':
// 文件信息
$trace[$title] = $info;
break;
default:
// 调试信息
if (strpos($name, '|')) {
// 多组信息
$names = explode('|', $name);
$result = [];
foreach ($names as $name) {
$result = array_merge($result, isset($debug[$name]) ? $debug[$name] : []);
}
$trace[$title] = $result;
} else {
$trace[$title] = isset($debug[$name]) ? $debug[$name] : '';
}
}
}
// 调用Trace页面模板
ob_start();
include $this->config['trace_file'];
echo ob_get_clean();
return true;
}
作者:livingviru
项目:cyfthin
/**
* Fetch the encryption key
*
* Returns it as MD5 in order to have an exact-length 128 bit key.
* Mcrypt is sensitive to keys that are not the correct length
*
* @access public
* @param string
* @return string
*/
public function getKey($key = '')
{
if ($key == '') {
if ($this->encryption_key != '') {
return $this->encryption_key;
}
$key = \think\Config::get('encryption_key');
}
return md5($key);
}
作者:guozqi
项目:thin
public function testConfig()
{
App::run(Config::get());
Config::parse('isTrue=1', 'test');
Config::range('test');
$this->assertTrue(Config::has('isTrue'));
$this->assertEquals(1, Config::get('isTrue'));
Config::set('isTrue', false);
$this->assertEquals(0, Config::get('isTrue'));
Config::reset();
}
作者:top-thin
项目:framewor
public function testRun()
{
$response = App::run(Request::create("http://www.example.com"));
$expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
$this->assertEquals($expectOutputString, $response->getContent());
$this->assertEquals(200, $response->getCode());
$this->assertEquals(true, function_exists('lang'));
$this->assertEquals(true, function_exists('config'));
$this->assertEquals(true, function_exists('input'));
$this->assertEquals(Config::get('default_timezone'), date_default_timezone_get());
}
作者:lw7866580
项目:thin
public function testConfig()
{
App::run();
$this->assertTrue(Config::has('url_route_on'));
$this->assertEquals(1, Config::get('url_route_on'));
Config::set('url_route_on', false);
$this->assertEquals(0, Config::get('url_route_on'));
Config::range('test');
$this->assertFalse(Config::has('url_route_on'));
Config::reset();
}
作者:xuyi591
项目:ipensof
private static function init()
{
// 加载初始化文件
if (is_file(APP_PATH . 'init' . EXT)) {
include APP_PATH . 'init' . EXT;
// 加载模块配置
$config = Config::get();
} else {
// 加载模块配置
$config = Config::load(APP_PATH . 'config' . EXT);
// 加载应用状态配置
if ($config['app_status']) {
$config = Config::load(APP_PATH . $config['app_status'] . EXT);
}
// 读取扩展配置文件
if ($config['extra_config_list']) {
foreach ($config['extra_config_list'] as $name => $file) {
$filename = APP_PATH . $file . EXT;
Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME));
}
}
// 加载别名文件
if (is_file(APP_PATH . 'alias' . EXT)) {
Loader::addMap(include APP_PATH . 'alias' . EXT);
}
// 加载行为扩展文件
if (APP_HOOK && is_file(APP_PATH . 'tags' . EXT)) {
Hook::import(include APP_PATH . 'tags' . EXT);
}
// 加载公共文件
if (is_file(APP_PATH . 'common' . EXT)) {
include APP_PATH . 'common' . EXT;
}
}
// 注册根命名空间
if (!empty($config['root_namespace'])) {
Loader::addNamespace($config['root_namespace']);
}
// 加载额外文件
if (!empty($config['extra_file_list'])) {
foreach ($config['extra_file_list'] as $file) {
$file = strpos($file, '.') ? $file : APP_PATH . $file . EXT;
if (is_file($file)) {
include_once $file;
}
}
}
// 设置系统时区
date_default_timezone_set($config['default_timezone']);
// 监听app_init
APP_HOOK && Hook::listen('app_init');
}
作者:minow
项目:smartthin
/**
* 架构函数
* 遍历合并Config
*
* @return void
*/
public function __construct()
{
if (!empty($this->options)) {
foreach ($this->options as $name => $val) {
// 参数已设置 则覆盖行为参数
if (Config::get($name) !== null) {
$this->options[$name] = C($name);
} else {
Config::set($name, $val);
}
}
array_change_key_case($this->options);
}
}