Composer机制 发表于 2019-05-07 | 分类于 PHP , Laravel 入口文件public/index.php 123456789101112131415161718<?phpdefine('LARAVEL_START', microtime(true));//引入文件-开始处理composer加载require __DIR__.'/../vendor/autoload.php';$app = require_once __DIR__.'/../bootstrap/app.php';$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);$response = $kernel->handle( $request = Illuminate\Http\Request::capture());$response->send();$kernel->terminate($request, $response); Composer 自动加载入口vendor/autoload.php 1234567<?php// autoload.php @generated by Composer// composer入口文件require_once __DIR__ . '/composer/autoload_real.php';// 返回自动加载对象return ComposerAutoloaderInitfba3f2472b9d57e801bec518de013a00::getLoader(); vendor/composer/autoload_real.php 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283<?php// autoload_real.php @generated by Composerclass ComposerAutoloaderInitfba3f2472b9d57e801bec518de013a00{ private static $loader; public static function loadClassLoader($class) { // 4.加载composer核心类 if ('Composer\Autoload\ClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } } public static function getLoader() { // 1. 如果已经实例化就立即返回 if (null !== self::$loader) { return self::$loader; } // 2. 通过spl_autoload_register将loadClassLoader注册到PHP自动加载队列 spl_autoload_register(array('ComposerAutoloaderInitfba3f2472b9d57e801bec518de013a00', 'loadClassLoader'), true, true); // 3. PHP没有载入 Composer\Autoload\ClassLoader 类,去找 PHP 自动加载队列,并执行里面的 loadClassLoader,然后载入 Composer\Autoload\ClassLoader 类,成功 new 一个对象。 self::$loader = $loader = new \Composer\Autoload\ClassLoader(); // 4. 从PHP自动加载队列中去除 spl_autoload_unregister(array('ComposerAutoloaderInitfba3f2472b9d57e801bec518de013a00', 'loadClassLoader')); // 5. 检测信息 PHP版本,是否使用HHVM_VERSION,是否有zend_loader_file_encoded方法 $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { // 6. 加载 vendor\composer\autoload_static.php 静态的自动加载映射类 require_once __DIR__ . '/autoload_static.php'; // 7.调用静态自动加载映射类的 getInitializer 方法,此方法将返回一个闭包,再通过执行此闭包,将自动加载映射规则注入到 loader 对象中 call_user_func(\Composer\Autoload\ComposerStaticInitfba3f2472b9d57e801bec518de013a00::getInitializer($loader)); } else { //如果静态加载false,则将命名空间和映射类文件加载进来 $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } $map = require __DIR__ . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } } // 8. 通过register方法,将loadClass方法加到PHP自动加载队列 $loader->register(true); // 9. 返回laravel全局函数文件的路径 if ($useStaticLoader) { $includeFiles = Composer\Autoload\ComposerStaticInitfba3f2472b9d57e801bec518de013a00::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { // 10.循环加载文件 $fileIdentifier为文件的标识码 $file为文件的路径 composerRequirefba3f2472b9d57e801bec518de013a00($fileIdentifier, $file); } return $loader; }}function composerRequirefba3f2472b9d57e801bec518de013a00($fileIdentifier, $file){ // 11.通过文件标识码判断是否已经加载过文件防止二次加载 if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; }}