改进Loader类的register方法 alias.php 更改为 classmap.php addMap方法更改为 addClassMap

base.php和start.php文件调整
This commit is contained in:
thinkphp
2016-06-22 14:11:23 +08:00
parent ee171fa963
commit 5ffb2359cc
7 changed files with 45 additions and 48 deletions

View File

@@ -37,3 +37,27 @@ defined('AUTO_SCAN_PACKAGE') or define('AUTO_SCAN_PACKAGE', false); // 是否自
// 环境常量
define('IS_CLI', PHP_SAPI == 'cli' ? true : false);
define('IS_WIN', strstr(PHP_OS, 'WIN') ? true : false);
// 载入Loader类
require CORE_PATH . 'Loader.php';
// 加载环境变量配置文件
if (is_file(ROOT_PATH . 'env' . EXT)) {
$env = include ROOT_PATH . 'env' . EXT;
foreach ($env as $key => $val) {
$name = ENV_PREFIX . $key;
if (is_bool($val)) {
$val = $val ? 1 : 0;
}
putenv("$name=$val");
}
}
// 注册自动加载
\think\Loader::register();
// 注册错误和异常处理机制
\think\Error::register();
// 加载模式配置文件
\think\Config::set(include THINK_PATH . 'convention' . EXT);

View File

@@ -399,7 +399,7 @@ class App
// 加载别名文件
if (is_file(CONF_PATH . $module . 'alias' . EXT)) {
Loader::addMap(include CONF_PATH . $module . 'alias' . EXT);
Loader::addClassMap(include CONF_PATH . $module . 'alias' . EXT);
}
// 加载行为扩展文件

View File

@@ -91,7 +91,7 @@ class Loader
}
// 注册classmap
public static function addMap($class, $map = '')
public static function addClassMap($class, $map = '')
{
if (is_array($class)) {
self::$map = array_merge(self::$map, $class);
@@ -125,7 +125,16 @@ class Loader
{
// 注册系统自动加载
spl_autoload_register($autoload ?: 'think\\Loader::autoload');
// 注册命名空间定义
self::addNamespace([
'think' => LIB_PATH . 'think' . DS,
'behavior' => LIB_PATH . 'behavior' . DS,
'traits' => LIB_PATH . 'traits' . DS,
]);
// 加载类库映射文件
self::addClassMap(include THINK_PATH . 'classmap' . EXT);
// Composer自动加载支持
if (is_dir(VENDOR_PATH . 'composer')) {
// 注册Composer自动加载
self::registerComposerLoader();
@@ -134,7 +143,7 @@ class Loader
// 读取Composer自动加载文件
$autoload = include VENDOR_PATH . 'think_autoload.php';
if (is_array($autoload)) {
self::addMap($autoload);
self::addClassMap($autoload);
}
} elseif (is_file(RUNTIME_PATH . 'autoload_composer.php')) {
$autoload = include RUNTIME_PATH . 'autoload_composer.php';
@@ -161,7 +170,7 @@ class Loader
$content = "<?php " . PHP_EOL;
if (!empty(self::$load)) {
foreach (self::$load as $file) {
$content .= 'include ' . $file . ';' . PHP_EOL;
$content .= 'include \'' . $file . '\';' . PHP_EOL;
}
}
@@ -170,7 +179,6 @@ class Loader
}
// 生成缓存
file_put_contents(RUNTIME_PATH . 'autoload_composer.php', $content);
}
// 解析Composer Package
@@ -227,7 +235,7 @@ class Loader
if (is_file(VENDOR_PATH . 'composer/autoload_classmap.php')) {
$classMap = require VENDOR_PATH . 'composer/autoload_classmap.php';
if ($classMap) {
self::addMap($classMap);
self::addClassMap($classMap);
}
}

View File

@@ -11,43 +11,8 @@
namespace think;
// ThinkPHP 实际引导文件
// ThinkPHP 引导文件
// 加载基础文件
require __DIR__ . '/base.php';
require CORE_PATH . 'Loader.php';
// 加载环境变量配置文件
if (is_file(ROOT_PATH . 'env' . EXT)) {
$env = include ROOT_PATH . 'env' . EXT;
foreach ($env as $key => $val) {
$name = ENV_PREFIX . $key;
if (is_bool($val)) {
$val = $val ? 1 : 0;
}
putenv("$name=$val");
}
}
// 注册命名空间定义
Loader::addNamespace([
'think' => LIB_PATH . 'think' . DS,
'behavior' => LIB_PATH . 'behavior' . DS,
'traits' => LIB_PATH . 'traits' . DS,
]);
// 注册自动加载
Loader::register();
// 加载别名定义
Loader::addMap(include THINK_PATH . 'alias' . EXT);
// 注册错误和异常处理机制
Error::register();
// 加载模式配置文件
Config::set(include THINK_PATH . 'convention' . EXT);
// 是否自动运行
if (APP_AUTO_RUN) {
App::run()->send();
}
// 执行应用
App::run()->send();

View File

@@ -48,7 +48,6 @@ class appTest extends \PHPUnit_Framework_TestCase
{
public function testRun()
{
Config::set('root_namespace', ['/path/']);
App::run(Request::create("http://www.example.com"))->send();
$expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
@@ -57,7 +56,8 @@ class appTest extends \PHPUnit_Framework_TestCase
$rc = new ReflectionClass('\think\Loader');
$ns = $rc->getProperty('namespace');
$ns->setAccessible(true);
$this->assertEquals(true, in_array('/path/', $ns->getValue()));
$namespace = $ns->getValue();
$this->assertEquals(TEST_PATH, $namespace['tests']);
$this->assertEquals(true, function_exists('lang'));
$this->assertEquals(true, function_exists('config'));

View File

@@ -28,9 +28,9 @@ class loaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(false, Loader::autoload('my\HelloTest'));
}
public function testAddMap()
public function testAddClassMap()
{
Loader::addMap('my\hello\Test', __DIR__ . DS . 'loader' . DS . 'Test.php');
Loader::addClassMap('my\hello\Test', __DIR__ . DS . 'loader' . DS . 'Test.php');
}
public function testAddNamespace()