改进console模式

This commit is contained in:
yunwuxin
2016-06-13 18:48:07 +08:00
parent e14174fd63
commit eb026e02a6
4 changed files with 77 additions and 178 deletions

View File

@@ -29,6 +29,11 @@ use think\Route;
*/ */
class App class App
{ {
/**
* @var bool 是否初始化过
*/
protected static $init = false;
/** /**
* 执行应用程序 * 执行应用程序
* @access public * @access public
@@ -40,30 +45,9 @@ class App
{ {
is_null($request) && $request = Request::instance(); is_null($request) && $request = Request::instance();
// 初始化应用 $config = self::initCommon();
$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']);
try { try {
// 监听app_init
Hook::listen('app_init');
// 开启多语言机制 // 开启多语言机制
if ($config['lang_switch_on']) { if ($config['lang_switch_on']) {
@@ -136,7 +120,7 @@ class App
* 执行函数或者闭包方法 支持参数调用 * 执行函数或者闭包方法 支持参数调用
* @access public * @access public
* @param string|array|\Closure $function 函数或者闭包 * @param string|array|\Closure $function 函数或者闭包
* @param array $vars 变量 * @param array $vars 变量
* @return mixed * @return mixed
*/ */
public static function invokeFunction($function, $vars = []) public static function invokeFunction($function, $vars = [])
@@ -152,7 +136,7 @@ class App
* 调用反射执行类的方法 支持参数绑定 * 调用反射执行类的方法 支持参数绑定
* @access public * @access public
* @param string|array $method 方法 * @param string|array $method 方法
* @param array $vars 变量 * @param array $vars 变量
* @return mixed * @return mixed
*/ */
public static function invokeMethod($method, $vars = []) public static function invokeMethod($method, $vars = [])
@@ -178,7 +162,7 @@ class App
* 绑定参数 * 绑定参数
* @access public * @access public
* @param \ReflectionMethod $reflect 反射类 * @param \ReflectionMethod $reflect 反射类
* @param array $vars 变量 * @param array $vars 变量
* @return array * @return array
*/ */
private static function bindParams($reflect, $vars) private static function bindParams($reflect, $vars)
@@ -239,7 +223,7 @@ class App
// 模块初始化 // 模块初始化
if ($module && $available) { if ($module && $available) {
// 初始化模块 // 初始化模块
$config = self::init($module, $config); $config = self::init($module);
} else { } else {
throw new HttpException(404, 'module [ ' . $module . ' ] not exists '); throw new HttpException(404, 'module [ ' . $module . ' ] not exists ');
} }
@@ -299,14 +283,47 @@ class App
return $data; 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 * @access public
* @param string $module 模块名 * @param string $module 模块名
* @param array $config 配置参数 * @return array
* @return void
*/ */
private static function init($module, $config) private static function init($module = '')
{ {
// 定位模块目录 // 定位模块目录
$module = ($module && APP_MULTI_MODULE) ? $module . DS : ''; $module = ($module && APP_MULTI_MODULE) ? $module . DS : '';
@@ -359,9 +376,9 @@ class App
* URL路由检测根据PATH_INFO) * URL路由检测根据PATH_INFO)
* @access public * @access public
* @param \think\Request $request * @param \think\Request $request
* @param array $config * @param array $config
* @return array * @return array
* @throws HttpException * @throws \think\Exception
*/ */
public static function route($request, array $config) public static function route($request, array $config)
{ {

View File

@@ -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 * @return int
@@ -90,7 +110,7 @@ class Console
$exitCode = $e->getCode(); $exitCode = $e->getCode();
if (is_numeric($exitCode)) { if (is_numeric($exitCode)) {
$exitCode = (int) $exitCode; $exitCode = (int)$exitCode;
if (0 === $exitCode) { if (0 === $exitCode) {
$exitCode = 1; $exitCode = 1;
} }
@@ -201,7 +221,7 @@ class Console
*/ */
public function setCatchExceptions($boolean) public function setCatchExceptions($boolean)
{ {
$this->catchExceptions = (bool) $boolean; $this->catchExceptions = (bool)$boolean;
} }
/** /**
@@ -211,7 +231,7 @@ class Console
*/ */
public function setAutoExit($boolean) public function setAutoExit($boolean)
{ {
$this->autoExit = (bool) $boolean; $this->autoExit = (bool)$boolean;
} }
/** /**
@@ -379,7 +399,7 @@ class Console
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*'; return preg_quote($matches[1]) . '[^:]*';
}, $namespace); }, $namespace);
$namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces);
if (empty($namespaces)) { if (empty($namespaces)) {
$message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
@@ -417,7 +437,7 @@ class Console
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { $expr = preg_replace_callback('{([^:]+|)}', function ($matches) {
return preg_quote($matches[1]) . '[^:]*'; return preg_quote($matches[1]) . '[^:]*';
}, $name); }, $name);
$commands = preg_grep('{^' . $expr . '}', $allCommands); $commands = preg_grep('{^' . $expr . '}', $allCommands);
if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) { if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) {
if (false !== $pos = strrpos($name, ':')) { if (false !== $pos = strrpos($name, ':')) {
@@ -606,19 +626,19 @@ class Console
if ('\\' === DS) { if ('\\' === DS) {
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { 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)) { 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 ($sttyString = $this->getSttyColumns()) {
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { 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)) { if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
return [(int) $matches[2], (int) $matches[1]]; return [(int)$matches[2], (int)$matches[1]];
} }
} }

View File

@@ -1,31 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
/**
* 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,
];

View File

@@ -1,107 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
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');
}
}