改进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']) {
@@ -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 : '';
@@ -361,7 +378,7 @@ class App
* @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

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');
}
}