mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 15:02:47 +08:00
改进框架入口文件
This commit is contained in:
@@ -49,6 +49,11 @@ class Loader
|
|||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} elseif ($file = self::findFileInComposer($class)) {
|
||||||
|
// Composer自动加载
|
||||||
|
// 记录加载信息
|
||||||
|
APP_DEBUG && self::$load[] = $file;
|
||||||
|
include $file;
|
||||||
} else {
|
} else {
|
||||||
// 命名空间自动加载
|
// 命名空间自动加载
|
||||||
if (!strpos($class, '\\')) {
|
if (!strpos($class, '\\')) {
|
||||||
@@ -117,6 +122,95 @@ class Loader
|
|||||||
{
|
{
|
||||||
// 注册系统自动加载
|
// 注册系统自动加载
|
||||||
spl_autoload_register($autoload ? $autoload : 'think\\Loader::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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
57
start.php
57
start.php
@@ -11,9 +11,58 @@
|
|||||||
|
|
||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
// ThinkPHP 引导文件
|
// ThinkPHP 实际引导文件
|
||||||
defined('THINK_AUTOLOAD') or define('THINK_AUTOLOAD', getenv('THINK_AUTOLOAD') !== '0');
|
// 加载基础文件
|
||||||
|
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
think.php
68
think.php
@@ -1,68 +0,0 @@
|
|||||||
<?php
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
// | Author: liu21st <liu21st@gmail.com>
|
|
||||||
// +----------------------------------------------------------------------
|
|
||||||
|
|
||||||
namespace think;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user