Skip to content

Commit

Permalink
改进框架入口文件
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Mar 25, 2016
1 parent 004b87a commit 0ddea95
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 72 deletions.
94 changes: 94 additions & 0 deletions library/think/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static function autoload($class)
} else {
return false;
}
} elseif ($file = self::findFileInComposer($class)) {
// Composer自动加载
// 记录加载信息
APP_DEBUG && self::$load[] = $file;
include $file;
} else {
// 命名空间自动加载
if (!strpos($class, '\\')) {
Expand Down Expand Up @@ -117,6 +122,95 @@ public static function register($autoload = '')
{
// 注册系统自动加载
spl_autoload_register($autoload ? $autoload : 'think\\Loader::autoload');
// 注册composer自动加载
self::registerComposerLoader();
}

// 注册composer自动加载
private static function registerComposerLoader()
{
if (is_file(VENDOR_PATH . 'composer/autoload_namespaces.php')) {
$map = require VENDOR_PATH . 'composer/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
self::$prefixesPsr0[$namespace[0]][$namespace] = (array) $path;
}
}

if (is_file(VENDOR_PATH . 'composer/autoload_psr4.php')) {
$map = require VENDOR_PATH . 'composer/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$length = strlen($namespace);
if ('\\' !== $namespace[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
self::$prefixLengthsPsr4[$namespace[0]][$namespace] = $length;
self::$prefixDirsPsr4[$namespace] = (array) $path;
}
}

if (is_file(VENDOR_PATH . 'composer/autoload_classmap.php')) {
$classMap = require VENDOR_PATH . 'composer/autoload_classmap.php';
if ($classMap) {
self::addMap($classMap);
}
}

if (is_file(VENDOR_PATH . 'composer/autoload_files.php')) {
$includeFiles = require VENDOR_PATH . 'composer/autoload_files.php';
foreach ($includeFiles as $fileIdentifier => $file) {
self::composerRequire($fileIdentifier, $file);
}
}
}

private static function composerRequire($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
}
}

private static function findFileInComposer($class, $ext = '.php')
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DS) . $ext;

$first = $class[0];
if (isset(self::$prefixLengthsPsr4[$first])) {
foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach (self::$prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DS . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DS);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DS) . $ext;
}

if (isset(self::$prefixesPsr0[$first])) {
foreach (self::$prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DS . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// Remember that this class does not exist.
return self::$map[$class] = false;
}

/**
Expand Down
57 changes: 53 additions & 4 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,58 @@

namespace think;

// ThinkPHP 引导文件
defined('THINK_AUTOLOAD') or define('THINK_AUTOLOAD', getenv('THINK_AUTOLOAD') !== '0');
// ThinkPHP 实际引导文件
// 加载基础文件
require __DIR__ . '/base.php';
require CORE_PATH . 'Loader.php';

if (THINK_AUTOLOAD) {
require_once __DIR__ . '/think.php';
// 加载环境变量配置文件
if (is_file(ROOT_PATH . 'env' . EXT)) {
$env = include ROOT_PATH . 'env' . EXT;
foreach ($env as $key => $val) {
$name = ENV_PREFIX . $key;
putenv("$name=$val");
}
}
// 自动识别调试模式
if (!defined('APP_DEBUG')) {
$debug = getenv(ENV_PREFIX . 'APP_DEBUG');
define('APP_DEBUG', $debug);
}

// 加载模式定义文件
$mode = require MODE_PATH . APP_MODE . EXT;

// 加载模式命名空间定义
if (isset($mode['namespace'])) {
Loader::addNamespace($mode['namespace']);
}

// 注册自动加载
Loader::register();

// 加载模式别名定义
if (isset($mode['alias'])) {
Loader::addMap($mode['alias']);
}

// 注册错误和异常处理机制
Error::register();

// 加载模式配置文件
if (isset($mode['config'])) {
is_array($mode['config']) ? Config::set($mode['config']) : Config::load($mode['config']);
}

// 是否开启HOOK
defined('APP_HOOK') or define('APP_HOOK', false);

// 加载模式行为定义
if (APP_HOOK && isset($mode['tags'])) {
Hook::import($mode['tags']);
}

// 是否自动运行
if (APP_AUTO_RUN) {
App::run();
}
68 changes: 0 additions & 68 deletions think.php

This file was deleted.

0 comments on commit 0ddea95

Please sign in to comment.