php laravel 源码阅读3(注册自动加载)
来源:互联网 发布:恒大票务网淘宝 编辑:程序博客网 时间:2024/06/06 21:37
根据入口文件的代码:
require __DIR__.'/../vendor/autoload.php';
看下autoload.php
<?php// autoload.php @generated by Composerrequire_once __DIR__ . '/composer/autoload_real.php';return ComposerAutoloaderInit01f7a204137bd9325ebf450e4838e63c::getLoader();
这就是autoload.php 源码了。
看来还得看看autoload_real.php
<?php// autoload_real.php @generated by Composerclass 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; }
其他部分的代码不做说明了,不是很复杂,顺着代码 都能看懂。
总结:
在这几个文件中,主要的工作就是做一个可以自动加载类的工作,让程序可以根据需要临时加载想要的文件。
阅读全文
0 0
- php laravel 源码阅读3(注册自动加载)
- laravel源码阅读之自动加载功能
- php laravel 源码阅读1
- php laravel 源码阅读2(入口文件)
- laravel(七)laravel自动生成基本注册登录功能
- php中注册自动加载方法
- Laravel 自动加载
- laravel自动加载
- Spring IOC 源码阅读资源加载和注册
- Spring IOC 源码阅读资源加载和注册
- Laravel 5.1 源码阅读笔记
- EventBus源码阅读(20)-EventBus注册
- Laravel自动加载类教程
- laravel composer自动加载函数
- php composer 在laravel中自动加载autoload_class.php中的配置
- [李景山php]每天laravel[002]-自动加载函数流程之bootstrap autoload.php
- [李景山php]每天laravel[003]-自动加载函数流程之vendor autoload.php
- [李景山php]每天laravel[004]-自动加载函数流程之composer autoload_real.php
- JavaScript编程精粹
- HDU 1051 Wooden Sticks
- jQuery Validate自定义各种验证方法
- qt 中怎样获取tableview中鼠标单击的单元格的内容
- IntelliJ Idea 2017 免费激活方法
- php laravel 源码阅读3(注册自动加载)
- Java 查找 List 中的最大值、最小值
- JQuery中bind和unbind函数
- 免费的编程中文书籍索引【必须转载、有空必看】
- [zz学习]MySQL索引背后的数据结构及算法原理
- 最近邻 相关推荐
- spring-data-jpa + SpringBoot + bootstrapTable 后端分页 模糊查询
- javascript drag()拖拽
- 一致性(连续性)hash算法(Consistent hashing)