Loader類理解

王大牛發表於2022-01-14

基於tp5.1框架

<?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;

use think\exception\ClassNotFoundException;
use function Couchbase\defaultDecoder;

class Loader
{
    /**
     * 類名對映資訊
     * @var array
     */
    protected static $classMap = [];

    /**
     * 類庫別名
     * @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 $files = [];

    /**
     * Composer安裝路徑
     * @var string
     */
    private static $composerPath;

    // 獲取應用根目錄
    public static function getRootPath()
    {
        // 命令列模式
        if ('cli' == PHP_SAPI) {
            // $_SERVER['argv'][0]  => think
            $scriptName = realpath($_SERVER['argv'][0]);
        } else {
            // 瀏覽器,載入入口檔案
            // $_SERVER['SCRIPT_FILENAME'] => D:\phpstudy_pro\WWW\my\tp5.1\public\index.php
            $scriptName = $_SERVER['SCRIPT_FILENAME'];
        }

        // 獲取目錄 D:\phpstudy_pro\WWW\my\tp5.1\public
        $path = realpath(dirname($scriptName));
        /**
         * DIRECTORY_SEPARATOR 目錄分割符常量
         *
         * 不存在
         * D:\phpstudy_pro\WWW\my\tp5.1\publicD:\phpstudy_pro\WWW\my\tp5.1\public\think
         */
        if (!is_file($path . DIRECTORY_SEPARATOR . 'think')) {
            // D:\phpstudy_pro\WWW\my\tp5.1\public
            $path = dirname($path);
        }

        // D:\phpstudy_pro\WWW\my\tp5.1\ 應用根目錄
        return $path . DIRECTORY_SEPARATOR;
    }

    // 註冊自動載入機制
    public static function register($autoload = '')
    {
        // 註冊系統自動載入
        // $autoload ?: 'think\\Loader::autoload' $autoload不存在跑 'think\\Loader::autoload' 方法
        spl_autoload_register($autoload ?: 'think\\Loader::autoload', true, true);

        // 獲取根目錄 D:\phpstudy_pro\WWW\my\tp5.1\publicD:\phpstudy_pro\WWW\my\tp5.1\
        $rootPath = self::getRootPath();

        // composerPath|目錄 => D:\phpstudy_pro\WWW\my\tp5.1\publicD:\phpstudy_pro\WWW\my\tp5.1\vendor\composer\
        self::$composerPath = $rootPath . 'vendor' . DIRECTORY_SEPARATOR . 'composer' . DIRECTORY_SEPARATOR;

        // Composer自動載入支援
        if (is_dir(self::$composerPath)) { // 是目錄
            // autoload_static.php是檔案
            if (is_file(self::$composerPath . 'autoload_static.php')) {
                // 引入
                require self::$composerPath . 'autoload_static.php';
                // 返回由已定義類的名字所組成的陣列
                $declaredClass = get_declared_classes();

                // 對應的composer類composerClass => Composer\Autoload\ComposerStaticInit5fd3fd8016bff3544878c6cd28d21978
                $composerClass = array_pop($declaredClass);

                foreach (['prefixLengthsPsr4', 'prefixDirsPsr4', 'fallbackDirsPsr4', 'prefixesPsr0', 'fallbackDirsPsr0', 'classMap', 'files'] as $attr) {
                    //  檢查物件或類是否具有該屬性
                    if (property_exists($composerClass, $attr)) {
                        /*
                         * prefixLengthsPsr4
                          Array
                            (
                                [t] => Array
                                (
                                    [think\composer\] => 15
                                )
                                [a] => Array
                                (
                                    [app\] => 4
                                )
                            )...
                        */
                        self::${$attr} = $composerClass::${$attr};
                    }
                }
            } else {
                self::registerComposerLoader(self::$composerPath);
            }
        }

        // 註冊名稱空間定義
        self::addNamespace([
            // D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\think
            'think' => __DIR__,
            // D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\traits
            'traits' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'traits', __DIR__,
        ]);

        // 載入類庫對映檔案
        // D:\phpstudy_pro\WWW\my\tp5.1\runtime\classmap.php 是檔案
        if (is_file($rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'classmap.php')) {
            // 註冊 classMap  __include_file 引入 相當於 include
            /*function __include_file($file)
            {
                return include $file;
            }*/
            self::addClassMap(__include_file($rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'classmap.php'));
        }

        // 自動載入extend目錄 D:\phpstudy_pro\WWW\my\tp5.1\extend
        self::addAutoLoadDir($rootPath . 'extend');
    }

