PHP包管理器Composer实现原理( 二 )


自动加载原理下面我们通过源码分析composer是如何实现自动加载功能 。
入口<?phprequire 'vendor/autoload.php';我们通过require ‘vendor/autoload.php实现自动加载,vendor/autoloaad.php文件引用composer/autoload_real.php 。
<?php// autoload.php @generated by Composerrequire_once __DIR__ . '/composer/autoload_real.php';return ComposerAutoloaderInitb84761f57e62a6a534584b91ca213591::getLoader();autoload_realautoload_real.php是自动加载引导类,程序主要调用了引导类的静态方法getLoader() 。
<?php// autoload_real.php @generated by Composerclass ComposerAutoloaderInitb84761f57e62a6a534584b91ca213591{ private static $loader; public static function loadClassLoader($class) { if ('ComposerAutoloadClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } } public static function getLoader() { // 返回ComposerAutoloadClassLoader单例 if (null !== self::$loader) { return self::$loader; } // 调用spl_autoload_register加载ComposerAutoloadClassLoader spl_autoload_register(array('ComposerAutoloaderInitb84761f57e62a6a534584b91ca213591', 'loadClassLoader'), true, true); // 实例化ComposerAutoloadClassLoader类 self::$loader = $loader = new ComposerAutoloadClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInitb84761f57e62a6a534584b91ca213591', 'loadClassLoader')); // 静态初始化只支持 PHP5.6 以上版本并且不支持 HHVM 虚拟机 $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { // 使用 autoload_static 进行静态初始化 require_once __DIR__ . '/autoload_static.php'; call_user_func(ComposerAutoloadComposerStaticInitb84761f57e62a6a534584b91ca213591::getInitializer($loader)); } else { // 如果PHP版本低于 5.6 或者使用 HHVM 虚拟机环境,那么就要使用核心类的接口进行初始化 // PSR0 标准 $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } // PSR4 标准 $map = require __DIR__ . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); }// classmap $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } } $loader->register(true); // files if ($useStaticLoader) { $includeFiles = ComposerAutoloadComposerStaticInitb84761f57e62a6a534584b91ca213591::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } // files定义的文件,直接require就行了 foreach ($includeFiles as $fileIdentifier => $file) { composerRequireb84761f57e62a6a534584b91ca213591($fileIdentifier, $file); } return $loader; }}autoload_static<?php// autoload_static.php @generated by Composernamespace ComposerAutoload;class ComposerStaticInitb84761f57e62a6a534584b91ca213591{ public static $files = array ( '7efd69bb86214589340b40039fd363f7' => __DIR__ . '/../..' . '/Controller/User.php', ); public static $prefixLengthsPsr4 = array ( 'P' =>array ( 'Predis\' => 7, ), 'C' =>array ( 'Controller\' => 11, ), ); public static $prefixDirsPsr4 = array ( 'Predis\' =>array ( 0 => __DIR__ . '/..' . '/predis/predis/src', ), 'Controller\' =>array ( 0 => __DIR__ . '/../..' . '/src', ), ); public static function getInitializer(ClassLoader $loader) { return Closure::bind(function () use ($loader) { $loader->prefixLengthsPsr4 = ComposerStaticInitb84761f57e62a6a534584b91ca213591::$prefixLengthsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitb84761f57e62a6a534584b91ca213591::$prefixDirsPsr4; }, null, ClassLoader::class); }}静态初始化类的核心就是 getInitializer() 函数,它将自己类中的顶级命名空间映射给了 ClassLoader 类 。
PSR4 标准顶级命名空间映射用了两个数组,第一个是用命名空间第一个字母作为前缀索引,然后是 顶级命名空间,但是最终并不是文件路径,而是 顶级命名空间的长度 。为什么呢?
因为 PSR4 标准是用顶级命名空间目录替换顶级命名空间,所以获得顶级命名空间的长度很重要 。
ClassLoaderpublic function register($prepend = false){ spl_autoload_register(array($this, 'loadClass'), true, $prepend);}public function loadClass($class){ if ($file = $this->findFile($class)) { includeFile($file); return true; }}/*** Finds the path to the file where the class is defined.** @param string $class The name of the class** @return string|false The path if found, false otherwise*/public function findFile($class){ // class map lookup if (isset($this->classMap[$class])) { return $this->classMap[$class]; } if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { return false; } if (null !== $this->apcuPrefix) { $file = apcu_fetch($this->apcuPrefix.$class, $hit); if ($hit) { return $file; } } $file = $this->findFileWithExtension($class, '.php'); // Search for Hack files if we are running on HHVM if (false === $file && defined('HHVM_VERSION')) { $file = $this->findFileWithExtension($class, '.hh'); } if (null !== $this->apcuPrefix) { apcu_add($this->apcuPrefix.$class, $file); } if (false === $file) { // Remember that this class does not exist. $this->missingClasses[$class] = true; } return $file;}function includeFile($file){ include $file;}


推荐阅读