根据入口文件的代码:
require __DIR__.'/../vendor/autoload.php';
看下autoload.php
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c::getLoader();
这就是autoload.php 源码了。
看来还得看看autoload_real.php
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
//首先被动用的方法
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
//注册自动加载函数,具体的用法可以看我的其他博文
spl_autoload_register(array('ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c', 'loadClassLoader'), true, true);
//实现自动加载类 的类 主要就是这个东西实现的自动加载
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
//将之前自动加载函 删除
spl_autoload_unregister(array('ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c', 'loadClassLoader'));
//主要根据环境的不同 启用不同的自动加载方式
//PHP_VERSION_ID php的版本
//HHVM_VERSION 是Facebook 的一个为php提高性能的一个开源项目 可以自行了解
//是否使用了zend guard 的编码方式
//有人想问为什么要判断这四项是吗? 别问我 我tmd也不知道 往下看
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
//虽然是两个分支。不过最后都是给自动加载类,部署一些配置变量,为了让自动加载的时候可以找到相应的文件,把可以找到的文件,按照规则配置到变量中。
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit01f7a204137bd9325ebf450e4838e63c::getInitializer($loader));
} else {
$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);
}
}
//配置完变量了,就是那些你想用的类,然后就是注册自动加载函数了。当实例化了不存在的类 就开始找类了,具体的代码 贴在下面吧
$loader->register(true);
//注册框架必须用的一些类文件
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit01f7a204137bd9325ebf450e4838e63c::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire01f7a204137bd9325ebf450e4838e63c($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire01f7a204137bd9325ebf450e4838e63c($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
}
}
在来看看ClassLoader.php
/**
* 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
//根据之前配置的变量,在各个变量中查找
//在classmap中找
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
//在classMapAuthoritative中找
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
//在apcuPrefix中找
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
//根据psr0 和 psr4 规则查找具体的类文件的位置
$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;
}
其他部分的代码不做说明了,不是很复杂,顺着代码 都能看懂。
总结:
在这几个文件中,主要的工作就是做一个可以自动加载类的工作,让程序可以根据需要临时加载想要的文件。