    // 自動載入 預設訪問index.php
    // 因為base.php 已經註冊錯誤和異常處理機制 Error::register();
    // $class => think\Error
    public static function autoload($class)
    {
        // self::$classAlias[$class] 定義的類庫別名 ['Test' => '\xx\xx\Test.php']
        if (isset(self::$classAlias[$class])) {
            // class_alias 為該類建立別名
            return class_alias(self::$classAlias[$class], $class);
        }
        if ($file = self::findFile($class)) {
            // Win環境嚴格區分大小寫
            // PHP_OS => WINNT   strpos(PHP_OS, 'WIN') !== false => true
            // Error => Error    pathinfo($file, PATHINFO_FILENAME) != pathinfo(realpath($file), PATHINFO_FILENAME) => false
            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)
    {
        // $class => think\Error  self::$classMap[$class]為空
        if (!empty(self::$classMap[$class])) {
            // 類庫對映
            return self::$classMap[$class];
        }

        // 查詢 PSR-4     strstr將 \ 替換成 DIRECTORY_SEPARATOR  $logicalPathPsr4 => think\Error.php
        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';

        // $first => t
        $first = $class[0];

        /*
         * self::$prefixLengthsPsr4[$first]
         * Array
            (
                [think\composer\] => 15
                [think\] => 6
                [traits\] => 7
            )...
        */
        if (isset(self::$prefixLengthsPsr4[$first])) {
            foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {
                // strpos($class, $prefix)  0 === strpos('think\Error', 'think\composer\') => true
                if (0 === strpos($class, $prefix)) {
                    /*
                     * self::$prefixDirsPsr4
                     * Array
                        (
                            [think\composer\] => Array
                            (
                                [0] => D:\phpstudy_pro\WWW\my\tp5.1\vendor\composer/../topthink/think-installer/src
                            )
                            [app\] => Array
                            (
                                [0] => D:\phpstudy_pro\WWW\my\tp5.1\vendor\composer/../../application
                            )
                            [think\] => Array
                            (
                                [0] => D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\think
                             )
                            [traits\] => Array
                            (
                                [0] => D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\traits
                            )
                            [0\] => Array
                             (
                                [0] => D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\think
                             )
                        )
                    */
                    // $prefix => $prefix
                    // self::$prefixDirsPsr4[$prefix]     D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\think
                    foreach (self::$prefixDirsPsr4[$prefix] as $dir) {
                        // D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\think\Error.php
                        if (is_file($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
                            // 返回檔案
                            return $file;
                        }
                    }
                }
            }
        }

        // 查詢 PSR-4 fallback dirs
        /*
         * self::$fallbackDirsPsr4
         * Array
            (
                [0] => D:\phpstudy_pro\WWW\my\tp5.1\extend
            )
        */
        foreach (self::$fallbackDirsPsr4 as $dir) {
            // D:\phpstudy_pro\WWW\my\tp5.1\extend\think\LoggerInterface.php
            if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
                return $file;
            }
        }
        // 查詢 PSR-0
        if (false !== $pos = strrpos($class, '\\')) {
            // namespaced class name     Psr\Log\LoggerInterface.phpthink\LoggerInterface.php
            $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::$classMap[$class] = false;
    }

    // 註冊classmap
    public static function addClassMap($class, $map = '')
    {
        if (is_array($class)) { // 是陣列合並
            self::$classMap = array_merge(self::$classMap, $class);
        } else {
            self::$classMap[$class] = $map;
        }
    }

    // 註冊名稱空間
    public static function addNamespace($namespace, $path = '')
    {
        if (is_array($namespace)) { // 陣列迴圈
            foreach ($namespace as $prefix => $paths) {
                // $prefix => think
                // rtrim($paths, DIRECTORY_SEPARATOR) => D:\phpstudy_pro\WWW\my\tp5.1\thinkphp\library\think
                self::addPsr4($prefix . '\\', rtrim($paths, DIRECTORY_SEPARATOR), true);
            }
        } 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空間
    private static function addPsr4($prefix, $paths, $prepend = false)
    {
        if (!$prefix) {
            // Register directories for the root namespace.
            if ($prepend) {
                self::$fallbackDirsPsr4 = array_merge(
                    (array)$paths,
                    self::$fallbackDirsPsr4
                );
            } else {
                self::$fallbackDirsPsr4 = array_merge(
                    self::$fallbackDirsPsr4,
                    (array)$paths
                );
            }
        } elseif (!isset(self::$prefixDirsPsr4[$prefix])) {
            // Register directories for a new namespace.
            $length = strlen($prefix);
            if ('\\' !== $prefix[$length - 1]) {
                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
            }

            self::$prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
            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);
            }
        }

        if (is_file($composerPath . 'autoload_files.php')) {
            self::$files = require $composerPath . 'autoload_files.php';
        }
    }

    // 載入composer autofile檔案
    public static function loadComposerAutoloadFiles()
    {
        foreach (self::$files as $fileIdentifier => $file) {
            if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
                __require_file($file);

                $GLOBALS['__composer_autoload_files'][$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) {
            $name = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
                return strtoupper($match[1]);
            }, $name);
            return $ucfirst ? ucfirst($name) : lcfirst($name);
        }

        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
    }

    /**
     * 建立工廠物件例項
     * @access public
     * @param string $name 工廠類名
     * @param string $namespace 預設名稱空間
     * @return mixed
     */
    public static function factory($name, $namespace = '', ...$args)
    {
        $class = false !== strpos($name, '\\') ? $name : $namespace . ucwords($name);

        if (class_exists($class)) {
            return Container::getInstance()->invokeClass($class, $args);
        } else {
            throw new ClassNotFoundException('class not exists:' . $class, $class);
        }
    }
}

/**
 * 作用範圍隔離
 *
 * @param $file
 * @return mixed
 */
function __include_file($file)
{
    return include $file;
}

function __require_file($file)
{
    return require $file;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章