diff --git a/library/think/App.php b/library/think/App.php index c25cc92b..5dc27bf3 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -29,6 +29,11 @@ use think\Route; */ class App { + /** + * @var bool 是否初始化过 + */ + protected static $init = false; + /** * 执行应用程序 * @access public @@ -40,30 +45,9 @@ class App { is_null($request) && $request = Request::instance(); - // 初始化应用 - $config = self::init('', Config::get()); - - // 注册根命名空间 - if (!empty($config['root_namespace'])) { - Loader::addNamespace($config['root_namespace']); - } - - // 加载额外文件 - if (!empty($config['extra_file_list'])) { - foreach ($config['extra_file_list'] as $file) { - $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; - if (is_file($file)) { - include_once $file; - } - } - } - - // 设置系统时区 - date_default_timezone_set($config['default_timezone']); + $config = self::initCommon(); try { - // 监听app_init - Hook::listen('app_init'); // 开启多语言机制 if ($config['lang_switch_on']) { @@ -136,7 +120,7 @@ class App * 执行函数或者闭包方法 支持参数调用 * @access public * @param string|array|\Closure $function 函数或者闭包 - * @param array $vars 变量 + * @param array $vars 变量 * @return mixed */ public static function invokeFunction($function, $vars = []) @@ -152,7 +136,7 @@ class App * 调用反射执行类的方法 支持参数绑定 * @access public * @param string|array $method 方法 - * @param array $vars 变量 + * @param array $vars 变量 * @return mixed */ public static function invokeMethod($method, $vars = []) @@ -178,7 +162,7 @@ class App * 绑定参数 * @access public * @param \ReflectionMethod $reflect 反射类 - * @param array $vars 变量 + * @param array $vars 变量 * @return array */ private static function bindParams($reflect, $vars) @@ -239,7 +223,7 @@ class App // 模块初始化 if ($module && $available) { // 初始化模块 - $config = self::init($module, $config); + $config = self::init($module); } else { throw new HttpException(404, 'module [ ' . $module . ' ] not exists '); } @@ -299,14 +283,47 @@ class App return $data; } + /** + * 初始化公共配置 + */ + public static function initCommon() + { + if (empty(self::$init)) { + // 初始化应用 + self::$init = $config = self::init(); + + // 注册根命名空间 + if (!empty($config['root_namespace'])) { + Loader::addNamespace($config['root_namespace']); + } + + // 加载额外文件 + if (!empty($config['extra_file_list'])) { + foreach ($config['extra_file_list'] as $file) { + $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; + if (is_file($file)) { + include_once $file; + } + } + } + + // 设置系统时区 + date_default_timezone_set($config['default_timezone']); + + // 监听app_init + Hook::listen('app_init'); + } + return self::$init; + } + + /** * 初始化应用或模块 * @access public * @param string $module 模块名 - * @param array $config 配置参数 - * @return void + * @return array */ - private static function init($module, $config) + private static function init($module = '') { // 定位模块目录 $module = ($module && APP_MULTI_MODULE) ? $module . DS : ''; @@ -359,9 +376,9 @@ class App * URL路由检测(根据PATH_INFO) * @access public * @param \think\Request $request - * @param array $config + * @param array $config * @return array - * @throws HttpException + * @throws \think\Exception */ public static function route($request, array $config) { diff --git a/library/think/Console.php b/library/think/Console.php index c4d96f59..3dc31467 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -66,6 +66,26 @@ class Console } } + public static function init() + { + // 实例化console + $console = new self('Think Console', '0.1'); + // 读取指令集 + if (is_file(CONF_PATH . 'command' . EXT)) { + $commands = include CONF_PATH . 'command' . EXT; + if (is_array($commands)) { + foreach ($commands as $command) { + if (class_exists($command) && is_subclass_of($command, "\\think\\console\\command\\Command")) { + // 注册指令 + $console->add(new $command()); + } + } + } + } + // 运行 + $console->run(); + } + /** * 执行当前的指令 * @return int @@ -90,7 +110,7 @@ class Console $exitCode = $e->getCode(); if (is_numeric($exitCode)) { - $exitCode = (int) $exitCode; + $exitCode = (int)$exitCode; if (0 === $exitCode) { $exitCode = 1; } @@ -201,7 +221,7 @@ class Console */ public function setCatchExceptions($boolean) { - $this->catchExceptions = (bool) $boolean; + $this->catchExceptions = (bool)$boolean; } /** @@ -211,7 +231,7 @@ class Console */ public function setAutoExit($boolean) { - $this->autoExit = (bool) $boolean; + $this->autoExit = (bool)$boolean; } /** @@ -379,7 +399,7 @@ class Console $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]) . '[^:]*'; }, $namespace); - $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); + $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); if (empty($namespaces)) { $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); @@ -417,7 +437,7 @@ class Console $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]) . '[^:]*'; }, $name); - $commands = preg_grep('{^' . $expr . '}', $allCommands); + $commands = preg_grep('{^' . $expr . '}', $allCommands); if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) { if (false !== $pos = strrpos($name, ':')) { @@ -606,19 +626,19 @@ class Console if ('\\' === DS) { if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { - return [(int) $matches[1], (int) $matches[2]]; + return [(int)$matches[1], (int)$matches[2]]; } if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { - return [(int) $matches[1], (int) $matches[2]]; + return [(int)$matches[1], (int)$matches[2]]; } } if ($sttyString = $this->getSttyColumns()) { if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { - return [(int) $matches[2], (int) $matches[1]]; + return [(int)$matches[2], (int)$matches[1]]; } if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { - return [(int) $matches[2], (int) $matches[1]]; + return [(int)$matches[2], (int)$matches[1]]; } } diff --git a/mode/console.php b/mode/console.php deleted file mode 100644 index 4b1be21b..00000000 --- a/mode/console.php +++ /dev/null @@ -1,31 +0,0 @@ - -// +---------------------------------------------------------------------- - -/** - * ThinkPHP CLI模式定义 - */ -return [ - - // 命名空间 - 'namespace' => [ - 'think' => LIB_PATH . 'think' . DS, - 'behavior' => LIB_PATH . 'behavior' . DS, - 'traits' => LIB_PATH . 'traits' . DS, - APP_NAMESPACE => APP_PATH, - ], - // 别名定义 - 'alias' => [ - 'think\App' => MODE_PATH . 'console' . DS . 'App' . EXT, - ], - // 配置文件 - 'config' => THINK_PATH . 'convention' . EXT, - -]; diff --git a/mode/console/App.php b/mode/console/App.php deleted file mode 100644 index 7376a84a..00000000 --- a/mode/console/App.php +++ /dev/null @@ -1,107 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think; - -use think\Console; - -class App -{ - /** - * 执行应用程序 - * @access public - * @return void - */ - public static function run() - { - self::init(); - - // 实例化console - $console = new Console('Think Console', '0.1'); - // 读取指令集 - if (is_file(CONF_PATH . 'command' . EXT)) { - $commands = include CONF_PATH . 'command' . EXT; - if (is_array($commands)) { - foreach ($commands as $command) { - if (class_exists($command) && is_subclass_of($command, "\\think\\console\\command\\Command")) { - // 注册指令 - $console->add(new $command()); - } - } - } - } - // 运行 - $console->run(); - } - - private static function init() - { - // 加载初始化文件 - if (is_file(APP_PATH . 'init' . EXT)) { - include APP_PATH . 'init' . EXT; - - // 加载模块配置 - $config = Config::get(); - } else { - // 加载模块配置 - $config = Config::load(CONF_PATH . 'config' . CONF_EXT); - - // 加载应用状态配置 - if ($config['app_status']) { - $config = Config::load(CONF_PATH . $config['app_status'] . CONF_EXT); - } - - // 读取扩展配置文件 - if ($config['extra_config_list']) { - foreach ($config['extra_config_list'] as $name => $file) { - $filename = CONF_PATH . $file . CONF_EXT; - Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME)); - } - } - - // 加载别名文件 - if (is_file(CONF_PATH . 'alias' . EXT)) { - Loader::addMap(include CONF_PATH . 'alias' . EXT); - } - - // 加载行为扩展文件 - if (is_file(CONF_PATH . 'tags' . EXT)) { - Hook::import(include CONF_PATH . 'tags' . EXT); - } - - // 加载公共文件 - if (is_file(APP_PATH . 'common' . EXT)) { - include APP_PATH . 'common' . EXT; - } - } - - // 注册根命名空间 - if (!empty($config['root_namespace'])) { - Loader::addNamespace($config['root_namespace']); - } - - // 加载额外文件 - if (!empty($config['extra_file_list'])) { - foreach ($config['extra_file_list'] as $file) { - $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; - if (is_file($file)) { - include_once $file; - } - } - } - - // 设置系统时区 - date_default_timezone_set($config['default_timezone']); - - // 监听app_init - Hook::listen('app_init'); - } -}