说明:

 由于公司项目使用Laravel 框架  也是第一次接触此框架  作为一个新手 记录使用过程的一些事情  以及对于框架源码分析的记录  整理自己的思路 也希望对大家有帮助  如果那里不对的地方  可以留言给我

 

 

 


 

本次目的 

本次了解pubilc/index.php中的第一行代码 实现的功能

 

  1. /*
  2. |--------------------------------------------------------------------------
  3. | Register The Auto Loader
  4. |--------------------------------------------------------------------------
  5. |
  6. | Composer provides a convenient, automatically generated class loader for
  7. | our application. We just need to utilize it! We'll simply require it
  8. | into the script here so that we don't have to worry about manual
  9. | loading any of our classes later on. It feels nice to relax.
  10. |
  11. */
  12.  
  13. require __DIR__.'/../bootstrap/autoload.php';

 

 

 

 

 


 

功能大致内容

 1.记录运行时间

 2.注册实现懒加载

 3.如果有自己定义的第三方文件 bootstrap/cache/compiled.php  引入该文件  

 


bootstrap/autoload.php 代码分析

 1.记录框架运行的时间

  1. define('LARAVEL_START', microtime(true));

 

2.实现自定义方法的文件引入    该文件位于/bootstrap/cache/compiled.php  

  1. $compiledPath = __DIR__.'/cache/compiled.php';
  2. if (file_exists($compiledPath)) {
  3. require $compiledPath;
  4. }

 

