取消APP_NAMESPACE常量定义 改为 App::$namespace 调整Build类的module和run方法 增加namespace参数

This commit is contained in:
thinkphp
2016-06-15 22:42:49 +08:00
parent fe8f51dd68
commit 2aa3e4fa5b
9 changed files with 33 additions and 22 deletions

View File

@@ -27,7 +27,6 @@ defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DS);
defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH . 'log' . DS);
defined('CACHE_PATH') or define('CACHE_PATH', RUNTIME_PATH . 'cache' . DS);
defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS);
defined('APP_NAMESPACE') or define('APP_NAMESPACE', 'app');
defined('CONF_PATH') or define('CONF_PATH', APP_PATH); // 配置文件目录
defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀
defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀

View File

@@ -5,6 +5,8 @@ return [
// | 应用设置
// +----------------------------------------------------------------------
// 应用命名空间
'app_namespace' => 'app',
// 应用调试模式
'app_debug' => true,
// 应用模式状态

View File

@@ -44,6 +44,11 @@ class App
*/
public static $debug = true;
/**
* @var string 应用类库命名空间
*/
public static $namespace = 'app';
/**
* 执行应用程序
* @access public
@@ -300,15 +305,17 @@ class App
{
if (empty(self::$init)) {
// 初始化应用
self::$init = $config = self::init();
$config = self::init();
// 是否调试模式
self::$debug = Config::get('app_debug');
// 应用调试模式
self::$debug = Config::get('app_debug');
if (!self::$debug) {
ini_set('display_errors', 'Off');
}
// 注册根命名空间
// 应用命名空间
self::$namespace = $config['app_namespace'];
Loader::addNamespace($config['app_namespace'], APP_PATH);
if (!empty($config['root_namespace'])) {
Loader::addNamespace($config['root_namespace']);
}
@@ -328,6 +335,8 @@ class App
// 监听app_init
Hook::listen('app_init');
self::$init = $config;
}
return self::$init;
}

View File

@@ -17,9 +17,10 @@ class Build
* 根据传入的build资料创建目录和文件
* @access protected
* @param array $build build列表
* @param string $namespace 应用类库命名空间
* @return void
*/
public static function run(array $build = [])
public static function run(array $build = [], $namespace = 'app')
{
// 锁定
$lockfile = APP_PATH . 'build.lock';
@@ -37,7 +38,7 @@ class Build
self::buildFile($list);
} else {
// 创建模块
self::module($module, $list);
self::module($module, $list, $namespace);
}
}
// 解除锁定
@@ -83,10 +84,11 @@ class Build
* 创建模块
* @access public
* @param string $module 模块名
* @param array $list build列表
* @param array $list build列表
* @param string $namespace 应用类库命名空间
* @return void
*/
public static function module($module = '', $list = [])
public static function module($module = '', $list = [], $namespace = 'app')
{
$module = $module ? $module : '';
if (!is_dir(APP_PATH . $module)) {
@@ -97,7 +99,7 @@ class Build
// 创建配置文件和公共文件
self::buildCommon($module);
// 创建模块的默认页面
self::buildHello($module);
self::buildHello($module, $namespace);
}
if (empty($list)) {
// 创建默认的模块目录和文件
@@ -129,7 +131,7 @@ class Build
foreach ($file as $val) {
$val = trim($val);
$filename = $modulePath . $path . DS . $val . (CLASS_APPEND_SUFFIX ? ucfirst($path) : '') . EXT;
$namespace = APP_NAMESPACE . '\\' . ($module ? $module . '\\' : '') . $path;
$namespace = $namespace . '\\' . ($module ? $module . '\\' : '') . $path;
$class = $val . (CLASS_APPEND_SUFFIX ? ucfirst($path) : '');
switch ($path) {
case 'controller': // 控制器
@@ -163,14 +165,15 @@ class Build
* 创建模块的欢迎页面
* @access public
* @param string $module 模块名
* @param string $namespace 应用类库命名空间
* @return void
*/
protected static function buildHello($module)
protected static function buildHello($module, $namespace)
{
$filename = APP_PATH . ($module ? $module . DS : '') . 'controller' . DS . 'Index' . (CLASS_APPEND_SUFFIX ? 'Controller' : '') . EXT;
if (!is_file($filename)) {
$content = file_get_contents(THINK_PATH . 'tpl' . DS . 'default_index.tpl');
$content = str_replace(['{$app}', '{$module}', '{layer}', '{$suffix}'], [APP_NAMESPACE, $module ? $module . '\\' : '', 'controller', CLASS_APPEND_SUFFIX ? 'Controller' : ''], $content);
$content = str_replace(['{$app}', '{$module}', '{layer}', '{$suffix}'], [$namespace, $module ? $module . '\\' : '', 'controller', CLASS_APPEND_SUFFIX ? 'Controller' : ''], $content);
if (!is_dir(dirname($filename))) {
mkdir(dirname($filename), 0777, true);
}

View File

@@ -464,6 +464,6 @@ class Loader
$array = explode('\\', $name);
$class = self::parseName(array_pop($array), 1) . (CLASS_APPEND_SUFFIX || $appendSuffix ? ucfirst($layer) : '');
$path = $array ? implode('\\', $array) . '\\' : '';
return APP_NAMESPACE . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class;
return App::$namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class;
}
}

View File

@@ -11,6 +11,7 @@
namespace think\console\command\make;
use think\App;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
@@ -39,7 +40,7 @@ class Controller extends \think\console\command\Make
// 处理命名空间
if (!empty($module)) {
$namespace = APP_NAMESPACE . "\\" . $module . "\\" . 'controller' . "\\" . $namespace;
$namespace = App::$namespace . "\\" . $module . "\\" . 'controller' . "\\" . $namespace;
}
// 处理继承
@@ -50,7 +51,7 @@ class Controller extends \think\console\command\Make
} else {
if (!preg_match("/\\\/", $extend)) {
if (!empty($module)) {
$extend = "\\" . APP_NAMESPACE . "\\" . $module . "\\" . 'controller' . "\\" . $extend;
$extend = "\\" . App::$namespace . "\\" . $module . "\\" . 'controller' . "\\" . $extend;
}
}
}

View File

@@ -15,10 +15,9 @@
return [
// 命名空间
'namespace' => [
'think' => LIB_PATH . 'think' . DS,
'behavior' => LIB_PATH . 'behavior' . DS,
'traits' => LIB_PATH . 'traits' . DS,
APP_NAMESPACE => APP_PATH,
'think' => LIB_PATH . 'think' . DS,
'behavior' => LIB_PATH . 'behavior' . DS,
'traits' => LIB_PATH . 'traits' . DS,
],
// 配置文件

View File

@@ -18,7 +18,6 @@ return [
'think' => LIB_PATH . 'think' . DS,
'behavior' => LIB_PATH . 'behavior' . DS,
'traits' => LIB_PATH . 'traits' . DS,
APP_NAMESPACE => APP_PATH,
],
// 配置文件

View File

@@ -27,7 +27,6 @@ class baseTest extends \PHPUnit_Framework_TestCase
$this->assertNotEmpty(CORE_PATH);
$this->assertNotEmpty(TRAIT_PATH);
$this->assertNotEmpty(APP_PATH);
$this->assertNotEmpty(APP_NAMESPACE);
$this->assertNotEmpty(RUNTIME_PATH);
$this->assertNotEmpty(LOG_PATH);
$this->assertNotEmpty(CACHE_PATH);