429 lines
14 KiB
PHP
429 lines
14 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// +----------------------------------------------------------------------
|
|
// | Author: liu21st <liu21st@gmail.com>
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace think;
|
|
|
|
class Loader
|
|
{
|
|
/**
|
|
* 类名映射信息
|
|
* @var array
|
|
*/
|
|
protected static $map = [];
|
|
|
|
/**
|
|
* 类库别名
|
|
* @var array
|
|
*/
|
|
protected static $classAlias = [];
|
|
|
|
/**
|
|
* PSR-4
|
|
* @var array
|
|
*/
|
|
private static $prefixLengthsPsr4 = [];
|
|
private static $prefixDirsPsr4 = [];
|
|
private static $fallbackDirsPsr4 = [];
|
|
|
|
/**
|
|
* PSR-0
|
|
* @var array
|
|
*/
|
|
private static $prefixesPsr0 = [];
|
|
private static $fallbackDirsPsr0 = [];
|
|
|
|
/**
|
|
* 自动加载的文件列表
|
|
* @var array
|
|
*/
|
|
private static $autoloadFiles = [];
|
|
|
|
/**
|
|
* Composer安装路径
|
|
* @var string
|
|
*/
|
|
private static $composerPath;
|
|
|
|
public static function dinfo(){
|
|
var_dump('---------------map 类名映射信息-------------------');
|
|
var_dump(self::$map);
|
|
var_dump('---------------classAlias 类库别名-------------------');
|
|
var_dump(self::$classAlias);
|
|
var_dump('---------------prefixLengthsPsr4-------------------');
|
|
var_dump(self::$prefixLengthsPsr4);
|
|
var_dump('---------------prefixDirsPsr4-------------------');
|
|
var_dump(self::$prefixDirsPsr4);
|
|
var_dump('---------------fallbackDirsPsr4-------------------');
|
|
var_dump(self::$fallbackDirsPsr4);
|
|
var_dump('---------------prefixesPsr0-------------------');
|
|
var_dump(self::$prefixesPsr0);
|
|
var_dump('---------------fallbackDirsPsr0-------------------');
|
|
var_dump(self::$fallbackDirsPsr0);
|
|
var_dump('---------------autoloadFiles 自动加载的文件列表-------------------');
|
|
var_dump(self::$autoloadFiles);
|
|
var_dump('---------------composerPath-------------------');
|
|
var_dump(self::$composerPath);
|
|
}
|
|
|
|
// 注册自动加载机制
|
|
public static function register($autoload = '')
|
|
{
|
|
// 注册系统自动加载
|
|
spl_autoload_register($autoload ?: 'think\\Loader::autoload', true, true);
|
|
|
|
// 注册命名空间定义
|
|
self::addNamespace([
|
|
//核心目录
|
|
'think' => __DIR__ . '/',
|
|
//复用目录
|
|
'traits' => __DIR__ . '/../traits/',
|
|
]);
|
|
|
|
// 获取程序根目录
|
|
// $_SERVER['SCRIPT_FILENAME'] 当前执行脚本的绝对路径,并非当前文件的绝对路径
|
|
$path = dirname($_SERVER['SCRIPT_FILENAME']);
|
|
// is_file() 函数检查指定的文件名是否是正常的文件
|
|
// realpath() 函数返回绝对路径。
|
|
if (is_file('./think')) {
|
|
$rootPath = realpath($path) . '/';
|
|
} else {
|
|
$rootPath = realpath($path . '/../') . '/';
|
|
}
|
|
// $rootPath 程序所在根目录
|
|
|
|
// 加载类库映射文件
|
|
if (is_file($rootPath . 'runtime/classmap.php')) {
|
|
self::addClassMap(__include_file($rootPath . 'runtime/classmap.php'));
|
|
}
|
|
|
|
// Composer安装路径
|
|
self::$composerPath = $rootPath . 'vendor/composer/';
|
|
|
|
// Composer自动加载支持
|
|
if (is_dir(self::$composerPath)) {
|
|
self::registerComposerLoader(self::$composerPath);
|
|
}
|
|
|
|
// 自动加载extend目录
|
|
self::addAutoLoadDir($rootPath . 'extend');
|
|
}
|
|
|
|
// 自动加载
|
|
public static function autoload($class)
|
|
{
|
|
if (isset(self::$classAlias[$class])) {
|
|
return class_alias(self::$classAlias[$class], $class);
|
|
}
|
|
|
|
if ($file = self::findFile($class)) {
|
|
|
|
// Win环境严格区分大小写
|
|
if (strpos(PHP_OS, 'WIN') !== false && pathinfo($file, PATHINFO_FILENAME) != pathinfo(realpath($file), PATHINFO_FILENAME)) {
|
|
return false;
|
|
}
|
|
|
|
__include_file($file);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查找文件
|
|
* @access private
|
|
* @param string $class
|
|
* @return string|false
|
|
*/
|
|
private static function findFile($class)
|
|
{
|
|
if (!empty(self::$map[$class])) {
|
|
// 类库映射
|
|
return self::$map[$class];
|
|
}
|
|
|
|
// 查找 PSR-4
|
|
// 将$class 字符串中的字符 内的\\ 替换成系统默认的文件目录,再拼接上文件后缀
|
|
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';
|
|
// var_dump($logicalPathPsr4);
|
|
$first = $class[0];
|
|
// var_dump($first);
|
|
if (isset(self::$prefixLengthsPsr4[$first])) {
|
|
foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {
|
|
// strpos() 字符串中第一次出现的位置
|
|
if (0 === strpos($class, $prefix)) {
|
|
foreach (self::$prefixDirsPsr4[$prefix] as $dir) {
|
|
|
|
// var_dump(substr($logicalPathPsr4, $length));
|
|
|
|
// substr() 从字符串中返回
|
|
if (is_file($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
|
|
// echo $file.'<br />';
|
|
return $file;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 查找 PSR-4 fallback dirs
|
|
foreach (self::$fallbackDirsPsr4 as $dir) {
|
|
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
|
|
echo $file.'---------------<br />';
|
|
return $file;
|
|
}
|
|
}
|
|
|
|
// // 查找 PSR-0
|
|
// if (false !== $pos = strrpos($class, '\\')) {
|
|
// // namespaced class name
|
|
// $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
|
|
// . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
|
|
// } else {
|
|
// // PEAR-like class name
|
|
// $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php';
|
|
// }
|
|
//
|
|
// if (isset(self::$prefixesPsr0[$first])) {
|
|
// foreach (self::$prefixesPsr0[$first] as $prefix => $dirs) {
|
|
// if (0 === strpos($class, $prefix)) {
|
|
// foreach ($dirs as $dir) {
|
|
// if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
|
|
// return $file;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// // 查找 PSR-0 fallback dirs
|
|
// foreach (self::$fallbackDirsPsr0 as $dir) {
|
|
// if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
|
|
// return $file;
|
|
// }
|
|
// }
|
|
//
|
|
// return self::$map[$class] = false;
|
|
}
|
|
|
|
// 注册classmap
|
|
public static function addClassMap($class, $map = '')
|
|
{
|
|
if (is_array($class)) {
|
|
self::$map = array_merge(self::$map, $class);
|
|
} else {
|
|
self::$map[$class] = $map;
|
|
}
|
|
}
|
|
|
|
// 注册命名空间
|
|
public static function addNamespace($namespace, $path = '')
|
|
{
|
|
// 检测是否为数组
|
|
if (is_array($namespace)) {
|
|
foreach ($namespace as $prefix => $paths) {
|
|
self::addPsr4($prefix . '\\', rtrim($paths, DIRECTORY_SEPARATOR), true);
|
|
// 会增加一次IF判断
|
|
// self::addNamespace($prefix, $paths);
|
|
}
|
|
} else {
|
|
// 非数组则执行这个
|
|
self::addPsr4($namespace . '\\', rtrim($path, DIRECTORY_SEPARATOR), true);
|
|
}
|
|
}
|
|
|
|
// 添加Ps0空间
|
|
private static function addPsr0($prefix, $paths, $prepend = false)
|
|
{
|
|
if (!$prefix) {
|
|
if ($prepend) {
|
|
self::$fallbackDirsPsr0 = array_merge(
|
|
(array) $paths,
|
|
self::$fallbackDirsPsr0
|
|
);
|
|
} else {
|
|
self::$fallbackDirsPsr0 = array_merge(
|
|
self::$fallbackDirsPsr0,
|
|
(array) $paths
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$first = $prefix[0];
|
|
if (!isset(self::$prefixesPsr0[$first][$prefix])) {
|
|
self::$prefixesPsr0[$first][$prefix] = (array) $paths;
|
|
|
|
return;
|
|
}
|
|
|
|
if ($prepend) {
|
|
self::$prefixesPsr0[$first][$prefix] = array_merge(
|
|
(array) $paths,
|
|
self::$prefixesPsr0[$first][$prefix]
|
|
);
|
|
} else {
|
|
self::$prefixesPsr0[$first][$prefix] = array_merge(
|
|
self::$prefixesPsr0[$first][$prefix],
|
|
(array) $paths
|
|
);
|
|
}
|
|
}
|
|
|
|
// 添加Psr4空间
|
|
// $prefix 别名
|
|
// $paths 路径
|
|
private static function addPsr4($prefix, $paths, $prepend = false)
|
|
{
|
|
if (!$prefix) {
|
|
// 无值 则执行
|
|
// 为根命名空间注册目录。 Register directories for the root namespace.
|
|
// array_merge() 数组元素有相同的键名,则最后的元素会覆盖其他元素
|
|
if ($prepend) {
|
|
// 为真执行,数组合并
|
|
self::$fallbackDirsPsr4 = array_merge(
|
|
(array) $paths,
|
|
self::$fallbackDirsPsr4
|
|
);
|
|
} else {
|
|
self::$fallbackDirsPsr4 = array_merge(
|
|
self::$fallbackDirsPsr4,
|
|
(array) $paths
|
|
);
|
|
}
|
|
} elseif (!isset(self::$prefixDirsPsr4[$prefix])) {
|
|
// isset() 检测变量是否已设置 存在并且值不是 NULL 则返回 TRUE
|
|
// self::$prefixDirsPsr4[$prefix] 为空执行
|
|
// 为新名称空间注册目录。 Register directories for a new namespace.
|
|
// 计算字符长度
|
|
$length = strlen($prefix);
|
|
if ('\\' !== $prefix[$length - 1]) {
|
|
// 判断最后以为是否为\ 符号,若不是则报错
|
|
// 在非空的PSR - 4字头端必须用命名空间separator
|
|
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
|
|
}
|
|
// 取$prefix 第一个字符做键值,$prefix 最后将$prefix的长度赋值
|
|
self::$prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
|
|
// $prefix 作为键值,将 $paths 转成数组做值
|
|
self::$prefixDirsPsr4[$prefix] = (array) $paths;
|
|
} elseif ($prepend) {
|
|
// 以上条件都不满足条件,判断为真 则执行
|
|
// 在目录已经注册的命名空间。 Prepend directories for an already registered namespace.
|
|
self::$prefixDirsPsr4[$prefix] = array_merge(
|
|
(array) $paths,
|
|
self::$prefixDirsPsr4[$prefix]
|
|
);
|
|
} else {
|
|
// 为已注册的命名空间追加目录。 Append directories for an already registered namespace.
|
|
self::$prefixDirsPsr4[$prefix] = array_merge(
|
|
self::$prefixDirsPsr4[$prefix],
|
|
(array) $paths
|
|
);
|
|
}
|
|
}
|
|
|
|
// 注册自动加载类库目录
|
|
public static function addAutoLoadDir($path)
|
|
{
|
|
self::$fallbackDirsPsr4[] = $path;
|
|
}
|
|
|
|
// 注册类别名
|
|
public static function addClassAlias($alias, $class = null)
|
|
{
|
|
if (is_array($alias)) {
|
|
self::$classAlias = array_merge(self::$classAlias, $alias);
|
|
} else {
|
|
self::$classAlias[$alias] = $class;
|
|
}
|
|
}
|
|
|
|
// 注册composer自动加载
|
|
public static function registerComposerLoader($composerPath)
|
|
{
|
|
if (is_file($composerPath . 'autoload_namespaces.php')) {
|
|
$map = require $composerPath . 'autoload_namespaces.php';
|
|
foreach ($map as $namespace => $path) {
|
|
self::addPsr0($namespace, $path);
|
|
}
|
|
}
|
|
|
|
if (is_file($composerPath . 'autoload_psr4.php')) {
|
|
$map = require $composerPath . 'autoload_psr4.php';
|
|
foreach ($map as $namespace => $path) {
|
|
self::addPsr4($namespace, $path);
|
|
}
|
|
}
|
|
|
|
if (is_file($composerPath . 'autoload_classmap.php')) {
|
|
$classMap = require $composerPath . 'autoload_classmap.php';
|
|
if ($classMap) {
|
|
self::addClassMap($classMap);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 加载composer autofile文件
|
|
public static function loadComposerAutoloadFiles()
|
|
{
|
|
if (is_file(self::$composerPath . 'autoload_files.php')) {
|
|
$includeFiles = require self::$composerPath . 'autoload_files.php';
|
|
foreach ($includeFiles as $fileIdentifier => $file) {
|
|
if (empty(self::$autoloadFiles[$fileIdentifier])) {
|
|
__require_file($file);
|
|
self::$autoloadFiles[$fileIdentifier] = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 字符串命名风格转换
|
|
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
|
|
* @access public
|
|
* @param string $name 字符串
|
|
* @param integer $type 转换类型
|
|
* @param bool $ucfirst 首字母是否大写(驼峰规则)
|
|
* @return string
|
|
*/
|
|
public static function parseName($name, $type = 0, $ucfirst = true)
|
|
{
|
|
if ($type) {
|
|
//strtoupper 把所有字符转换为大写
|
|
$name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
|
|
return strtoupper($match[1]);
|
|
}, $name);
|
|
// ucfirst() 首字符转换为大写
|
|
// lcfirst() 首字符转换为小写
|
|
return $ucfirst ? ucfirst($name) : lcfirst($name);
|
|
} else {
|
|
// strtolower 所有字符转成小写
|
|
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 作用范围隔离
|
|
*
|
|
* @param $file
|
|
* @return mixed
|
|
*/
|
|
function __include_file($file)
|
|
{
|
|
return include $file;
|
|
}
|
|
|
|
function __require_file($file)
|
|
{
|
|
return require $file;
|
|
}
|