改进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
{
/**
* @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)
{

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
@@ -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]];
}
}