取消vendor文件夹的排除

This commit is contained in:
win7 committed 2019-12-24 09:42:21 +08:00
1 parent 55d4d7b9c8
commit 1eafa8f5c9
1279 files changed
+277858 -1

No files matched your search

+663
View File
@@ -0,0 +1,663 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
//------------------------
// ThinkPHP 助手函数
//-------------------------
use think\App;
use think\Container;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Env;
use think\facade\Event;
use think\facade\Lang;
use think\facade\Log;
use think\facade\Request;
use think\facade\Route;
use think\facade\Session;
use think\Response;
use think\response\File;
use think\response\Json;
use think\response\Jsonp;
use think\response\Redirect;
use think\response\View;
use think\response\Xml;
use think\route\Url as UrlBuild;
use think\Validate;
if (!function_exists('abort')) {
/**
* 抛出HTTP异常
* @param integer|Response $code 状态码 或者 Response对象实例
* @param string $message 错误信息
* @param array $header 参数
*/
function abort($code, string $message = '', array $header = [])
{
if ($code instanceof Response) {
throw new HttpResponseException($code);
} else {
throw new HttpException($code, $message, null, $header);
}
}
}
if (!function_exists('app')) {
/**
* 快速获取容器中的实例 支持依赖注入
* @param string $name 类名或标识 默认获取当前应用实例
* @param array $args 参数
* @param bool $newInstance 是否每次创建新的实例
* @return object|App
*/
function app(string $name = '', array $args = [], bool $newInstance = false)
{
return Container::getInstance()->make($name ?: App::class, $args, $newInstance);
}
}
if (!function_exists('bind')) {
/**
* 绑定一个类到容器
* @param string|array $abstract 类标识、接口(支持批量绑定)
* @param mixed $concrete 要绑定的类、闭包或者实例
* @return Container
*/
function bind($abstract, $concrete = null)
{
return Container::getInstance()->bind($abstract, $concrete);
}
}
if (!function_exists('cache')) {
/**
* 缓存管理
* @param string $name 缓存名称
* @param mixed $value 缓存值
* @param mixed $options 缓存参数
* @param string $tag 缓存标签
* @return mixed
*/
function cache(string $name = null, $value = '', $options = null, $tag = null)
{
if (is_null($name)) {
return app('cache');
}
if ('' === $value) {
// 获取缓存
return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name);
} elseif (is_null($value)) {
// 删除缓存
return Cache::delete($name);
}
// 缓存数据
if (is_array($options)) {
$expire = $options['expire'] ?? null; //修复查询缓存无法设置过期时间
} else {
$expire = $options;
}
if (is_null($tag)) {
return Cache::set($name, $value, $expire);
} else {
return Cache::tag($tag)->set($name, $value, $expire);
}
}
}
if (!function_exists('config')) {
/**
* 获取和设置配置参数
* @param string|array $name 参数名
* @param mixed $value 参数值
* @return mixed
*/
function config($name = '', $value = null)
{
if (is_array($name)) {
return Config::set($name, $value);
}
return 0 === strpos($name, '?') ? Config::has(substr($name, 1)) : Config::get($name, $value);
}
}
if (!function_exists('cookie')) {
/**
* Cookie管理
* @param string $name cookie名称
* @param mixed $value cookie值
* @param mixed $option 参数
* @return mixed
*/
function cookie(string $name, $value = '', $option = null)
{
if (is_null($value)) {
// 删除
Cookie::delete($name);
} elseif ('' === $value) {
// 获取
return 0 === strpos($name, '?') ? Cookie::has(substr($name, 1)) : Cookie::get($name);
} else {
// 设置
return Cookie::set($name, $value, $option);
}
}
}
if (!function_exists('download')) {
/**
* 获取\think\response\Download对象实例
* @param string $filename 要下载的文件
* @param string $name 显示文件名
* @param bool $content 是否为内容
* @param int $expire 有效期(秒)
* @return \think\response\File
*/
function download(string $filename, string $name = '', bool $content = false, int $expire = 180): File
{
return Response::create($filename, 'file')->name($name)->isContent($content)->expire($expire);
}
}
if (!function_exists('dump')) {
/**
* 浏览器友好的变量输出
* @param mixed $vars 要输出的变量
* @return void
*/
function dump(...$vars)
{
ob_start();
var_dump(...$vars);
$output = ob_get_clean();
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
if (PHP_SAPI == 'cli') {
$output = PHP_EOL . $output . PHP_EOL;
} else {
if (!extension_loaded('xdebug')) {
$output = htmlspecialchars($output, ENT_SUBSTITUTE);
}
$output = '<pre>' . $output . '</pre>';
}
echo $output;
}
}
if (!function_exists('env')) {
/**
* 获取环境变量值
* @access public
* @param string $name 环境变量名(支持二级 .号分割)
* @param string $default 默认值
* @return mixed
*/
function env(string $name = null, $default = null)
{
return Env::get($name, $default);
}
}
if (!function_exists('event')) {
/**
* 触发事件
* @param mixed $event 事件名(或者类名)
* @param mixed $args 参数
* @return mixed
*/
function event($event, $args = null)
{
return Event::trigger($event, $args);
}
}
if (!function_exists('halt')) {
/**
* 调试变量并且中断输出
* @param mixed $vars 调试变量或者信息
*/
function halt(...$vars)
{
dump(...$vars);
throw new HttpResponseException(Response::create());
}
}
if (!function_exists('input')) {
/**
* 获取输入数据 支持默认值和过滤
* @param string $key 获取的变量名
* @param mixed $default 默认值
* @param string $filter 过滤方法
* @return mixed
*/
function input(string $key = '', $default = null, $filter = '')
{
if (0 === strpos($key, '?')) {
$key = substr($key, 1);
$has = true;
}
if ($pos = strpos($key, '.')) {
// 指定参数来源
$method = substr($key, 0, $pos);
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'route', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
$key = substr($key, $pos + 1);
if ('server' == $method && is_null($default)) {
$default = '';
}
} else {
$method = 'param';
}
} else {
// 默认为自动判断
$method = 'param';
}
return isset($has) ?
request()->has($key, $method) :
request()->$method($key, $default, $filter);
}
}
if (!function_exists('invoke')) {
/**
* 调用反射实例化对象或者执行方法 支持依赖注入
* @param mixed $call 类名或者callable
* @param array $args 参数
* @return mixed
*/
function invoke($call, array $args = [])
{
if (is_callable($call)) {
return Container::getInstance()->invoke($call, $args);
}
return Container::getInstance()->invokeClass($call, $args);
}
}
if (!function_exists('json')) {
/**
* 获取\think\response\Json对象实例
* @param mixed $data 返回的数据
* @param int $code 状态码
* @param array $header 头部
* @param array $options 参数
* @return \think\response\Json
*/
function json($data = [], $code = 200, $header = [], $options = []): Json
{
return Response::create($data, 'json', $code)->header($header)->options($options);
}
}
if (!function_exists('jsonp')) {
/**
* 获取\think\response\Jsonp对象实例
* @param mixed $data 返回的数据
* @param int $code 状态码
* @param array $header 头部
* @param array $options 参数
* @return \think\response\Jsonp
*/
function jsonp($data = [], $code = 200, $header = [], $options = []): Jsonp
{
return Response::create($data, 'jsonp', $code)->header($header)->options($options);
}
}
if (!function_exists('lang')) {
/**
* 获取语言变量值
* @param string $name 语言变量名
* @param array $vars 动态变量值
* @param string $lang 语言
* @return mixed
*/
function lang(string $name, array $vars = [], string $lang = '')
{
return Lang::get($name, $vars, $lang);
}
}
if (!function_exists('parse_name')) {
/**
* 字符串命名风格转换
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
* @param string $name 字符串
* @param int $type 转换类型
* @param bool $ucfirst 首字母是否大写(驼峰规则)
* @return string
*/
function parse_name(string $name, int $type = 0, bool $ucfirst = true): string
{
if ($type) {
$name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
return strtoupper($match[1]);
}, $name);
return $ucfirst ? ucfirst($name) : lcfirst($name);
}
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}
if (!function_exists('redirect')) {
/**
* 获取\think\response\Redirect对象实例
* @param string $url 重定向地址
* @param int $code 状态码
* @return \think\response\Redirect
*/
function redirect(string $url, int $code = 302): Redirect
{
return Response::create($url, 'redirect', $code);
}
}
if (!function_exists('request')) {
/**
* 获取当前Request对象实例
* @return Request
*/
function request(): \think\Request
{
return app('request');
}
}
if (!function_exists('response')) {
/**
* 创建普通 Response 对象实例
* @param mixed $data 输出数据
* @param int|string $code 状态码
* @param array $header 头信息
* @param string $type
* @return Response
*/
function response($data = '', $code = 200, $header = [], $type = 'html'): Response
{
return Response::create($data, $type, $code)->header($header);
}
}
if (!function_exists('session')) {
/**
* Session管理
* @param string $name session名称
* @param mixed $value session值
* @return mixed
*/
function session($name = '', $value = '')
{
if (is_null($name)) {
// 清除
Session::clear();
} elseif ('' === $name) {
return Session::all();
} elseif (is_null($value)) {
// 删除
Session::delete($name);
} elseif ('' === $value) {
// 判断或获取
return 0 === strpos($name, '?') ? Session::has(substr($name, 1)) : Session::get($name);
} else {
// 设置
Session::set($name, $value);
}
}
}
if (!function_exists('token')) {
/**
* 获取Token令牌
* @param string $name 令牌名称
* @param mixed $type 令牌生成方法
* @return string
*/
function token(string $name = '__token__', string $type = 'md5'): string
{
return Request::buildToken($name, $type);
}
}
if (!function_exists('token_field')) {
/**
* 生成令牌隐藏表单
* @param string $name 令牌名称
* @param mixed $type 令牌生成方法
* @return string
*/
function token_field(string $name = '__token__', string $type = 'md5'): string
{
$token = Request::buildToken($name, $type);
return '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
}
}
if (!function_exists('token_meta')) {
/**
* 生成令牌meta
* @param string $name 令牌名称
* @param mixed $type 令牌生成方法
* @return string
*/
function token_meta(string $name = '__token__', string $type = 'md5'): string
{
$token = Request::buildToken($name, $type);
return '<meta name="csrf-token" content="' . $token . '">';
}
}
if (!function_exists('trace')) {
/**
* 记录日志信息
* @param mixed $log log信息 支持字符串和数组
* @param string $level 日志级别
* @return array|void
*/
function trace($log = '[think]', string $level = 'log')
{
if ('[think]' === $log) {
return Log::getLog();
}
Log::record($log, $level);
}
}
if (!function_exists('url')) {
/**
* Url生成
* @param string $url 路由地址
* @param array $vars 变量
* @param bool|string $suffix 生成的URL后缀
* @param bool|string $domain 域名
* @return UrlBuild
*/
function url(string $url = '', array $vars = [], $suffix = true, $domain = false): UrlBuild
{
return Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
}
}
if (!function_exists('validate')) {
/**
* 生成验证对象
* @param string|array $validate 验证器类名或者验证规则数组
* @param array $message 错误提示信息
* @param bool $batch 是否批量验证
* @param bool $failException 是否抛出异常
* @return Validate
*/
function validate($validate = '', array $message = [], bool $batch = false, bool $failException = true): Validate
{
if (is_array($validate) || '' === $validate) {
$v = new Validate();
if (is_array($validate)) {
$v->rule($validate);
}
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : app()->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
return $v->message($message)->batch($batch)->failException($failException);
}
}
if (!function_exists('view')) {
/**
* 渲染模板输出
* @param string $template 模板文件
* @param array $vars 模板变量
* @param int $code 状态码
* @param callable $filter 内容过滤
* @return \think\response\View
*/
function view(string $template = '', $vars = [], $code = 200, $filter = null): View
{
return Response::create($template, 'view', $code)->assign($vars)->filter($filter);
}
}
if (!function_exists('display')) {
/**
* 渲染模板输出
* @param string $content 渲染内容
* @param array $vars 模板变量
* @param int $code 状态码
* @param callable $filter 内容过滤
* @return \think\response\View
*/
function display(string $content, $vars = [], $code = 200, $filter = null): View
{
return Response::create($content, 'view', $code)->isContent(true)->assign($vars)->filter($filter);
}
}
if (!function_exists('xml')) {
/**
* 获取\think\response\Xml对象实例
* @param mixed $data 返回的数据
* @param int $code 状态码
* @param array $header 头部
* @param array $options 参数
* @return \think\response\Xml
*/
function xml($data = [], $code = 200, $header = [], $options = []): Xml
{
return Response::create($data, 'xml', $code)->header($header)->options($options);
}
}
if (!function_exists('app_path')) {
/**
* 获取当前应用目录
*
* @param string $path
* @return string
*/
function app_path($path = '')
{
return app()->getAppPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
}
}
if (!function_exists('base_path')) {
/**
* 获取应用基础目录
*
* @param string $path
* @return string
*/
function base_path($path = '')
{
return app()->getBasePath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
}
}
if (!function_exists('config_path')) {
/**
* 获取应用配置目录
*
* @param string $path
* @return string
*/
function config_path($path = '')
{
return app()->getConfigPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
}
}
if (!function_exists('public_path')) {
/**
* 获取web根目录
*
* @param string $path
* @return string
*/
function public_path($path = '')
{
return app()->getRootPath() . ($path ? ltrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $path);
}
}
if (!function_exists('runtime_path')) {
/**
* 获取应用运行时目录
*
* @param string $path
* @return string
*/
function runtime_path($path = '')
{
return app()->getRuntimePath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
}
}
if (!function_exists('root_path')) {
/**
* 获取项目根目录
*
* @param string $path
* @return string
*/
function root_path($path = '')
{
return app()->getRootPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
}
}
+148
View File
@@ -0,0 +1,148 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 核心中文语言包
return [
// 系统错误提示
'Undefined variable' => '未定义变量',
'Undefined index' => '未定义数组索引',
'Undefined offset' => '未定义数组下标',
'Parse error' => '语法解析错误',
'Type error' => '类型错误',
'Fatal error' => '致命错误',
'syntax error' => '语法错误',
// 框架核心错误提示
'dispatch type not support' => '不支持的调度类型',
'method param miss' => '方法参数错误',
'method not exists' => '方法不存在',
'function not exists' => '函数不存在',
'app not exists' => '应用不存在',
'controller not exists' => '控制器不存在',
'class not exists' => '类不存在',
'property not exists' => '类的属性不存在',
'template not exists' => '模板文件不存在',
'illegal controller name' => '非法的控制器名称',
'illegal action name' => '非法的操作名称',
'url suffix deny' => '禁止的URL后缀访问',
'Undefined cache config' => '缓存配置未定义',
'Route Not Found' => '当前访问路由未定义或不匹配',
'Undefined db config' => '数据库配置未定义',
'Undefined log config' => '日志配置未定义',
'Undefined db type' => '未定义数据库类型',
'variable type error' => '变量类型错误',
'PSR-4 error' => 'PSR-4 规范错误',
'not support type' => '不支持的分页索引字段类型',
'not support total' => '简洁模式下不能获取数据总数',
'not support last' => '简洁模式下不能获取最后一页',
'error session handler' => '错误的SESSION处理器类',
'not allow php tag' => '模板不允许使用PHP语法',
'not support' => '不支持',
'database config error' => '数据库配置信息错误',
'redisd master' => 'Redisd 主服务器错误',
'redisd slave' => 'Redisd 从服务器错误',
'must run at sae' => '必须在SAE运行',
'memcache init error' => '未开通Memcache服务,请在SAE管理平台初始化Memcache服务',
'KVDB init error' => '没有初始化KVDB,请在SAE管理平台初始化KVDB服务',
'fields not exists' => '数据表字段不存在',
'where express error' => '查询表达式错误',
'no data to update' => '没有任何数据需要更新',
'miss data to insert' => '缺少需要写入的数据',
'miss complex primary data' => '缺少复合主键数据',
'miss update condition' => '缺少更新条件',
'model data Not Found' => '模型数据不存在',
'table data not Found' => '表数据不存在',
'delete without condition' => '没有条件不会执行删除操作',
'miss relation data' => '缺少关联表数据',
'tag attr must' => '模板标签属性必须',
'tag error' => '模板标签错误',
'cache write error' => '缓存写入失败',
'sae mc write error' => 'SAE mc 写入错误',
'route name not exists' => '路由标识不存在(或参数不够)',
'invalid request' => '非法请求',
'bind attr has exists' => '模型的属性已经存在',
'relation data not exists' => '关联数据不存在',
'relation not support' => '关联不支持',
'chunk not support order' => 'Chunk不支持调用order方法',
'route pattern error' => '路由变量规则定义错误',
'route behavior will not support' => '路由行为废弃(使用中间件替代)',
'closure not support cache(true)' => '使用闭包查询不支持cache(true),请指定缓存Key',
// 上传错误信息
'unknown upload error' => '未知上传错误!',
'file write error' => '文件写入失败!',
'upload temp dir not found' => '找不到临时文件夹!',
'no file to uploaded' => '没有文件被上传!',
'only the portion of file is uploaded' => '文件只有部分被上传!',
'upload File size exceeds the maximum value' => '上传文件大小超过了最大值!',
'upload write error' => '文件上传保存错误!',
'has the same filename: {:filename}' => '存在同名文件:{:filename}',
'upload illegal files' => '非法上传文件',
'illegal image files' => '非法图片文件',
'extensions to upload is not allowed' => '上传文件后缀不允许',
'mimetype to upload is not allowed' => '上传文件MIME类型不允许!',
'filesize not match' => '上传文件大小不符!',
'directory {:path} creation failed' => '目录 {:path} 创建失败!',
'The middleware must return Response instance' => '中间件方法必须返回Response对象实例',
'The queue was exhausted, with no response returned' => '中间件队列为空',
// Validate Error Message
':attribute require' => ':attribute不能为空',
':attribute must' => ':attribute必须',
':attribute must be numeric' => ':attribute必须是数字',
':attribute must be integer' => ':attribute必须是整数',
':attribute must be float' => ':attribute必须是浮点数',
':attribute must be bool' => ':attribute必须是布尔值',
':attribute not a valid email address' => ':attribute格式不符',
':attribute not a valid mobile' => ':attribute格式不符',
':attribute must be a array' => ':attribute必须是数组',
':attribute must be yes,on or 1' => ':attribute必须是yes、on或者1',
':attribute not a valid datetime' => ':attribute不是一个有效的日期或时间格式',
':attribute not a valid file' => ':attribute不是有效的上传文件',
':attribute not a valid image' => ':attribute不是有效的图像文件',
':attribute must be alpha' => ':attribute只能是字母',
':attribute must be alpha-numeric' => ':attribute只能是字母和数字',
':attribute must be alpha-numeric, dash, underscore' => ':attribute只能是字母、数字和下划线_及破折号-',
':attribute not a valid domain or ip' => ':attribute不是有效的域名或者IP',
':attribute must be chinese' => ':attribute只能是汉字',
':attribute must be chinese or alpha' => ':attribute只能是汉字、字母',
':attribute must be chinese,alpha-numeric' => ':attribute只能是汉字、字母和数字',
':attribute must be chinese,alpha-numeric,underscore, dash' => ':attribute只能是汉字、字母、数字和下划线_及破折号-',
':attribute not a valid url' => ':attribute不是有效的URL地址',
':attribute not a valid ip' => ':attribute不是有效的IP地址',
':attribute must be dateFormat of :rule' => ':attribute必须使用日期格式 :rule',
':attribute must be in :rule' => ':attribute必须在 :rule 范围内',
':attribute be notin :rule' => ':attribute不能在 :rule 范围内',
':attribute must between :1 - :2' => ':attribute只能在 :1 - :2 之间',
':attribute not between :1 - :2' => ':attribute不能在 :1 - :2 之间',
'size of :attribute must be :rule' => ':attribute长度不符合要求 :rule',
'max size of :attribute must be :rule' => ':attribute长度不能超过 :rule',
'min size of :attribute must be :rule' => ':attribute长度不能小于 :rule',
':attribute cannot be less than :rule' => ':attribute日期不能小于 :rule',
':attribute cannot exceed :rule' => ':attribute日期不能超过 :rule',
':attribute not within :rule' => '不在有效期内 :rule',
'access IP is not allowed' => '不允许的IP访问',
'access IP denied' => '禁止的IP访问',
':attribute out of accord with :2' => ':attribute和确认字段:2不一致',
':attribute cannot be same with :2' => ':attribute和比较字段:2不能相同',
':attribute must greater than or equal :rule' => ':attribute必须大于等于 :rule',
':attribute must greater than :rule' => ':attribute必须大于 :rule',
':attribute must less than or equal :rule' => ':attribute必须小于等于 :rule',
':attribute must less than :rule' => ':attribute必须小于 :rule',
':attribute must equal :rule' => ':attribute必须等于 :rule',
':attribute has exists' => ':attribute已存在',
':attribute not conform to the rules' => ':attribute不符合指定规则',
'invalid Request method' => '无效的请求类型',
'invalid token' => '令牌数据无效',
'not conform to the rules' => '规则错误',
'record has update' => '记录已经被更新了',
];
+610
View File
@@ -0,0 +1,610 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think;
use think\event\AppInit;
use think\helper\Str;
use think\initializer\BootService;
use think\initializer\Error;
use think\initializer\RegisterService;
/**
* App 基础类
* @property Route $route
* @property Config $config
* @property Cache $cache
* @property Request $request
* @property Http $http
* @property Console $console
* @property Env $env
* @property Event $event
* @property Middleware $middleware
* @property Log $log
* @property Lang $lang
* @property Db $db
* @property Cookie $cookie
* @property Session $session
* @property Validate $validate
* @property Filesystem $filesystem
*/
class App extends Container
{
const VERSION = '6.0.0';
/**
* 应用调试模式
* @var bool
*/
protected $appDebug = false;
/**
* 应用开始时间
* @var float
*/
protected $beginTime;
/**
* 应用内存初始占用
* @var integer
*/
protected $beginMem;
/**
* 当前应用类库命名空间
* @var string
*/
protected $namespace = 'app';
/**
* 应用根目录
* @var string
*/
protected $rootPath = '';
/**
* 框架目录
* @var string
*/
protected $thinkPath = '';
/**
* 应用目录
* @var string
*/
protected $appPath = '';
/**
* Runtime目录
* @var string
*/
protected $runtimePath = '';
/**
* 路由定义目录
* @var string
*/
protected $routePath = '';
/**
* 配置后缀
* @var string
*/
protected $configExt = '.php';
/**
* 应用初始化器
* @var array
*/
protected $initializers = [
Error::class,
RegisterService::class,
BootService::class,
];
/**
* 注册的系统服务
* @var array
*/
protected $services = [];
/**
* 初始化
* @var bool
*/
protected $initialized = false;
/**
* 容器绑定标识
* @var array
*/
protected $bind = [
'app' => App::class,
'cache' => Cache::class,
'config' => Config::class,
'console' => Console::class,
'cookie' => Cookie::class,
'db' => Db::class,
'env' => Env::class,
'event' => Event::class,
'http' => Http::class,
'lang' => Lang::class,
'log' => Log::class,
'middleware' => Middleware::class,
'request' => Request::class,
'response' => Response::class,
'route' => Route::class,
'session' => Session::class,
'validate' => Validate::class,
'view' => View::class,
'filesystem' => Filesystem::class,
'think\DbManager' => Db::class,
'think\LogManager' => Log::class,
'think\CacheManager' => Cache::class,
// 接口依赖注入
'Psr\Log\LoggerInterface' => Log::class,
];
/**
* 架构方法
* @access public
* @param string $rootPath 应用根目录
*/
public function __construct(string $rootPath = '')
{
$this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR;
$this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();
$this->appPath = $this->rootPath . 'app' . DIRECTORY_SEPARATOR;
$this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;
if (is_file($this->appPath . 'provider.php')) {
$this->bind(include $this->appPath . 'provider.php');
}
static::setInstance($this);
$this->instance('app', $this);
$this->instance('think\Container', $this);
}
/**
* 注册服务
* @access public
* @param Service|string $service 服务
* @param bool $force 强制重新注册
* @return Service|null
*/
public function register($service, bool $force = false)
{
$registered = $this->getService($service);
if ($registered && !$force) {
return $registered;
}
if (is_string($service)) {
$service = new $service($this);
}
if (method_exists($service, 'register')) {
$service->register();
}
if (property_exists($service, 'bind')) {
$this->bind($service->bind);
}
$this->services[] = $service;
}
/**
* 执行服务
* @access public
* @param Service $service 服务
* @return mixed
*/
public function bootService($service)
{
if (method_exists($service, 'boot')) {
return $this->invoke([$service, 'boot']);
}
}
/**
* 获取服务
* @param string|Service $service
* @return Service|null
*/
public function getService($service)
{
$name = is_string($service) ? $service : get_class($service);
return array_values(array_filter($this->services, function ($value) use ($name) {
return $value instanceof $name;
}, ARRAY_FILTER_USE_BOTH))[0] ?? null;
}
/**
* 开启应用调试模式
* @access public
* @param bool $debug 开启应用调试模式
* @return $this
*/
public function debug(bool $debug = true)
{
$this->appDebug = $debug;
return $this;
}
/**
* 是否为调试模式
* @access public
* @return bool
*/
public function isDebug(): bool
{
return $this->appDebug;
}
/**
* 设置应用命名空间
* @access public
* @param string $namespace 应用命名空间
* @return $this
*/
public function setNamespace(string $namespace)
{
$this->namespace = $namespace;
return $this;
}
/**
* 获取应用类库命名空间
* @access public
* @return string
*/
public function getNamespace(): string
{
return $this->namespace;
}
/**
* 获取框架版本
* @access public
* @return string
*/
public function version(): string
{
return static::VERSION;
}
/**
* 获取应用根目录
* @access public
* @return string
*/
public function getRootPath(): string
{
return $this->rootPath;
}
/**
* 获取应用基础目录
* @access public
* @return string
*/
public function getBasePath(): string
{
return $this->rootPath . 'app' . DIRECTORY_SEPARATOR;
}
/**
* 获取当前应用目录
* @access public
* @return string
*/
public function getAppPath(): string
{
return $this->appPath;
}
/**
* 设置应用目录
* @param string $path 应用目录
*/
public function setAppPath(string $path)
{
$this->appPath = $path;
}
/**
* 获取应用运行时目录
* @access public
* @return string
*/
public function getRuntimePath(): string
{
return $this->runtimePath;
}
/**
* 设置runtime目录
* @param string $path 定义目录
*/
public function setRuntimePath(string $path): void
{
$this->runtimePath = $path;
}
/**
* 获取核心框架目录
* @access public
* @return string
*/
public function getThinkPath(): string
{
return $this->thinkPath;
}
/**
* 获取应用配置目录
* @access public
* @return string
*/
public function getConfigPath(): string
{
return $this->rootPath . 'config' . DIRECTORY_SEPARATOR;
}
/**
* 获取配置后缀
* @access public
* @return string
*/
public function getConfigExt(): string
{
return $this->configExt;
}
/**
* 获取应用开启时间
* @access public
* @return float
*/
public function getBeginTime(): float
{
return $this->beginTime;
}
/**
* 获取应用初始内存占用
* @access public
* @return integer
*/
public function getBeginMem(): int
{
return $this->beginMem;
}
/**
* 初始化应用
* @access public
* @return $this
*/
public function initialize()
{
$this->initialized = true;
$this->beginTime = microtime(true);
$this->beginMem = memory_get_usage();
// 加载环境变量
if (is_file($this->rootPath . '.env')) {
$this->env->load($this->rootPath . '.env');
}
$this->configExt = $this->env->get('config_ext', '.php');
$this->debugModeInit();
// 加载全局初始化文件
$this->load();
// 加载框架默认语言包
$langSet = $this->lang->defaultLangSet();
$this->lang->load($this->thinkPath . 'lang' . DIRECTORY_SEPARATOR . $langSet . '.php');
// 加载应用默认语言包
$this->loadLangPack($langSet);
// 监听AppInit
$this->event->trigger(AppInit::class);
date_default_timezone_set($this->config->get('app.default_timezone', 'Asia/Shanghai'));
// 初始化
foreach ($this->initializers as $initializer) {
$this->make($initializer)->init($this);
}
return $this;
}
/**
* 是否初始化过
* @return bool
*/
public function initialized()
{
return $this->initialized;
}
/**
* 加载语言包
* @param string $langset 语言
* @return void
*/
public function loadLangPack($langset)
{
if (empty($langset)) {
return;
}
// 加载系统语言包
$files = glob($this->appPath . 'lang' . DIRECTORY_SEPARATOR . $langset . '.*');
$this->lang->load($files);
// 加载扩展(自定义)语言包
$list = $this->config->get('lang.extend_list', []);
if (isset($list[$langset])) {
$this->lang->load($list[$langset]);
}
}
/**
* 引导应用
* @access public
* @return void
*/
public function boot(): void
{
array_walk($this->services, function ($service) {
$this->bootService($service);
});
}
/**
* 加载应用文件和配置
* @access protected
* @return void
*/
protected function load(): void
{
$appPath = $this->getAppPath();
if (is_file($appPath . 'common.php')) {
include_once $appPath . 'common.php';
}
include_once $this->thinkPath . 'helper.php';
$configPath = $this->getConfigPath();
$files = [];
if (is_dir($configPath)) {
$files = glob($configPath . '*' . $this->configExt);
}
foreach ($files as $file) {
$this->config->load($file, pathinfo($file, PATHINFO_FILENAME));
}
if (is_file($appPath . 'event.php')) {
$this->loadEvent(include $appPath . 'event.php');
}
if (is_file($appPath . 'service.php')) {
$services = include $appPath . 'service.php';
foreach ($services as $service) {
$this->register($service);
}
}
}
/**
* 调试模式设置
* @access protected
* @return void
*/
protected function debugModeInit(): void
{
// 应用调试模式
if (!$this->appDebug) {
$this->appDebug = $this->env->get('app_debug') ? true : false;
ini_set('display_errors', 'Off');
}
if (!$this->runningInConsole()) {
//重新申请一块比较大的buffer
if (ob_get_level() > 0) {
$output = ob_get_clean();
}
ob_start();
if (!empty($output)) {
echo $output;
}
}
}
/**
* 注册应用事件
* @access protected
* @param array $event 事件数据
* @return void
*/
public function loadEvent(array $event): void
{
if (isset($event['bind'])) {
$this->event->bind($event['bind']);
}
if (isset($event['listen'])) {
$this->event->listenEvents($event['listen']);
}
if (isset($event['subscribe'])) {
$this->event->subscribe($event['subscribe']);
}
}
/**
* 解析应用类的类名
* @access public
* @param string $layer 层名 controller model ...
* @param string $name 类名
* @return string
*/
public function parseClass(string $layer, string $name): string
{
$name = str_replace(['/', '.'], '\\', $name);
$array = explode('\\', $name);
$class = Str::studly(array_pop($array));
$path = $array ? implode('\\', $array) . '\\' : '';
return $this->namespace . '\\' . $layer . '\\' . $path . $class;
}
/**
* 是否运行在命令行下
* @return bool
*/
public function runningInConsole()
{
return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg';
}
/**
* 获取应用根目录
* @access protected
* @return string
*/
protected function getDefaultRootPath(): string
{
$path = dirname(dirname(dirname(dirname($this->thinkPath))));
return $path . DIRECTORY_SEPARATOR;
}
}
Loaded 3 of 1279 files, more files were not shown because too many files have changed in this diff. Show more