diff --git a/thinkphp/lang/zh-cn.php b/thinkphp/lang/zh-cn.php
new file mode 100755
index 0000000000000000000000000000000000000000..1e050820471872cdaa208f6cec167aa3e612c805
--- /dev/null
+++ b/thinkphp/lang/zh-cn.php
@@ -0,0 +1,144 @@
+
+// +----------------------------------------------------------------------
+
+// 核心中文语言包
+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' => '函数不存在',
+ 'file not exists' => '文件不存在',
+ 'module not exists' => '模块不存在',
+ 'controller not exists' => '控制器不存在',
+ 'class not exists' => '类不存在',
+ 'property not exists' => '类的属性不存在',
+ 'template not exists' => '模板文件不存在',
+ 'illegal controller name' => '非法的控制器名称',
+ 'illegal action name' => '非法的操作名称',
+ 'url suffix deny' => '禁止的URL后缀访问',
+ 'Route Not Found' => '当前访问路由未定义或不匹配',
+ 'Undefined db type' => '未定义数据库类型',
+ 'variable type error' => '变量类型错误',
+ 'PSR-4 error' => 'PSR-4 规范错误',
+ 'not support total' => '简洁模式下不能获取数据总数',
+ 'not support last' => '简洁模式下不能获取最后一页',
+ 'error session handler' => '错误的SESSION处理器类',
+ 'not allow php tag' => '模板不允许使用PHP语法',
+ 'not support' => '不支持',
+ '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' => '查询表达式错误',
+ 'order express error' => '排序表达式错误',
+ 'no data to update' => '没有任何数据需要更新',
+ 'miss data to insert' => '缺少需要写入的数据',
+ 'not support data' => '不支持的数据表达式',
+ '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' => '规则错误',
+];
diff --git a/thinkphp/library/think/App.php b/thinkphp/library/think/App.php
new file mode 100755
index 0000000000000000000000000000000000000000..7766555eb563a61cb6cf0a77efd9624184dec6d2
--- /dev/null
+++ b/thinkphp/library/think/App.php
@@ -0,0 +1,981 @@
+
+// +----------------------------------------------------------------------
+
+namespace think;
+
+use think\exception\ClassNotFoundException;
+use think\exception\HttpResponseException;
+use think\route\Dispatch;
+
+/**
+ * App 应用管理
+ */
+class App extends Container
+{
+ const VERSION = '5.1.38 LTS';
+
+ /**
+ * 当前模块路径
+ * @var string
+ */
+ protected $modulePath;
+
+ /**
+ * 应用调试模式
+ * @var bool
+ */
+ protected $appDebug = true;
+
+ /**
+ * 应用开始时间
+ * @var float
+ */
+ protected $beginTime;
+
+ /**
+ * 应用内存初始占用
+ * @var integer
+ */
+ protected $beginMem;
+
+ /**
+ * 应用类库命名空间
+ * @var string
+ */
+ protected $namespace = 'app';
+
+ /**
+ * 应用类库后缀
+ * @var bool
+ */
+ protected $suffix = false;
+
+ /**
+ * 严格路由检测
+ * @var bool
+ */
+ protected $routeMust;
+
+ /**
+ * 应用类库目录
+ * @var string
+ */
+ protected $appPath;
+
+ /**
+ * 框架目录
+ * @var string
+ */
+ protected $thinkPath;
+
+ /**
+ * 应用根目录
+ * @var string
+ */
+ protected $rootPath;
+
+ /**
+ * 运行时目录
+ * @var string
+ */
+ protected $runtimePath;
+
+ /**
+ * 配置目录
+ * @var string
+ */
+ protected $configPath;
+
+ /**
+ * 路由目录
+ * @var string
+ */
+ protected $routePath;
+
+ /**
+ * 配置后缀
+ * @var string
+ */
+ protected $configExt;
+
+ /**
+ * 应用调度实例
+ * @var Dispatch
+ */
+ protected $dispatch;
+
+ /**
+ * 绑定模块(控制器)
+ * @var string
+ */
+ protected $bindModule;
+
+ /**
+ * 初始化
+ * @var bool
+ */
+ protected $initialized = false;
+
+ public function __construct($appPath = '')
+ {
+ $this->thinkPath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR;
+ $this->path($appPath);
+ }
+
+ /**
+ * 绑定模块或者控制器
+ * @access public
+ * @param string $bind
+ * @return $this
+ */
+ public function bind($bind)
+ {
+ $this->bindModule = $bind;
+ return $this;
+ }
+
+ /**
+ * 设置应用类库目录
+ * @access public
+ * @param string $path 路径
+ * @return $this
+ */
+ public function path($path)
+ {
+ $this->appPath = $path ? realpath($path) . DIRECTORY_SEPARATOR : $this->getAppPath();
+
+ return $this;
+ }
+
+ /**
+ * 初始化应用
+ * @access public
+ * @return void
+ */
+ public function initialize()
+ {
+ if ($this->initialized) {
+ return;
+ }
+
+ $this->initialized = true;
+ $this->beginTime = microtime(true);
+ $this->beginMem = memory_get_usage();
+
+ $this->rootPath = dirname($this->appPath) . DIRECTORY_SEPARATOR;
+ $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;
+ $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR;
+ $this->configPath = $this->rootPath . 'config' . DIRECTORY_SEPARATOR;
+
+ static::setInstance($this);
+
+ $this->instance('app', $this);
+
+ // 加载环境变量配置文件
+ if (is_file($this->rootPath . '.env')) {
+ $this->env->load($this->rootPath . '.env');
+ }
+
+ $this->configExt = $this->env->get('config_ext', '.php');
+
+ // 加载惯例配置文件
+ $this->config->set(include $this->thinkPath . 'convention.php');
+
+ // 设置路径环境变量
+ $this->env->set([
+ 'think_path' => $this->thinkPath,
+ 'root_path' => $this->rootPath,
+ 'app_path' => $this->appPath,
+ 'config_path' => $this->configPath,
+ 'route_path' => $this->routePath,
+ 'runtime_path' => $this->runtimePath,
+ 'extend_path' => $this->rootPath . 'extend' . DIRECTORY_SEPARATOR,
+ 'vendor_path' => $this->rootPath . 'vendor' . DIRECTORY_SEPARATOR,
+ ]);
+
+ $this->namespace = $this->env->get('app_namespace', $this->namespace);
+ $this->env->set('app_namespace', $this->namespace);
+
+ // 注册应用命名空间
+ Loader::addNamespace($this->namespace, $this->appPath);
+
+ // 初始化应用
+ $this->init();
+
+ // 开启类名后缀
+ $this->suffix = $this->config('app.class_suffix');
+
+ // 应用调试模式
+ $this->appDebug = $this->env->get('app_debug', $this->config('app.app_debug'));
+ $this->env->set('app_debug', $this->appDebug);
+
+ if (!$this->appDebug) {
+ ini_set('display_errors', 'Off');
+ } elseif (PHP_SAPI != 'cli') {
+ //重新申请一块比较大的buffer
+ if (ob_get_level() > 0) {
+ $output = ob_get_clean();
+ }
+ ob_start();
+ if (!empty($output)) {
+ echo $output;
+ }
+ }
+
+ // 注册异常处理类
+ if ($this->config('app.exception_handle')) {
+ Error::setExceptionHandler($this->config('app.exception_handle'));
+ }
+
+ // 注册根命名空间
+ if (!empty($this->config('app.root_namespace'))) {
+ Loader::addNamespace($this->config('app.root_namespace'));
+ }
+
+ // 加载composer autofile文件
+ Loader::loadComposerAutoloadFiles();
+
+ // 注册类库别名
+ Loader::addClassAlias($this->config->pull('alias'));
+
+ // 数据库配置初始化
+ Db::init($this->config->pull('database'));
+
+ // 设置系统时区
+ date_default_timezone_set($this->config('app.default_timezone'));
+
+ // 读取语言包
+ $this->loadLangPack();
+
+ // 路由初始化
+ $this->routeInit();
+ }
+
+ /**
+ * 初始化应用或模块
+ * @access public
+ * @param string $module 模块名
+ * @return void
+ */
+ public function init($module = '')
+ {
+ // 定位模块目录
+ $module = $module ? $module . DIRECTORY_SEPARATOR : '';
+ $path = $this->appPath . $module;
+
+ // 加载初始化文件
+ if (is_file($path . 'init.php')) {
+ include $path . 'init.php';
+ } elseif (is_file($this->runtimePath . $module . 'init.php')) {
+ include $this->runtimePath . $module . 'init.php';
+ } else {
+ // 加载行为扩展文件
+ if (is_file($path . 'tags.php')) {
+ $tags = include $path . 'tags.php';
+ if (is_array($tags)) {
+ $this->hook->import($tags);
+ }
+ }
+
+ // 加载公共文件
+ if (is_file($path . 'common.php')) {
+ include_once $path . 'common.php';
+ }
+
+ if ('' == $module) {
+ // 加载系统助手函数
+ include $this->thinkPath . 'helper.php';
+ }
+
+ // 加载中间件
+ if (is_file($path . 'middleware.php')) {
+ $middleware = include $path . 'middleware.php';
+ if (is_array($middleware)) {
+ $this->middleware->import($middleware);
+ }
+ }
+
+ // 注册服务的容器对象实例
+ if (is_file($path . 'provider.php')) {
+ $provider = include $path . 'provider.php';
+ if (is_array($provider)) {
+ $this->bindTo($provider);
+ }
+ }
+
+ // 自动读取配置文件
+ if (is_dir($path . 'config')) {
+ $dir = $path . 'config' . DIRECTORY_SEPARATOR;
+ } elseif (is_dir($this->configPath . $module)) {
+ $dir = $this->configPath . $module;
+ }
+
+ $files = isset($dir) ? scandir($dir) : [];
+
+ foreach ($files as $file) {
+ if ('.' . pathinfo($file, PATHINFO_EXTENSION) === $this->configExt) {
+ $this->config->load($dir . $file, pathinfo($file, PATHINFO_FILENAME));
+ }
+ }
+ }
+
+ $this->setModulePath($path);
+
+ if ($module) {
+ // 对容器中的对象实例进行配置更新
+ $this->containerConfigUpdate($module);
+ }
+ }
+
+ protected function containerConfigUpdate($module)
+ {
+ $config = $this->config->get();
+
+ // 注册异常处理类
+ if ($config['app']['exception_handle']) {
+ Error::setExceptionHandler($config['app']['exception_handle']);
+ }
+
+ Db::init($config['database']);
+ $this->middleware->setConfig($config['middleware']);
+ $this->route->setConfig($config['app']);
+ $this->request->init($config['app']);
+ $this->cookie->init($config['cookie']);
+ $this->view->init($config['template']);
+ $this->log->init($config['log']);
+ $this->session->setConfig($config['session']);
+ $this->debug->setConfig($config['trace']);
+ $this->cache->init($config['cache'], true);
+
+ // 加载当前模块语言包
+ $this->lang->load($this->appPath . $module . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php');
+
+ // 模块请求缓存检查
+ $this->checkRequestCache(
+ $config['app']['request_cache'],
+ $config['app']['request_cache_expire'],
+ $config['app']['request_cache_except']
+ );
+ }
+
+ /**
+ * 执行应用程序
+ * @access public
+ * @return Response
+ * @throws Exception
+ */
+ public function run()
+ {
+ try {
+ // 初始化应用
+ $this->initialize();
+
+ // 监听app_init
+ $this->hook->listen('app_init');
+
+ if ($this->bindModule) {
+ // 模块/控制器绑定
+ $this->route->bind($this->bindModule);
+ } elseif ($this->config('app.auto_bind_module')) {
+ // 入口自动绑定
+ $name = pathinfo($this->request->baseFile(), PATHINFO_FILENAME);
+ if ($name && 'index' != $name && is_dir($this->appPath . $name)) {
+ $this->route->bind($name);
+ }
+ }
+
+ // 监听app_dispatch
+ $this->hook->listen('app_dispatch');
+
+ $dispatch = $this->dispatch;
+
+ if (empty($dispatch)) {
+ // 路由检测
+ $dispatch = $this->routeCheck()->init();
+ }
+
+ // 记录当前调度信息
+ $this->request->dispatch($dispatch);
+
+ // 记录路由和请求信息
+ if ($this->appDebug) {
+ $this->log('[ ROUTE ] ' . var_export($this->request->routeInfo(), true));
+ $this->log('[ HEADER ] ' . var_export($this->request->header(), true));
+ $this->log('[ PARAM ] ' . var_export($this->request->param(), true));
+ }
+
+ // 监听app_begin
+ $this->hook->listen('app_begin');
+
+ // 请求缓存检查
+ $this->checkRequestCache(
+ $this->config('request_cache'),
+ $this->config('request_cache_expire'),
+ $this->config('request_cache_except')
+ );
+
+ $data = null;
+ } catch (HttpResponseException $exception) {
+ $dispatch = null;
+ $data = $exception->getResponse();
+ }
+
+ $this->middleware->add(function (Request $request, $next) use ($dispatch, $data) {
+ return is_null($data) ? $dispatch->run() : $data;
+ });
+
+ $response = $this->middleware->dispatch($this->request);
+
+ // 监听app_end
+ $this->hook->listen('app_end', $response);
+
+ return $response;
+ }
+
+ protected function getRouteCacheKey()
+ {
+ if ($this->config->get('route_check_cache_key')) {
+ $closure = $this->config->get('route_check_cache_key');
+ $routeKey = $closure($this->request);
+ } else {
+ $routeKey = md5($this->request->baseUrl(true) . ':' . $this->request->method());
+ }
+
+ return $routeKey;
+ }
+
+ protected function loadLangPack()
+ {
+ // 读取默认语言
+ $this->lang->range($this->config('app.default_lang'));
+
+ if ($this->config('app.lang_switch_on')) {
+ // 开启多语言机制 检测当前语言
+ $this->lang->detect();
+ }
+
+ $this->request->setLangset($this->lang->range());
+
+ // 加载系统语言包
+ $this->lang->load([
+ $this->thinkPath . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php',
+ $this->appPath . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php',
+ ]);
+ }
+
+ /**
+ * 设置当前地址的请求缓存
+ * @access public
+ * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
+ * @param mixed $expire 缓存有效期
+ * @param array $except 缓存排除
+ * @param string $tag 缓存标签
+ * @return void
+ */
+ public function checkRequestCache($key, $expire = null, $except = [], $tag = null)
+ {
+ $cache = $this->request->cache($key, $expire, $except, $tag);
+
+ if ($cache) {
+ $this->setResponseCache($cache);
+ }
+ }
+
+ public function setResponseCache($cache)
+ {
+ list($key, $expire, $tag) = $cache;
+
+ if (strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $this->request->server('REQUEST_TIME')) {
+ // 读取缓存
+ $response = Response::create()->code(304);
+ throw new HttpResponseException($response);
+ } elseif ($this->cache->has($key)) {
+ list($content, $header) = $this->cache->get($key);
+
+ $response = Response::create($content)->header($header);
+ throw new HttpResponseException($response);
+ }
+ }
+
+ /**
+ * 设置当前请求的调度信息
+ * @access public
+ * @param Dispatch $dispatch 调度信息
+ * @return $this
+ */
+ public function dispatch(Dispatch $dispatch)
+ {
+ $this->dispatch = $dispatch;
+ return $this;
+ }
+
+ /**
+ * 记录调试信息
+ * @access public
+ * @param mixed $msg 调试信息
+ * @param string $type 信息类型
+ * @return void
+ */
+ public function log($msg, $type = 'info')
+ {
+ $this->appDebug && $this->log->record($msg, $type);
+ }
+
+ /**
+ * 获取配置参数 为空则获取所有配置
+ * @access public
+ * @param string $name 配置参数名(支持二级配置 .号分割)
+ * @return mixed
+ */
+ public function config($name = '')
+ {
+ return $this->config->get($name);
+ }
+
+ /**
+ * 路由初始化 导入路由定义规则
+ * @access public
+ * @return void
+ */
+ public function routeInit()
+ {
+ // 路由检测
+ $files = scandir($this->routePath);
+ foreach ($files as $file) {
+ if (strpos($file, '.php')) {
+ $filename = $this->routePath . $file;
+ // 导入路由配置
+ $rules = include $filename;
+ if (is_array($rules)) {
+ $this->route->import($rules);
+ }
+ }
+ }
+
+ if ($this->route->config('route_annotation')) {
+ // 自动生成路由定义
+ if ($this->appDebug) {
+ $suffix = $this->route->config('controller_suffix') || $this->route->config('class_suffix');
+ $this->build->buildRoute($suffix);
+ }
+
+ $filename = $this->runtimePath . 'build_route.php';
+
+ if (is_file($filename)) {
+ include $filename;
+ }
+ }
+ }
+
+ /**
+ * URL路由检测(根据PATH_INFO)
+ * @access public
+ * @return Dispatch
+ */
+ public function routeCheck()
+ {
+ // 检测路由缓存
+ if (!$this->appDebug && $this->config->get('route_check_cache')) {
+ $routeKey = $this->getRouteCacheKey();
+ $option = $this->config->get('route_cache_option');
+
+ if ($option && $this->cache->connect($option)->has($routeKey)) {
+ return $this->cache->connect($option)->get($routeKey);
+ } elseif ($this->cache->has($routeKey)) {
+ return $this->cache->get($routeKey);
+ }
+ }
+
+ // 获取应用调度信息
+ $path = $this->request->path();
+
+ // 是否强制路由模式
+ $must = !is_null($this->routeMust) ? $this->routeMust : $this->route->config('url_route_must');
+
+ // 路由检测 返回一个Dispatch对象
+ $dispatch = $this->route->check($path, $must);
+
+ if (!empty($routeKey)) {
+ try {
+ if ($option) {
+ $this->cache->connect($option)->tag('route_cache')->set($routeKey, $dispatch);
+ } else {
+ $this->cache->tag('route_cache')->set($routeKey, $dispatch);
+ }
+ } catch (\Exception $e) {
+ // 存在闭包的时候缓存无效
+ }
+ }
+
+ return $dispatch;
+ }
+
+ /**
+ * 设置应用的路由检测机制
+ * @access public
+ * @param bool $must 是否强制检测路由
+ * @return $this
+ */
+ public function routeMust($must = false)
+ {
+ $this->routeMust = $must;
+ return $this;
+ }
+
+ /**
+ * 解析模块和类名
+ * @access protected
+ * @param string $name 资源地址
+ * @param string $layer 验证层名称
+ * @param bool $appendSuffix 是否添加类名后缀
+ * @return array
+ */
+ protected function parseModuleAndClass($name, $layer, $appendSuffix)
+ {
+ if (false !== strpos($name, '\\')) {
+ $class = $name;
+ $module = $this->request->module();
+ } else {
+ if (strpos($name, '/')) {
+ list($module, $name) = explode('/', $name, 2);
+ } else {
+ $module = $this->request->module();
+ }
+
+ $class = $this->parseClass($module, $layer, $name, $appendSuffix);
+ }
+
+ return [$module, $class];
+ }
+
+ /**
+ * 实例化应用类库
+ * @access public
+ * @param string $name 类名称
+ * @param string $layer 业务层名称
+ * @param bool $appendSuffix 是否添加类名后缀
+ * @param string $common 公共模块名
+ * @return object
+ * @throws ClassNotFoundException
+ */
+ public function create($name, $layer, $appendSuffix = false, $common = 'common')
+ {
+ $guid = $name . $layer;
+
+ if ($this->__isset($guid)) {
+ return $this->__get($guid);
+ }
+
+ list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
+
+ if (class_exists($class)) {
+ $object = $this->__get($class);
+ } else {
+ $class = str_replace('\\' . $module . '\\', '\\' . $common . '\\', $class);
+ if (class_exists($class)) {
+ $object = $this->__get($class);
+ } else {
+ throw new ClassNotFoundException('class not exists:' . $class, $class);
+ }
+ }
+
+ $this->__set($guid, $class);
+
+ return $object;
+ }
+
+ /**
+ * 实例化(分层)模型
+ * @access public
+ * @param string $name Model名称
+ * @param string $layer 业务层名称
+ * @param bool $appendSuffix 是否添加类名后缀
+ * @param string $common 公共模块名
+ * @return Model
+ * @throws ClassNotFoundException
+ */
+ public function model($name = '', $layer = 'model', $appendSuffix = false, $common = 'common')
+ {
+ return $this->create($name, $layer, $appendSuffix, $common);
+ }
+
+ /**
+ * 实例化(分层)控制器 格式:[模块名/]控制器名
+ * @access public
+ * @param string $name 资源地址
+ * @param string $layer 控制层名称
+ * @param bool $appendSuffix 是否添加类名后缀
+ * @param string $empty 空控制器名称
+ * @return object
+ * @throws ClassNotFoundException
+ */
+ public function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '')
+ {
+ list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
+
+ if (class_exists($class)) {
+ return $this->make($class, true);
+ } elseif ($empty && class_exists($emptyClass = $this->parseClass($module, $layer, $empty, $appendSuffix))) {
+ return $this->make($emptyClass, true);
+ }
+
+ throw new ClassNotFoundException('class not exists:' . $class, $class);
+ }
+
+ /**
+ * 实例化验证类 格式:[模块名/]验证器名
+ * @access public
+ * @param string $name 资源地址
+ * @param string $layer 验证层名称
+ * @param bool $appendSuffix 是否添加类名后缀
+ * @param string $common 公共模块名
+ * @return Validate
+ * @throws ClassNotFoundException
+ */
+ public function validate($name = '', $layer = 'validate', $appendSuffix = false, $common = 'common')
+ {
+ $name = $name ?: $this->config('default_validate');
+
+ if (empty($name)) {
+ return new Validate;
+ }
+
+ return $this->create($name, $layer, $appendSuffix, $common);
+ }
+
+ /**
+ * 数据库初始化
+ * @access public
+ * @param mixed $config 数据库配置
+ * @param bool|string $name 连接标识 true 强制重新连接
+ * @return \think\db\Query
+ */
+ public function db($config = [], $name = false)
+ {
+ return Db::connect($config, $name);
+ }
+
+ /**
+ * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
+ * @access public
+ * @param string $url 调用地址
+ * @param string|array $vars 调用参数 支持字符串和数组
+ * @param string $layer 要调用的控制层名称
+ * @param bool $appendSuffix 是否添加类名后缀
+ * @return mixed
+ * @throws ClassNotFoundException
+ */
+ public function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
+ {
+ $info = pathinfo($url);
+ $action = $info['basename'];
+ $module = '.' != $info['dirname'] ? $info['dirname'] : $this->request->controller();
+ $class = $this->controller($module, $layer, $appendSuffix);
+
+ if (is_scalar($vars)) {
+ if (strpos($vars, '=')) {
+ parse_str($vars, $vars);
+ } else {
+ $vars = [$vars];
+ }
+ }
+
+ return $this->invokeMethod([$class, $action . $this->config('action_suffix')], $vars);
+ }
+
+ /**
+ * 解析应用类的类名
+ * @access public
+ * @param string $module 模块名
+ * @param string $layer 层名 controller model ...
+ * @param string $name 类名
+ * @param bool $appendSuffix
+ * @return string
+ */
+ public function parseClass($module, $layer, $name, $appendSuffix = false)
+ {
+ $name = str_replace(['/', '.'], '\\', $name);
+ $array = explode('\\', $name);
+ $class = Loader::parseName(array_pop($array), 1) . ($this->suffix || $appendSuffix ? ucfirst($layer) : '');
+ $path = $array ? implode('\\', $array) . '\\' : '';
+
+ return $this->namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class;
+ }
+
+ /**
+ * 获取框架版本
+ * @access public
+ * @return string
+ */
+ public function version()
+ {
+ return static::VERSION;
+ }
+
+ /**
+ * 是否为调试模式
+ * @access public
+ * @return bool
+ */
+ public function isDebug()
+ {
+ return $this->appDebug;
+ }
+
+ /**
+ * 获取模块路径
+ * @access public
+ * @return string
+ */
+ public function getModulePath()
+ {
+ return $this->modulePath;
+ }
+
+ /**
+ * 设置模块路径
+ * @access public
+ * @param string $path 路径
+ * @return void
+ */
+ public function setModulePath($path)
+ {
+ $this->modulePath = $path;
+ $this->env->set('module_path', $path);
+ }
+
+ /**
+ * 获取应用根目录
+ * @access public
+ * @return string
+ */
+ public function getRootPath()
+ {
+ return $this->rootPath;
+ }
+
+ /**
+ * 获取应用类库目录
+ * @access public
+ * @return string
+ */
+ public function getAppPath()
+ {
+ if (is_null($this->appPath)) {
+ $this->appPath = Loader::getRootPath() . 'application' . DIRECTORY_SEPARATOR;
+ }
+
+ return $this->appPath;
+ }
+
+ /**
+ * 获取应用运行时目录
+ * @access public
+ * @return string
+ */
+ public function getRuntimePath()
+ {
+ return $this->runtimePath;
+ }
+
+ /**
+ * 获取核心框架目录
+ * @access public
+ * @return string
+ */
+ public function getThinkPath()
+ {
+ return $this->thinkPath;
+ }
+
+ /**
+ * 获取路由目录
+ * @access public
+ * @return string
+ */
+ public function getRoutePath()
+ {
+ return $this->routePath;
+ }
+
+ /**
+ * 获取应用配置目录
+ * @access public
+ * @return string
+ */
+ public function getConfigPath()
+ {
+ return $this->configPath;
+ }
+
+ /**
+ * 获取配置后缀
+ * @access public
+ * @return string
+ */
+ public function getConfigExt()
+ {
+ return $this->configExt;
+ }
+
+ /**
+ * 获取应用类库命名空间
+ * @access public
+ * @return string
+ */
+ public function getNamespace()
+ {
+ return $this->namespace;
+ }
+
+ /**
+ * 设置应用类库命名空间
+ * @access public
+ * @param string $namespace 命名空间名称
+ * @return $this
+ */
+ public function setNamespace($namespace)
+ {
+ $this->namespace = $namespace;
+ return $this;
+ }
+
+ /**
+ * 是否启用类库后缀
+ * @access public
+ * @return bool
+ */
+ public function getSuffix()
+ {
+ return $this->suffix;
+ }
+
+ /**
+ * 获取应用开启时间
+ * @access public
+ * @return float
+ */
+ public function getBeginTime()
+ {
+ return $this->beginTime;
+ }
+
+ /**
+ * 获取应用初始内存占用
+ * @access public
+ * @return integer
+ */
+ public function getBeginMem()
+ {
+ return $this->beginMem;
+ }
+
+}
diff --git a/thinkphp/library/think/Build.php b/thinkphp/library/think/Build.php
new file mode 100755
index 0000000000000000000000000000000000000000..7a531d74c786563c99da034c131eb99f2b703105
--- /dev/null
+++ b/thinkphp/library/think/Build.php
@@ -0,0 +1,415 @@
+
+// +----------------------------------------------------------------------
+
+namespace think;
+
+class Build
+{
+ /**
+ * 应用对象
+ * @var App
+ */
+ protected $app;
+
+ /**
+ * 应用目录
+ * @var string
+ */
+ protected $basePath;
+
+ public function __construct(App $app)
+ {
+ $this->app = $app;
+ $this->basePath = $this->app->getAppPath();
+ }
+
+ /**
+ * 根据传入的build资料创建目录和文件
+ * @access public
+ * @param array $build build列表
+ * @param string $namespace 应用类库命名空间
+ * @param bool $suffix 类库后缀
+ * @return void
+ */
+ public function run(array $build = [], $namespace = 'app', $suffix = false)
+ {
+ // 锁定
+ $lockfile = $this->basePath . 'build.lock';
+
+ if (is_writable($lockfile)) {
+ return;
+ } elseif (!touch($lockfile)) {
+ throw new Exception('应用目录[' . $this->basePath . ']不可写,目录无法自动生成!
请手动生成项目目录~', 10006);
+ }
+
+ foreach ($build as $module => $list) {
+ if ('__dir__' == $module) {
+ // 创建目录列表
+ $this->buildDir($list);
+ } elseif ('__file__' == $module) {
+ // 创建文件列表
+ $this->buildFile($list);
+ } else {
+ // 创建模块
+ $this->module($module, $list, $namespace, $suffix);
+ }
+ }
+
+ // 解除锁定
+ unlink($lockfile);
+ }
+
+ /**
+ * 创建目录
+ * @access protected
+ * @param array $list 目录列表
+ * @return void
+ */
+ protected function buildDir($list)
+ {
+ foreach ($list as $dir) {
+ $this->checkDirBuild($this->basePath . $dir);
+ }
+ }
+
+ /**
+ * 创建文件
+ * @access protected
+ * @param array $list 文件列表
+ * @return void
+ */
+ protected function buildFile($list)
+ {
+ foreach ($list as $file) {
+ if (!is_dir($this->basePath . dirname($file))) {
+ // 创建目录
+ mkdir($this->basePath . dirname($file), 0755, true);
+ }
+
+ if (!is_file($this->basePath . $file)) {
+ file_put_contents($this->basePath . $file, 'php' == pathinfo($file, PATHINFO_EXTENSION) ? "basePath . $module)) {
+ // 创建模块目录
+ mkdir($this->basePath . $module);
+ }
+
+ if (basename($this->app->getRuntimePath()) != $module) {
+ // 创建配置文件和公共文件
+ $this->buildCommon($module);
+ // 创建模块的默认页面
+ $this->buildHello($module, $namespace, $suffix);
+ }
+
+ if (empty($list)) {
+ // 创建默认的模块目录和文件
+ $list = [
+ '__file__' => ['common.php'],
+ '__dir__' => ['controller', 'model', 'view', 'config'],
+ ];
+ }
+
+ // 创建子目录和文件
+ foreach ($list as $path => $file) {
+ $modulePath = $this->basePath . $module . DIRECTORY_SEPARATOR;
+ if ('__dir__' == $path) {
+ // 生成子目录
+ foreach ($file as $dir) {
+ $this->checkDirBuild($modulePath . $dir);
+ }
+ } elseif ('__file__' == $path) {
+ // 生成(空白)文件
+ foreach ($file as $name) {
+ if (!is_file($modulePath . $name)) {
+ file_put_contents($modulePath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? "checkDirBuild(dirname($filename));
+ $content = '';
+ break;
+ default:
+ // 其他文件
+ $content = "app->getNameSpace();
+ $content = 'app->config('app.url_controller_layer');
+ }
+
+ if ($this->app->config('app.app_multi_module')) {
+ $modules = glob($this->basePath . '*', GLOB_ONLYDIR);
+
+ foreach ($modules as $module) {
+ $module = basename($module);
+
+ if (in_array($module, $this->app->config('app.deny_module_list'))) {
+ continue;
+ }
+
+ $path = $this->basePath . $module . DIRECTORY_SEPARATOR . $layer . DIRECTORY_SEPARATOR;
+ $content .= $this->buildDirRoute($path, $namespace, $module, $suffix, $layer);
+ }
+ } else {
+ $path = $this->basePath . $layer . DIRECTORY_SEPARATOR;
+ $content .= $this->buildDirRoute($path, $namespace, '', $suffix, $layer);
+ }
+
+ $filename = $this->app->getRuntimePath() . 'build_route.php';
+ file_put_contents($filename, $content);
+
+ return $filename;
+ }
+
+ /**
+ * 生成子目录控制器类的路由规则
+ * @access protected
+ * @param string $path 控制器目录
+ * @param string $namespace 应用命名空间
+ * @param string $module 模块
+ * @param bool $suffix 类库后缀
+ * @param string $layer 控制器层目录名
+ * @return string
+ */
+ protected function buildDirRoute($path, $namespace, $module, $suffix, $layer)
+ {
+ $content = '';
+ $controllers = glob($path . '*.php');
+
+ foreach ($controllers as $controller) {
+ $controller = basename($controller, '.php');
+
+ $class = new \ReflectionClass($namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $controller);
+
+ if (strpos($layer, '\\')) {
+ // 多级控制器
+ $level = str_replace(DIRECTORY_SEPARATOR, '.', substr($layer, 11));
+ $controller = $level . '.' . $controller;
+ $length = strlen(strstr($layer, '\\', true));
+ } else {
+ $length = strlen($layer);
+ }
+
+ if ($suffix) {
+ $controller = substr($controller, 0, -$length);
+ }
+
+ $content .= $this->getControllerRoute($class, $module, $controller);
+ }
+
+ $subDir = glob($path . '*', GLOB_ONLYDIR);
+
+ foreach ($subDir as $dir) {
+ $content .= $this->buildDirRoute($dir . DIRECTORY_SEPARATOR, $namespace, $module, $suffix, $layer . '\\' . basename($dir));
+ }
+
+ return $content;
+ }
+
+ /**
+ * 生成控制器类的路由规则
+ * @access protected
+ * @param string $class 控制器完整类名
+ * @param string $module 模块名
+ * @param string $controller 控制器名
+ * @return string
+ */
+ protected function getControllerRoute($class, $module, $controller)
+ {
+ $content = '';
+ $comment = $class->getDocComment();
+
+ if (false !== strpos($comment, '@route(')) {
+ $comment = $this->parseRouteComment($comment);
+ $route = ($module ? $module . '/' : '') . $controller;
+ $comment = preg_replace('/route\(\s?([\'\"][\-\_\/\:\<\>\?\$\[\]\w]+[\'\"])\s?\)/is', 'Route::resource(\1,\'' . $route . '\')', $comment);
+ $content .= PHP_EOL . $comment;
+ } elseif (false !== strpos($comment, '@alias(')) {
+ $comment = $this->parseRouteComment($comment, '@alias(');
+ $route = ($module ? $module . '/' : '') . $controller;
+ $comment = preg_replace('/alias\(\s?([\'\"][\-\_\/\w]+[\'\"])\s?\)/is', 'Route::alias(\1,\'' . $route . '\')', $comment);
+ $content .= PHP_EOL . $comment;
+ }
+
+ $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
+
+ foreach ($methods as $method) {
+ $comment = $this->getMethodRouteComment($module, $controller, $method);
+ if ($comment) {
+ $content .= PHP_EOL . $comment;
+ }
+ }
+
+ return $content;
+ }
+
+ /**
+ * 解析路由注释
+ * @access protected
+ * @param string $comment
+ * @param string $tag
+ * @return string
+ */
+ protected function parseRouteComment($comment, $tag = '@route(')
+ {
+ $comment = substr($comment, 3, -2);
+ $comment = explode(PHP_EOL, substr(strstr(trim($comment), $tag), 1));
+ $comment = array_map(function ($item) {return trim(trim($item), ' \t*');}, $comment);
+
+ if (count($comment) > 1) {
+ $key = array_search('', $comment);
+ $comment = array_slice($comment, 0, false === $key ? 1 : $key);
+ }
+
+ $comment = implode(PHP_EOL . "\t", $comment) . ';';
+
+ if (strpos($comment, '{')) {
+ $comment = preg_replace_callback('/\{\s?.*?\s?\}/s', function ($matches) {
+ return false !== strpos($matches[0], '"') ? '[' . substr(var_export(json_decode($matches[0], true), true), 7, -1) . ']' : $matches[0];
+ }, $comment);
+ }
+ return $comment;
+ }
+
+ /**
+ * 获取方法的路由注释
+ * @access protected
+ * @param string $module 模块
+ * @param string $controller 控制器名
+ * @param \ReflectMethod $reflectMethod
+ * @return string|void
+ */
+ protected function getMethodRouteComment($module, $controller, $reflectMethod)
+ {
+ $comment = $reflectMethod->getDocComment();
+
+ if (false !== strpos($comment, '@route(')) {
+ $comment = $this->parseRouteComment($comment);
+ $action = $reflectMethod->getName();
+
+ if ($suffix = $this->app->config('app.action_suffix')) {
+ $action = substr($action, 0, -strlen($suffix));
+ }
+
+ $route = ($module ? $module . '/' : '') . $controller . '/' . $action;
+ $comment = preg_replace('/route\s?\(\s?([\'\"][\-\_\/\:\<\>\?\$\[\]\w]+[\'\"])\s?\,?\s?[\'\"]?(\w+?)[\'\"]?\s?\)/is', 'Route::\2(\1,\'' . $route . '\')', $comment);
+ $comment = preg_replace('/route\s?\(\s?([\'\"][\-\_\/\:\<\>\?\$\[\]\w]+[\'\"])\s?\)/is', 'Route::rule(\1,\'' . $route . '\')', $comment);
+
+ return $comment;
+ }
+ }
+
+ /**
+ * 创建模块的欢迎页面
+ * @access protected
+ * @param string $module 模块名
+ * @param string $namespace 应用类库命名空间
+ * @param bool $suffix 类库后缀
+ * @return void
+ */
+ protected function buildHello($module, $namespace, $suffix = false)
+ {
+ $filename = $this->basePath . ($module ? $module . DIRECTORY_SEPARATOR : '') . 'controller' . DIRECTORY_SEPARATOR . 'Index' . ($suffix ? 'Controller' : '') . '.php';
+ if (!is_file($filename)) {
+ $content = file_get_contents($this->app->getThinkPath() . 'tpl' . DIRECTORY_SEPARATOR . 'default_index.tpl');
+ $content = str_replace(['{$app}', '{$module}', '{layer}', '{$suffix}'], [$namespace, $module ? $module . '\\' : '', 'controller', $suffix ? 'Controller' : ''], $content);
+ $this->checkDirBuild(dirname($filename));
+
+ file_put_contents($filename, $content);
+ }
+ }
+
+ /**
+ * 创建模块的公共文件
+ * @access protected
+ * @param string $module 模块名
+ * @return void
+ */
+ protected function buildCommon($module)
+ {
+ $filename = $this->app->getConfigPath() . ($module ? $module . DIRECTORY_SEPARATOR : '') . 'app.php';
+ $this->checkDirBuild(dirname($filename));
+
+ if (!is_file($filename)) {
+ file_put_contents($filename, "basePath . ($module ? $module . DIRECTORY_SEPARATOR : '') . 'common.php';
+
+ if (!is_file($filename)) {
+ file_put_contents($filename, "
+// +----------------------------------------------------------------------
+
+namespace think;
+
+use think\cache\Driver;
+
+/**
+ * Class Cache
+ *
+ * @package think
+ *
+ * @mixin Driver
+ * @mixin \think\cache\driver\File
+ */
+class Cache
+{
+ /**
+ * 缓存实例
+ * @var array
+ */
+ protected $instance = [];
+
+ /**
+ * 缓存配置
+ * @var array
+ */
+ protected $config = [];
+
+ /**
+ * 操作句柄
+ * @var object
+ */
+ protected $handler;
+
+ public function __construct(array $config = [])
+ {
+ $this->config = $config;
+ $this->init($config);
+ }
+
+ /**
+ * 连接缓存
+ * @access public
+ * @param array $options 配置数组
+ * @param bool|string $name 缓存连接标识 true 强制重新连接
+ * @return Driver
+ */
+ public function connect(array $options = [], $name = false)
+ {
+ if (false === $name) {
+ $name = md5(serialize($options));
+ }
+
+ if (true === $name || !isset($this->instance[$name])) {
+ $type = !empty($options['type']) ? $options['type'] : 'File';
+
+ if (true === $name) {
+ $name = md5(serialize($options));
+ }
+
+ $this->instance[$name] = Loader::factory($type, '\\think\\cache\\driver\\', $options);
+ }
+
+ return $this->instance[$name];
+ }
+
+ /**
+ * 自动初始化缓存
+ * @access public
+ * @param array $options 配置数组
+ * @param bool $force 强制更新
+ * @return Driver
+ */
+ public function init(array $options = [], $force = false)
+ {
+ if (is_null($this->handler) || $force) {
+
+ if ('complex' == $options['type']) {
+ $default = $options['default'];
+ $options = isset($options[$default['type']]) ? $options[$default['type']] : $default;
+ }
+
+ $this->handler = $this->connect($options);
+ }
+
+ return $this->handler;
+ }
+
+ public static function __make(Config $config)
+ {
+ return new static($config->pull('cache'));
+ }
+
+ public function getConfig()
+ {
+ return $this->config;
+ }
+
+ public function setConfig(array $config)
+ {
+ $this->config = array_merge($this->config, $config);
+ }
+
+ /**
+ * 切换缓存类型 需要配置 cache.type 为 complex
+ * @access public
+ * @param string $name 缓存标识
+ * @return Driver
+ */
+ public function store($name = '')
+ {
+ if ('' !== $name && 'complex' == $this->config['type']) {
+ return $this->connect($this->config[$name], strtolower($name));
+ }
+
+ return $this->init();
+ }
+
+ public function __call($method, $args)
+ {
+ return call_user_func_array([$this->init(), $method], $args);
+ }
+
+}
diff --git a/thinkphp/library/think/Collection.php b/thinkphp/library/think/Collection.php
new file mode 100755
index 0000000000000000000000000000000000000000..8251fc8f1c46c4cf8769c6e2513a84e9920b6672
--- /dev/null
+++ b/thinkphp/library/think/Collection.php
@@ -0,0 +1,552 @@
+
+// +----------------------------------------------------------------------
+
+namespace think;
+
+use ArrayAccess;
+use ArrayIterator;
+use Countable;
+use IteratorAggregate;
+use JsonSerializable;
+
+class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
+{
+ /**
+ * 数据集数据
+ * @var array
+ */
+ protected $items = [];
+
+ public function __construct($items = [])
+ {
+ $this->items = $this->convertToArray($items);
+ }
+
+ public static function make($items = [])
+ {
+ return new static($items);
+ }
+
+ /**
+ * 是否为空
+ * @access public
+ * @return bool
+ */
+ public function isEmpty()
+ {
+ return empty($this->items);
+ }
+
+ public function toArray()
+ {
+ return array_map(function ($value) {
+ return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value;
+ }, $this->items);
+ }
+
+ public function all()
+ {
+ return $this->items;
+ }
+
+ /**
+ * 合并数组
+ *
+ * @access public
+ * @param mixed $items
+ * @return static
+ */
+ public function merge($items)
+ {
+ return new static(array_merge($this->items, $this->convertToArray($items)));
+ }
+
+ /**
+ * 交换数组中的键和值
+ *
+ * @access public
+ * @return static
+ */
+ public function flip()
+ {
+ return new static(array_flip($this->items));
+ }
+
+ /**
+ * 按指定键整理数据
+ *
+ * @access public
+ * @param mixed $items 数据
+ * @param string $indexKey 键名
+ * @return array
+ */
+ public function dictionary($items = null, &$indexKey = null)
+ {
+ if ($items instanceof self || $items instanceof Paginator) {
+ $items = $items->all();
+ }
+
+ $items = is_null($items) ? $this->items : $items;
+
+ if ($items && empty($indexKey)) {
+ $indexKey = is_array($items[0]) ? 'id' : $items[0]->getPk();
+ }
+
+ if (isset($indexKey) && is_string($indexKey)) {
+ return array_column($items, null, $indexKey);
+ }
+
+ return $items;
+ }
+
+ /**
+ * 比较数组,返回差集
+ *
+ * @access public
+ * @param mixed $items 数据
+ * @param string $indexKey 指定比较的键名
+ * @return static
+ */
+ public function diff($items, $indexKey = null)
+ {
+ if ($this->isEmpty() || is_scalar($this->items[0])) {
+ return new static(array_diff($this->items, $this->convertToArray($items)));
+ }
+
+ $diff = [];
+ $dictionary = $this->dictionary($items, $indexKey);
+
+ if (is_string($indexKey)) {
+ foreach ($this->items as $item) {
+ if (!isset($dictionary[$item[$indexKey]])) {
+ $diff[] = $item;
+ }
+ }
+ }
+
+ return new static($diff);
+ }
+
+ /**
+ * 比较数组,返回交集
+ *
+ * @access public
+ * @param mixed $items 数据
+ * @param string $indexKey 指定比较的键名
+ * @return static
+ */
+ public function intersect($items, $indexKey = null)
+ {
+ if ($this->isEmpty() || is_scalar($this->items[0])) {
+ return new static(array_diff($this->items, $this->convertToArray($items)));
+ }
+
+ $intersect = [];
+ $dictionary = $this->dictionary($items, $indexKey);
+
+ if (is_string($indexKey)) {
+ foreach ($this->items as $item) {
+ if (isset($dictionary[$item[$indexKey]])) {
+ $intersect[] = $item;
+ }
+ }
+ }
+
+ return new static($intersect);
+ }
+
+ /**
+ * 返回数组中所有的键名
+ *
+ * @access public
+ * @return array
+ */
+ public function keys()
+ {
+ $current = current($this->items);
+
+ if (is_scalar($current)) {
+ $array = $this->items;
+ } elseif (is_array($current)) {
+ $array = $current;
+ } else {
+ $array = $current->toArray();
+ }
+
+ return array_keys($array);
+ }
+
+ /**
+ * 删除数组的最后一个元素(出栈)
+ *
+ * @access public
+ * @return mixed
+ */
+ public function pop()
+ {
+ return array_pop($this->items);
+ }
+
+ /**
+ * 通过使用用户自定义函数,以字符串返回数组
+ *
+ * @access public
+ * @param callable $callback
+ * @param mixed $initial
+ * @return mixed
+ */
+ public function reduce(callable $callback, $initial = null)
+ {
+ return array_reduce($this->items, $callback, $initial);
+ }
+
+ /**
+ * 以相反的顺序返回数组。
+ *
+ * @access public
+ * @return static
+ */
+ public function reverse()
+ {
+ return new static(array_reverse($this->items));
+ }
+
+ /**
+ * 删除数组中首个元素,并返回被删除元素的值
+ *
+ * @access public
+ * @return mixed
+ */
+ public function shift()
+ {
+ return array_shift($this->items);
+ }
+
+ /**
+ * 在数组结尾插入一个元素
+ * @access public
+ * @param mixed $value
+ * @param mixed $key
+ * @return void
+ */
+ public function push($value, $key = null)
+ {
+ if (is_null($key)) {
+ $this->items[] = $value;
+ } else {
+ $this->items[$key] = $value;
+ }
+ }
+
+ /**
+ * 把一个数组分割为新的数组块.
+ *
+ * @access public
+ * @param int $size
+ * @param bool $preserveKeys
+ * @return static
+ */
+ public function chunk($size, $preserveKeys = false)
+ {
+ $chunks = [];
+
+ foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
+ $chunks[] = new static($chunk);
+ }
+
+ return new static($chunks);
+ }
+
+ /**
+ * 在数组开头插入一个元素
+ * @access public
+ * @param mixed $value
+ * @param mixed $key
+ * @return void
+ */
+ public function unshift($value, $key = null)
+ {
+ if (is_null($key)) {
+ array_unshift($this->items, $value);
+ } else {
+ $this->items = [$key => $value] + $this->items;
+ }
+ }
+
+ /**
+ * 给每个元素执行个回调
+ *
+ * @access public
+ * @param callable $callback
+ * @return $this
+ */
+ public function each(callable $callback)
+ {
+ foreach ($this->items as $key => $item) {
+ $result = $callback($item, $key);
+
+ if (false === $result) {
+ break;
+ } elseif (!is_object($item)) {
+ $this->items[$key] = $result;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * 用回调函数处理数组中的元素
+ * @access public
+ * @param callable|null $callback
+ * @return static
+ */
+ public function map(callable $callback)
+ {
+ return new static(array_map($callback, $this->items));
+ }
+
+ /**
+ * 用回调函数过滤数组中的元素
+ * @access public
+ * @param callable|null $callback
+ * @return static
+ */
+ public function filter(callable $callback = null)
+ {
+ if ($callback) {
+ return new static(array_filter($this->items, $callback));
+ }
+
+ return new static(array_filter($this->items));
+ }
+
+ /**
+ * 根据字段条件过滤数组中的元素
+ * @access public
+ * @param string $field 字段名
+ * @param mixed $operator 操作符
+ * @param mixed $value 数据
+ * @return static
+ */
+ public function where($field, $operator, $value = null)
+ {
+ if (is_null($value)) {
+ $value = $operator;
+ $operator = '=';
+ }
+
+ return $this->filter(function ($data) use ($field, $operator, $value) {
+ if (strpos($field, '.')) {
+ list($field, $relation) = explode('.', $field);
+
+ $result = isset($data[$field][$relation]) ? $data[$field][$relation] : null;
+ } else {
+ $result = isset($data[$field]) ? $data[$field] : null;
+ }
+
+ switch (strtolower($operator)) {
+ case '===':
+ return $result === $value;
+ case '!==':
+ return $result !== $value;
+ case '!=':
+ case '<>':
+ return $result != $value;
+ case '>':
+ return $result > $value;
+ case '>=':
+ return $result >= $value;
+ case '<':
+ return $result < $value;
+ case '<=':
+ return $result <= $value;
+ case 'like':
+ return is_string($result) && false !== strpos($result, $value);
+ case 'not like':
+ return is_string($result) && false === strpos($result, $value);
+ case 'in':
+ return is_scalar($result) && in_array($result, $value, true);
+ case 'not in':
+ return is_scalar($result) && !in_array($result, $value, true);
+ case 'between':
+ list($min, $max) = is_string($value) ? explode(',', $value) : $value;
+ return is_scalar($result) && $result >= $min && $result <= $max;
+ case 'not between':
+ list($min, $max) = is_string($value) ? explode(',', $value) : $value;
+ return is_scalar($result) && $result > $max || $result < $min;
+ case '==':
+ case '=':
+ default:
+ return $result == $value;
+ }
+ });
+ }
+
+ /**
+ * 返回数据中指定的一列
+ * @access public
+ * @param mixed $columnKey 键名
+ * @param mixed $indexKey 作为索引值的列
+ * @return array
+ */
+ public function column($columnKey, $indexKey = null)
+ {
+ return array_column($this->items, $columnKey, $indexKey);
+ }
+
+ /**
+ * 对数组排序
+ *
+ * @access public
+ * @param callable|null $callback
+ * @return static
+ */
+ public function sort(callable $callback = null)
+ {
+ $items = $this->items;
+
+ $callback = $callback ?: function ($a, $b) {
+ return $a == $b ? 0 : (($a < $b) ? -1 : 1);
+
+ };
+
+ uasort($items, $callback);
+
+ return new static($items);
+ }
+
+ /**
+ * 指定字段排序
+ * @access public
+ * @param string $field 排序字段
+ * @param string $order 排序
+ * @param bool $intSort 是否为数字排序
+ * @return $this
+ */
+ public function order($field, $order = null, $intSort = true)
+ {
+ return $this->sort(function ($a, $b) use ($field, $order, $intSort) {
+ $fieldA = isset($a[$field]) ? $a[$field] : null;
+ $fieldB = isset($b[$field]) ? $b[$field] : null;
+
+ if ($intSort) {
+ return 'desc' == strtolower($order) ? $fieldB >= $fieldA : $fieldA >= $fieldB;
+ } else {
+ return 'desc' == strtolower($order) ? strcmp($fieldB, $fieldA) : strcmp($fieldA, $fieldB);
+ }
+ });
+ }
+
+ /**
+ * 将数组打乱
+ *
+ * @access public
+ * @return static
+ */
+ public function shuffle()
+ {
+ $items = $this->items;
+
+ shuffle($items);
+
+ return new static($items);
+ }
+
+ /**
+ * 截取数组
+ *
+ * @access public
+ * @param int $offset
+ * @param int $length
+ * @param bool $preserveKeys
+ * @return static
+ */
+ public function slice($offset, $length = null, $preserveKeys = false)
+ {
+ return new static(array_slice($this->items, $offset, $length, $preserveKeys));
+ }
+
+ // ArrayAccess
+ public function offsetExists($offset)
+ {
+ return array_key_exists($offset, $this->items);
+ }
+
+ public function offsetGet($offset)
+ {
+ return $this->items[$offset];
+ }
+
+ public function offsetSet($offset, $value)
+ {
+ if (is_null($offset)) {
+ $this->items[] = $value;
+ } else {
+ $this->items[$offset] = $value;
+ }
+ }
+
+ public function offsetUnset($offset)
+ {
+ unset($this->items[$offset]);
+ }
+
+ //Countable
+ public function count()
+ {
+ return count($this->items);
+ }
+
+ //IteratorAggregate
+ public function getIterator()
+ {
+ return new ArrayIterator($this->items);
+ }
+
+ //JsonSerializable
+ public function jsonSerialize()
+ {
+ return $this->toArray();
+ }
+
+ /**
+ * 转换当前数据集为JSON字符串
+ * @access public
+ * @param integer $options json参数
+ * @return string
+ */
+ public function toJson($options = JSON_UNESCAPED_UNICODE)
+ {
+ return json_encode($this->toArray(), $options);
+ }
+
+ public function __toString()
+ {
+ return $this->toJson();
+ }
+
+ /**
+ * 转换成数组
+ *
+ * @access public
+ * @param mixed $items
+ * @return array
+ */
+ protected function convertToArray($items)
+ {
+ if ($items instanceof self) {
+ return $items->all();
+ }
+
+ return (array) $items;
+ }
+}
diff --git a/thinkphp/library/think/Config.php b/thinkphp/library/think/Config.php
new file mode 100755
index 0000000000000000000000000000000000000000..bec6222adbf0a0b94d7df77f2efbbd1dcb162f1c
--- /dev/null
+++ b/thinkphp/library/think/Config.php
@@ -0,0 +1,398 @@
+
+// +----------------------------------------------------------------------
+
+namespace think;
+
+use Yaconf;
+
+class Config implements \ArrayAccess
+{
+ /**
+ * 配置参数
+ * @var array
+ */
+ protected $config = [];
+
+ /**
+ * 配置前缀
+ * @var string
+ */
+ protected $prefix = 'app';
+
+ /**
+ * 配置文件目录
+ * @var string
+ */
+ protected $path;
+
+ /**
+ * 配置文件后缀
+ * @var string
+ */
+ protected $ext;
+
+ /**
+ * 是否支持Yaconf
+ * @var bool
+ */
+ protected $yaconf;
+
+ /**
+ * 构造方法
+ * @access public
+ */
+ public function __construct($path = '', $ext = '.php')
+ {
+ $this->path = $path;
+ $this->ext = $ext;
+ $this->yaconf = class_exists('Yaconf');
+ }
+
+ public static function __make(App $app)
+ {
+ $path = $app->getConfigPath();
+ $ext = $app->getConfigExt();
+ return new static($path, $ext);
+ }
+
+ /**
+ * 设置开启Yaconf
+ * @access public
+ * @param bool|string $yaconf 是否使用Yaconf
+ * @return void
+ */
+ public function setYaconf($yaconf)
+ {
+ if ($this->yaconf) {
+ $this->yaconf = $yaconf;
+ }
+ }
+
+ /**
+ * 设置配置参数默认前缀
+ * @access public
+ * @param string $prefix 前缀
+ * @return void
+ */
+ public function setDefaultPrefix($prefix)
+ {
+ $this->prefix = $prefix;
+ }
+
+ /**
+ * 解析配置文件或内容
+ * @access public
+ * @param string $config 配置文件路径或内容
+ * @param string $type 配置解析类型
+ * @param string $name 配置名(如设置即表示二级配置)
+ * @return mixed
+ */
+ public function parse($config, $type = '', $name = '')
+ {
+ if (empty($type)) {
+ $type = pathinfo($config, PATHINFO_EXTENSION);
+ }
+
+ $object = Loader::factory($type, '\\think\\config\\driver\\', $config);
+
+ return $this->set($object->parse(), $name);
+ }
+
+ /**
+ * 加载配置文件(多种格式)
+ * @access public
+ * @param string $file 配置文件名
+ * @param string $name 一级配置名
+ * @return mixed
+ */
+ public function load($file, $name = '')
+ {
+ if (is_file($file)) {
+ $filename = $file;
+ } elseif (is_file($this->path . $file . $this->ext)) {
+ $filename = $this->path . $file . $this->ext;
+ }
+
+ if (isset($filename)) {
+ return $this->loadFile($filename, $name);
+ } elseif ($this->yaconf && Yaconf::has($file)) {
+ return $this->set(Yaconf::get($file), $name);
+ }
+
+ return $this->config;
+ }
+
+ /**
+ * 获取实际的yaconf配置参数
+ * @access protected
+ * @param string $name 配置参数名
+ * @return string
+ */
+ protected function getYaconfName($name)
+ {
+ if ($this->yaconf && is_string($this->yaconf)) {
+ return $this->yaconf . '.' . $name;
+ }
+
+ return $name;
+ }
+
+ /**
+ * 获取yaconf配置
+ * @access public
+ * @param string $name 配置参数名
+ * @param mixed $default 默认值
+ * @return mixed
+ */
+ public function yaconf($name, $default = null)
+ {
+ if ($this->yaconf) {
+ $yaconfName = $this->getYaconfName($name);
+
+ if (Yaconf::has($yaconfName)) {
+ return Yaconf::get($yaconfName);
+ }
+ }
+
+ return $default;
+ }
+
+ protected function loadFile($file, $name)
+ {
+ $name = strtolower($name);
+ $type = pathinfo($file, PATHINFO_EXTENSION);
+
+ if ('php' == $type) {
+ return $this->set(include $file, $name);
+ } elseif ('yaml' == $type && function_exists('yaml_parse_file')) {
+ return $this->set(yaml_parse_file($file), $name);
+ }
+
+ return $this->parse($file, $type, $name);
+ }
+
+ /**
+ * 检测配置是否存在
+ * @access public
+ * @param string $name 配置参数名(支持多级配置 .号分割)
+ * @return bool
+ */
+ public function has($name)
+ {
+ if (false === strpos($name, '.')) {
+ $name = $this->prefix . '.' . $name;
+ }
+
+ return !is_null($this->get($name));
+ }
+
+ /**
+ * 获取一级配置
+ * @access public
+ * @param string $name 一级配置名
+ * @return array
+ */
+ public function pull($name)
+ {
+ $name = strtolower($name);
+
+ if ($this->yaconf) {
+ $yaconfName = $this->getYaconfName($name);
+
+ if (Yaconf::has($yaconfName)) {
+ $config = Yaconf::get($yaconfName);
+ return isset($this->config[$name]) ? array_merge($this->config[$name], $config) : $config;
+ }
+ }
+
+ return isset($this->config[$name]) ? $this->config[$name] : [];
+ }
+
+ /**
+ * 获取配置参数 为空则获取所有配置
+ * @access public
+ * @param string $name 配置参数名(支持多级配置 .号分割)
+ * @param mixed $default 默认值
+ * @return mixed
+ */
+ public function get($name = null, $default = null)
+ {
+ if ($name && false === strpos($name, '.')) {
+ $name = $this->prefix . '.' . $name;
+ }
+
+ // 无参数时获取所有
+ if (empty($name)) {
+ return $this->config;
+ }
+
+ if ('.' == substr($name, -1)) {
+ return $this->pull(substr($name, 0, -1));
+ }
+
+ if ($this->yaconf) {
+ $yaconfName = $this->getYaconfName($name);
+
+ if (Yaconf::has($yaconfName)) {
+ return Yaconf::get($yaconfName);
+ }
+ }
+
+ $name = explode('.', $name);
+ $name[0] = strtolower($name[0]);
+ $config = $this->config;
+
+ // 按.拆分成多维数组进行判断
+ foreach ($name as $val) {
+ if (isset($config[$val])) {
+ $config = $config[$val];
+ } else {
+ return $default;
+ }
+ }
+
+ return $config;
+ }
+
+ /**
+ * 设置配置参数 name为数组则为批量设置
+ * @access public
+ * @param string|array $name 配置参数名(支持三级配置 .号分割)
+ * @param mixed $value 配置值
+ * @return mixed
+ */
+ public function set($name, $value = null)
+ {
+ if (is_string($name)) {
+ if (false === strpos($name, '.')) {
+ $name = $this->prefix . '.' . $name;
+ }
+
+ $name = explode('.', $name, 3);
+
+ if (count($name) == 2) {
+ $this->config[strtolower($name[0])][$name[1]] = $value;
+ } else {
+ $this->config[strtolower($name[0])][$name[1]][$name[2]] = $value;
+ }
+
+ return $value;
+ } elseif (is_array($name)) {
+ // 批量设置
+ if (!empty($value)) {
+ if (isset($this->config[$value])) {
+ $result = array_merge($this->config[$value], $name);
+ } else {
+ $result = $name;
+ }
+
+ $this->config[$value] = $result;
+ } else {
+ $result = $this->config = array_merge($this->config, $name);
+ }
+ } else {
+ // 为空直接返回 已有配置
+ $result = $this->config;
+ }
+
+ return $result;
+ }
+
+ /**
+ * 移除配置
+ * @access public
+ * @param string $name 配置参数名(支持三级配置 .号分割)
+ * @return void
+ */
+ public function remove($name)
+ {
+ if (false === strpos($name, '.')) {
+ $name = $this->prefix . '.' . $name;
+ }
+
+ $name = explode('.', $name, 3);
+
+ if (count($name) == 2) {
+ unset($this->config[strtolower($name[0])][$name[1]]);
+ } else {
+ unset($this->config[strtolower($name[0])][$name[1]][$name[2]]);
+ }
+ }
+
+ /**
+ * 重置配置参数
+ * @access public
+ * @param string $prefix 配置前缀名
+ * @return void
+ */
+ public function reset($prefix = '')
+ {
+ if ('' === $prefix) {
+ $this->config = [];
+ } else {
+ $this->config[$prefix] = [];
+ }
+ }
+
+ /**
+ * 设置配置
+ * @access public
+ * @param string $name 参数名
+ * @param mixed $value 值
+ */
+ public function __set($name, $value)
+ {
+ return $this->set($name, $value);
+ }
+
+ /**
+ * 获取配置参数
+ * @access public
+ * @param string $name 参数名
+ * @return mixed
+ */
+ public function __get($name)
+ {
+ return $this->get($name);
+ }
+
+ /**
+ * 检测是否存在参数
+ * @access public
+ * @param string $name 参数名
+ * @return bool
+ */
+ public function __isset($name)
+ {
+ return $this->has($name);
+ }
+
+ // ArrayAccess
+ public function offsetSet($name, $value)
+ {
+ $this->set($name, $value);
+ }
+
+ public function offsetExists($name)
+ {
+ return $this->has($name);
+ }
+
+ public function offsetUnset($name)
+ {
+ $this->remove($name);
+ }
+
+ public function offsetGet($name)
+ {
+ return $this->get($name);
+ }
+}
diff --git a/thinkphp/library/think/Console.php b/thinkphp/library/think/Console.php
new file mode 100755
index 0000000000000000000000000000000000000000..22f3e2c5f512f7bd3e67cb32170efd5f5c4a04be
--- /dev/null
+++ b/thinkphp/library/think/Console.php
@@ -0,0 +1,829 @@
+
+// +----------------------------------------------------------------------
+
+namespace think;
+
+use think\console\Command;
+use think\console\command\Help as HelpCommand;
+use think\console\Input;
+use think\console\input\Argument as InputArgument;
+use think\console\input\Definition as InputDefinition;
+use think\console\input\Option as InputOption;
+use think\console\Output;
+use think\console\output\driver\Buffer;
+
+class Console
+{
+
+ private $name;
+ private $version;
+
+ /** @var Command[] */
+ private $commands = [];
+
+ private $wantHelps = false;
+
+ private $catchExceptions = true;
+ private $autoExit = true;
+ private $definition;
+ private $defaultCommand;
+
+ private static $defaultCommands = [
+ 'help' => "think\\console\\command\\Help",
+ 'list' => "think\\console\\command\\Lists",
+ 'build' => "think\\console\\command\\Build",
+ 'clear' => "think\\console\\command\\Clear",
+ 'make:command' => "think\\console\\command\\make\\Command",
+ 'make:controller' => "think\\console\\command\\make\\Controller",
+ 'make:model' => "think\\console\\command\\make\\Model",
+ 'make:middleware' => "think\\console\\command\\make\\Middleware",
+ 'make:validate' => "think\\console\\command\\make\\Validate",
+ 'optimize:autoload' => "think\\console\\command\\optimize\\Autoload",
+ 'optimize:config' => "think\\console\\command\\optimize\\Config",
+ 'optimize:schema' => "think\\console\\command\\optimize\\Schema",
+ 'optimize:route' => "think\\console\\command\\optimize\\Route",
+ 'run' => "think\\console\\command\\RunServer",
+ 'version' => "think\\console\\command\\Version",
+ 'route:list' => "think\\console\\command\\RouteList",
+ ];
+
+ /**
+ * Console constructor.
+ * @access public
+ * @param string $name 名称
+ * @param string $version 版本
+ * @param null|string $user 执行用户
+ */
+ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', $user = null)
+ {
+ $this->name = $name;
+ $this->version = $version;
+
+ if ($user) {
+ $this->setUser($user);
+ }
+
+ $this->defaultCommand = 'list';
+ $this->definition = $this->getDefaultInputDefinition();
+ }
+
+ /**
+ * 设置执行用户
+ * @param $user
+ */
+ public function setUser($user)
+ {
+ if (DIRECTORY_SEPARATOR == '\\') {
+ return;
+ }
+
+ $user = posix_getpwnam($user);
+ if ($user) {
+ posix_setuid($user['uid']);
+ posix_setgid($user['gid']);
+ }
+ }
+
+ /**
+ * 初始化 Console
+ * @access public
+ * @param bool $run 是否运行 Console
+ * @return int|Console
+ */
+ public static function init($run = true)
+ {
+ static $console;
+
+ if (!$console) {
+ $config = Container::get('config')->pull('console');
+ $console = new self($config['name'], $config['version'], $config['user']);
+
+ $commands = $console->getDefinedCommands($config);
+
+ // 添加指令集
+ $console->addCommands($commands);
+ }
+
+ if ($run) {
+ // 运行
+ return $console->run();
+ } else {
+ return $console;
+ }
+ }
+
+ /**
+ * @access public
+ * @param array $config
+ * @return array
+ */
+ public function getDefinedCommands(array $config = [])
+ {
+ $commands = self::$defaultCommands;
+
+ if (!empty($config['auto_path']) && is_dir($config['auto_path'])) {
+ // 自动加载指令类
+ $files = scandir($config['auto_path']);
+
+ if (count($files) > 2) {
+ $beforeClass = get_declared_classes();
+
+ foreach ($files as $file) {
+ if (pathinfo($file, PATHINFO_EXTENSION) == 'php') {
+ include $config['auto_path'] . $file;
+ }
+ }
+
+ $afterClass = get_declared_classes();
+ $commands = array_merge($commands, array_diff($afterClass, $beforeClass));
+ }
+ }
+
+ $file = Container::get('env')->get('app_path') . 'command.php';
+
+ if (is_file($file)) {
+ $appCommands = include $file;
+
+ if (is_array($appCommands)) {
+ $commands = array_merge($commands, $appCommands);
+ }
+ }
+
+ return $commands;
+ }
+
+ /**
+ * @access public
+ * @param string $command
+ * @param array $parameters
+ * @param string $driver
+ * @return Output|Buffer
+ */
+ public static function call($command, array $parameters = [], $driver = 'buffer')
+ {
+ $console = self::init(false);
+
+ array_unshift($parameters, $command);
+
+ $input = new Input($parameters);
+ $output = new Output($driver);
+
+ $console->setCatchExceptions(false);
+ $console->find($command)->run($input, $output);
+
+ return $output;
+ }
+
+ /**
+ * 执行当前的指令
+ * @access public
+ * @return int
+ * @throws \Exception
+ * @api
+ */
+ public function run()
+ {
+ $input = new Input();
+ $output = new Output();
+
+ $this->configureIO($input, $output);
+
+ try {
+ $exitCode = $this->doRun($input, $output);
+ } catch (\Exception $e) {
+ if (!$this->catchExceptions) {
+ throw $e;
+ }
+
+ $output->renderException($e);
+
+ $exitCode = $e->getCode();
+ if (is_numeric($exitCode)) {
+ $exitCode = (int) $exitCode;
+ if (0 === $exitCode) {
+ $exitCode = 1;
+ }
+ } else {
+ $exitCode = 1;
+ }
+ }
+
+ if ($this->autoExit) {
+ if ($exitCode > 255) {
+ $exitCode = 255;
+ }
+
+ exit($exitCode);
+ }
+
+ return $exitCode;
+ }
+
+ /**
+ * 执行指令
+ * @access public
+ * @param Input $input
+ * @param Output $output
+ * @return int
+ */
+ public function doRun(Input $input, Output $output)
+ {
+ if (true === $input->hasParameterOption(['--version', '-V'])) {
+ $output->writeln($this->getLongVersion());
+
+ return 0;
+ }
+
+ $name = $this->getCommandName($input);
+
+ if (true === $input->hasParameterOption(['--help', '-h'])) {
+ if (!$name) {
+ $name = 'help';
+ $input = new Input(['help']);
+ } else {
+ $this->wantHelps = true;
+ }
+ }
+
+ if (!$name) {
+ $name = $this->defaultCommand;
+ $input = new Input([$this->defaultCommand]);
+ }
+
+ $command = $this->find($name);
+
+ $exitCode = $this->doRunCommand($command, $input, $output);
+
+ return $exitCode;
+ }
+
+ /**
+ * 设置输入参数定义
+ * @access public
+ * @param InputDefinition $definition
+ */
+ public function setDefinition(InputDefinition $definition)
+ {
+ $this->definition = $definition;
+ }
+
+ /**
+ * 获取输入参数定义
+ * @access public
+ * @return InputDefinition The InputDefinition instance
+ */
+ public function getDefinition()
+ {
+ return $this->definition;
+ }
+
+ /**
+ * Gets the help message.
+ * @access public
+ * @return string A help message.
+ */
+ public function getHelp()
+ {
+ return $this->getLongVersion();
+ }
+
+ /**
+ * 是否捕获异常
+ * @access public
+ * @param bool $boolean
+ * @api
+ */
+ public function setCatchExceptions($boolean)
+ {
+ $this->catchExceptions = (bool) $boolean;
+ }
+
+ /**
+ * 是否自动退出
+ * @access public
+ * @param bool $boolean
+ * @api
+ */
+ public function setAutoExit($boolean)
+ {
+ $this->autoExit = (bool) $boolean;
+ }
+
+ /**
+ * 获取名称
+ * @access public
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * 设置名称
+ * @access public
+ * @param string $name
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * 获取版本
+ * @access public
+ * @return string
+ * @api
+ */
+ public function getVersion()
+ {
+ return $this->version;
+ }
+
+ /**
+ * 设置版本
+ * @access public
+ * @param string $version
+ */
+ public function setVersion($version)
+ {
+ $this->version = $version;
+ }
+
+ /**
+ * 获取完整的版本号
+ * @access public
+ * @return string
+ */
+ public function getLongVersion()
+ {
+ if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
+ return sprintf('
' . $label . $output . ''; + } + if ($echo) { + echo($output); + return; + } + return $output; + } + + public function inject(Response $response, &$content) + { + $config = $this->config; + $type = isset($config['type']) ? $config['type'] : 'Html'; + + unset($config['type']); + + $trace = Loader::factory($type, '\\think\\debug\\', $config); + + if ($response instanceof Redirect) { + //TODO 记录 + } else { + $output = $trace->output($response, $this->app['log']->getLog()); + if (is_string($output)) { + // trace调试信息注入 + $pos = strripos($content, '