From 0ddea95f80d4e39294ad358518cc26415b3fbd7a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 25 Mar 2016 22:57:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A1=86=E6=9E=B6=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Loader.php | 94 ++++++++++++++++++++++++++++++++++++++++ start.php | 57 ++++++++++++++++++++++-- think.php | 68 ----------------------------- 3 files changed, 147 insertions(+), 72 deletions(-) delete mode 100644 think.php diff --git a/library/think/Loader.php b/library/think/Loader.php index c52d3b9c..beb4b4f8 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -49,6 +49,11 @@ class Loader } else { return false; } + } elseif ($file = self::findFileInComposer($class)) { + // Composer自动加载 + // 记录加载信息 + APP_DEBUG && self::$load[] = $file; + include $file; } else { // 命名空间自动加载 if (!strpos($class, '\\')) { @@ -117,6 +122,95 @@ class Loader { // 注册系统自动加载 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; } /** diff --git a/start.php b/start.php index 9f947dfb..5ad6f069 100644 --- a/start.php +++ b/start.php @@ -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(); } diff --git a/think.php b/think.php deleted file mode 100644 index 5ad6f069..00000000 --- a/think.php +++ /dev/null @@ -1,68 +0,0 @@ - -// +---------------------------------------------------------------------- - -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(); -}