3(重点部分 ) 实现类的懒加载

  1. /*
  2. |--------------------------------------------------------------------------
  3. | Register The Composer Auto Loader
  4. |--------------------------------------------------------------------------
  5. |
  6. | Composer provides a convenient, automatically generated class loader
  7. | for our application. We just need to utilize it! We'll require it
  8. | into the script here so that we do not have to worry about the
  9. | loading of any our classes "manually". Feels great to relax.
  10. |
  11. */
  12.  
  13. require __DIR__.'/../vendor/autoload.php';

  

 3.1流程图

  

  3.2 注释代码

  1. <?php
  2. use phpDocumentor\Reflection\DocBlock\Tags\Var_;
  3. use Symfony\Component\VarDumper\VarDumper;
  4. // autoload_real.php @generated by Composer
  5.  
  6. class ComposerAutoloaderInit98979ba43685ad709d46df8619e90b69
  7. {
  8. private static $loader;
  9. public static function loadClassLoader($class)
  10. {
  11. if ('Composer\Autoload\ClassLoader' === $class) {
  12. require __DIR__ . '/ClassLoader.php';
  13. }
  14. }
  15. public static function getLoader()
  16. {
  17. //单列模式 防止重复加载该类
  18. if (null !== self::$loader) {
  19. return self::$loader;
  20. }
  21. /**
  22. * @link http://php.net/manual/zh/function.spl-autoload-register.php
  23. * 注册给定的函数作为 __autoload 的实现
  24. * 使用本类中的loadClassLoader 函数实现__autoload 功能 并且放入spl_autoload_register队列之首
  25. */
  26. spl_autoload_register(array('ComposerAutoloaderInit98979ba43685ad709d46df8619e90b69', 'loadClassLoader'), true, true);
  27. /**
  28. * 由于上面注册类ComposerAutoloaderInit98979ba43685ad709d46df8619e90b69::loadClassLoader 加载方法 new的时候 调用该方法
  29. */
  30. self::$loader = $loader = new \Composer\Autoload\ClassLoader();
  31. /**
  32. * @link http://php.net/manual/zh/function.spl-autoload-unregister.php
  33. * 注销已注册的__autoload()函数
  34. * 将本类中的loadClassLoader 注册方法注销
  35. */
  36. spl_autoload_unregister(array('ComposerAutoloaderInit98979ba43685ad709d46df8619e90b69', 'loadClassLoader'));
  37. /**
  38. * @link http://php.net/manual/zh/reserved.constants.php#reserved.constants.core
  39. * PHP_VERSION_ID 作为系统参量保存
  40. * HHVM_VERSION 是否使用HHVM
  41. * @link http://files.zend.com/help/Zend-Guard-5/zend_guard_api.htm
  42. * zend_loader_file_encoded PHP文件是否加密方法存在
  43. *
  44. * 如果是ture 表示版本大于5.6 没有使用HHVM 不存在加密文件
  45. */
  46. $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
  47. //表示版本大于5.6 没有使用HHVM 不存在加密文件
  48. if ($useStaticLoader) {
  49. //引入 vendor/composer/autoload_static.php 文件
  50. require_once __DIR__ . '/autoload_static.php';
  51. /**
  52. * 该方法 理解可能存在问题 希望大家指出
  53. * 1.运行 vendor/composer/autoload_static.php 中的getInitializer 方法 返回一个匿名函数 函数的内容为
  54. * function () use ($loader) {
  55. * $loader->prefixLengthsPsr4 = ComposerStaticInit98979ba43685ad709d46df8619e90b69::$prefixLengthsPsr4;
  56. * $loader->prefixDirsPsr4 = ComposerStaticInit98979ba43685ad709d46df8619e90b69::$prefixDirsPsr4;
  57. * $loader->prefixesPsr0 = ComposerStaticInit98979ba43685ad709d46df8619e90b69::$prefixesPsr0;
  58. * $loader->classMap = ComposerStaticInit98979ba43685ad709d46df8619e90b69::$classMap;
  59. * }
  60. * 2. 调用call_user_func 方法执行第一步的匿名函数
  61. *
  62. * 说明: 步骤一传入的loadClassLoader对象 此处 说明文档 http://blog.csdn.net/mizhenxiao/article/details/51909398
  63. * 由loadClassLoader 执行秘密函数 给自身赋值
  64. * 由于传递过程中loadClassLoader 一直使用的为同一个对象 系统的对象编号没有发生编号
  65. * 所以 loadClassLoader中的prefixLengthsPsr4,prefixDirsPsr4,prefixesPsr0,classMap 替换成了vendor/composer/autoload_static.php 类中的对于的属性
  66. */
  67. call_user_func(\Composer\Autoload\ComposerStaticInit98979ba43685ad709d46df8619e90b69::getInitializer($loader));
  68. } else {
  69. //引入 vendor/composer/autoload_namespaces.php文件
  70. $map = require __DIR__ . '/autoload_namespaces.php';
  71. /**
  72. * 1.接受 vendor/composer/autoload_namespaces.php 返回的数组
  73. * 2.调用loadClassLoader 类中的set方法设置
  74. */
  75. foreach ($map as $namespace => $path) {
  76. $loader->set($namespace, $path);
  77. }
  78. /**
  79. * 1.接受 vendor/composer/autoload_psr4.php 返回的数组
  80. * 2.调用loadClassLoader 类中的setPsr4方法设置
  81. */
  82. $map = require __DIR__ . '/autoload_psr4.php';
  83. foreach ($map as $namespace => $path) {
  84. $loader->setPsr4($namespace, $path);
  85. }
  86. /**
  87. * 1.接受 vendor/composer/autoload_classmap.php 返回的数组
  88. * 2.调用loadClassLoader 类中的addClassMap方法设置
  89. */
  90. $classMap = require __DIR__ . '/autoload_classmap.php';
  91. if ($classMap) {
  92. $loader->addClassMap($classMap);
  93. }
  94. }
  95. /**
  96. * 调用loadClassLoader 的register
  97. * 注册给定的函数作为 __autoload 的实现 使用loadClassLoader类下的 loadClass方法
  98. */
  99. $loader->register(true);
  100. //表示版本大于5.6 没有使用HHVM 不存在加密文件
  101. if ($useStaticLoader) {
  102. //接受vendor/composer/autoload_static.php 下的$files 数组
  103. $includeFiles = Composer\Autoload\ComposerStaticInit98979ba43685ad709d46df8619e90b69::$files;
  104. } else {
  105. //vendor/composer/autoload_files.php 返回数组
  106. $includeFiles = require __DIR__ . '/autoload_files.php';
  107. }
  108. //全局优先加载 文件
  109. foreach ($includeFiles as $fileIdentifier => $file) {
  110. composerRequire98979ba43685ad709d46df8619e90b69($fileIdentifier, $file);
  111. }
  112. return $loader;
  113. }
  114. }
  115. function composerRequire98979ba43685ad709d46df8619e90b69($fileIdentifier, $file)
  116. {
  117. if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
  118. require $file;
  119. $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
  120. }
  121. }

 

 


 

小尾巴  

如果其中有什么问题 希望大家留意   

 

posted on 2018-02-11 11:38 Sunlight1992 阅读() 评论() 编辑 收藏

版权声明:本文为Sunlight1992原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Sunlight1992/p/8440175.html