广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >分析Composer实现自动加载原理
  • 676
分享到

分析Composer实现自动加载原理

2024-04-02 19:04:59 676人浏览 泡泡鱼
摘要

目录1. 入口文件2.  autoload.PHP3. autoload_real.php5. spl_autoload_reGISter和spl_autoload_unr

1. 入口文件

(/public/index.php)中引入了 autoload.php


require __DIR__.'/../vendor/autoload.php';

2.  autoload.php


require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273::getLoader();

3. autoload_real.php


<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273{
    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('ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
        spl_autoload_unregister(array('ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273', 'loadClassLoader'));
        $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\ComposerStaticInit1215780529014c2b50a6fca7ce889273::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\ComposerStaticInit1215780529014c2b50a6fca7ce889273::$files;
        } else {
            $includeFiles = require __DIR__ . '/autoload_files.php';
        }
        foreach ($includeFiles as $fileIdentifier => $file) {
            composerRequire1215780529014c2b50a6fca7ce889273($fileIdentifier, $file);
        }
 
        return $loader;
    }
}
function composerRequire1215780529014c2b50a6fca7ce889273($fileIdentifier, $file){
    if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
        require $file;
 
        $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
    }
}
 

可以看出这一段是 composer 自动加载的重点,首先在 autoload.php 中调用

ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273::getLoader () 方法,getLoader () 首先判断当前loader是不是null,如果不为null就直接返回,否则就初始化一个ClassLoader类给赋值给 loader 是不是 null,如果不为 null 就直接返回,否则就初始化一个 ClassLoader 类给赋值给 loader,接着将 autoload_namespaces.php、autoload_psr4.php、autoload_classmap.php 文件中的内容加入到 $loader 中对应的数组中,然后给注册 loadClass 函数,将 autoload_files.php 中的所有路径所示的文件都包含进来,当在 new 一个类的时候如果没有找到相关的类就会触发这个 loadClass 函数,在 loadClass () 又调用了 findFile () 去查找相应的文件,找到相应文件后就会返回该文件,然后 loadClass 调用 includeFile () 方法将该文件 include 进去,否则 findFile 返回 false,这样就完成了自动加载

4.  findFile ()


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;
}

private function findFileWithExtension($class, $ext){
    // PSR-4 lookup
    $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;

    $first = $class[0];
    if (isset($this->prefixLengthsPsr4[$first])) {
        $subPath = $class;
        while (false !== $lastPos = strrpos($subPath, '\\')) {
            $subPath = substr($subPath, 0, $lastPos);
            $search = $subPath.'\\';
            if (isset($this->prefixDirsPsr4[$search])) {
                $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
                foreach ($this->prefixDirsPsr4[$search] as $dir) {
                    if (file_exists($file = $dir . $pathEnd)) {
                        return $file;
                    }
                }
            }
        }
    }
    // PSR-4 fallback dirs
    foreach ($this->fallbackDirsPsr4 as $dir) {
        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
            return $file;
        }
    }

    // PSR-0 lookup
    if (false !== $pos = strrpos($class, '\\')) {
        // namespaced class name
        $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
        . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
    } else {
        // PEAR-like class name
        $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
    }

    if (isset($this->prefixesPsr0[$first])) {
        foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
            if (0 === strpos($class, $prefix)) {
                foreach ($dirs as $dir) {
                    if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
                        return $file;
                    }
                }
            }
        }
    }

    // PSR-0 fallback dirs
    foreach ($this->fallbackDirsPsr0 as $dir) {
        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
            return $file;
        }
    }
    // PSR-0 include paths.
    if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
        return $file;
    }
    return false;
}

findFile () 函数先在 classMap 中查找,如果找不到的话就会尝试在 apcu 缓存中查找,如果还是找不到的话就会调用 findFileWithExtension () 函数查找,如果找到了就会将该文件加到apcu缓存,如果找不到的话就会在 missingClasses 数组中设一个标记表示识这个类找不到findFileWithExtension()方法根据之前通过loader->set(loader−>set(namespace, path)和path)和loader->setPsr4(namespace,namespace,path)方法设置的信息找出类文件的路径信息

5. spl_autoload_register和spl_autoload_unregister函数

1. spl_autoload_register 函数

1.spl_autoload_register — 注册给定的函数作为 __autoload 的实现,

bool spl_autoload_register([callable autoloadfunction[,boolautoloadfunction[,boolthrow = true [, bool $prepend = false ]]])

2.prepend

如果是 true,spl_autoload_register () 会添加函数到队列之首,而不是队列尾部。

3.如果在你的程序中已经实现了 autoload () 函数,它必须显式注册到 autoload () 队列中。因为 spl_autoload_register () 函数会将 Zend Engine 中的__autoload () 函数取代为 spl_autoload () 或 spl_autoload_call ()

例:

function __autoload($name) { require 'class/'.$name.'.php'; echo '1'; } function autoload_test($name) { echo '2'; } spl_autoload_register('autoload_test'); spl_autoload_register('__autoload'); $ca=new Ca();

2. spl_autoload_unregister 函数

spl_autoload_unregister — 注销已注册的 autoload () 函数,如果该函数队列处于激活状态,并且在给定函数注销后该队列变为空,则该函数队列将会变为无效。如果该函数注销后使得自动装载函数队列无效,即使存在有 autoload 函数它也不会自动激活。

bool spl_autoload_unregister ( mixed $autoload_function )

以上就是分析Composer实现自动加载原理的详细内容,更多关于Composer实现自动加载原理的资料请关注编程网其它相关文章!

--结束END--

本文标题: 分析Composer实现自动加载原理

本文链接: https://www.lsjlt.com/news/127141.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 分析Composer实现自动加载原理
    目录1. 入口文件2.  autoload.php3. autoload_real.php5. spl_autoload_register和spl_autoload_unr...
    99+
    2022-11-12
  • composer中自动加载原理的示例分析
    这篇文章主要介绍了composer中自动加载原理的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。深入解析 composer 的自动加载原理前言PHP 自5.3的版本之...
    99+
    2023-06-14
  • Composer如何实现自动加载原理
    小编给大家分享一下Composer如何实现自动加载原理,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!1. 入口文件(/public/index.php)中引入了 autoload.phprequire __DIR...
    99+
    2023-06-15
  • php实现自动加载的示例分析
    这篇“php实现自动加载的示例分析”文章,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要参考一下,对于“php实现自动加载的示例分析”,小编整理了以下知识点,请大家跟着小编的步伐一步一步的慢慢理解,接下来就让我们进入...
    99+
    2023-06-06
  • Springboot自动加载配置的原理解析
    目录1、springboot自动配置的原理初探2. 补充扩展(解释为什么引用的包都报红错了,项目还能启动)3、又一个问题总结1、springboot自动配置的原理初探 以下注解都在...
    99+
    2022-11-12
  • spring boot启动加载数据原理分析
    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。创建实现接口 CommandLine...
    99+
    2023-05-31
    spring boot 启动
  • SpringBoot详细分析自动装配原理并实现starter
    目录约定优于配置自动装配手写一个starter组件约定优于配置 SpringBoot的预定优于配置主要体现在以下几个方面: maven的目录结构: 配置文件默认存放在resource...
    99+
    2022-11-13
  • TensorFlow的自动求导原理分析
    原理: TensorFlow使用的求导方法称为自动微分(Automatic Differentiation),它既不是符号求导也不是数值求导,而类似于将两者结合的产物。 最基本的原理...
    99+
    2022-11-12
  • SpringBoot自动配置的原理分析
    这篇文章主要介绍了SpringBoot自动配置的原理分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot自动配置的原理分析文章都会有所收获,下面我们一起来看看吧。初始化一个Springboot...
    99+
    2023-06-08
  • PHP实现自动加载机制
    php自动加载 php自动载方法有两种. 第一种方案用__autoload,这个函数较简单,也较弱. 但有一问题没有解决, 就是在include前判断文件是否存在的问题. set...
    99+
    2022-11-12
  • php怎么实现自动加载
    在PHP中,可以使用spl_autoload_register()函数实现自动加载。这个函数用于注册一个自定义的自动加载函数,当使用...
    99+
    2023-09-06
    php
  • SpringBoot自动装配原理以及分析
    目录先看看SpringBoot的主配置类先看看@SpringBootConfiguration注解再进去看看@Configuration先看看@AutoConfigurationPa...
    99+
    2022-11-21
    SpringBoot自动装配原理 SpringBoot自动装配 自动装配原理
  • Gochannel实现原理分析
    目录channelchannel类型创建channelchannel操作发送接收关闭无缓冲的通道有缓冲的通道close()如何优雅的从通道循环取值单向通道通道遍历异步通道通道总结ch...
    99+
    2023-05-14
    Go channel Go channel实现原理
  • SpringBoot源码分析之bootstrap.properties文件加载的原理
    目录1.bootstrap的使用2.bootstrap加载原理分析2.1 BootstrapApplicationListener2.2 启动流程梳理2.3 bootstrap.pr...
    99+
    2022-11-12
  • Android实现ListView分页自动加载数据的方法
    Android应用开发中,采用ListView组件来展示数据是很常用的功能,当一个应用要展现很多的数据时,一般情况下都不会把所有的数据一次就展示出来,而是通过分页的形式来展示数...
    99+
    2022-06-06
    自动 方法 数据 listview Android
  • SpringBoot自动配置原理的示例分析
    这篇文章主要介绍了SpringBoot自动配置原理的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、SpringBoot是什么SpringBoot 的诞生就是为了简...
    99+
    2023-06-22
  • springBoot自动注入原理的示例分析
    这篇文章给大家分享的是有关springBoot自动注入原理的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。@SpringBootApplication注解解读为什么我们的启动类上标注一个@SpringBo...
    99+
    2023-06-29
  • SpringBoot自动装配的原理详解分析
    目录前言自动装配案例自动装配分析自动装配总结前言 关于 ​​SpringBoot​​​ 的自动装配功能,相信是每一个 ​​Java​​ 程序员...
    99+
    2022-11-13
    SpringBoot 自动装配原理 SpringBoot 自动装配
  • JavaScript懒加载与预加载原理与实现详解
    目录1、懒加载1.1、什么是懒加载1.2、为什么要使用懒加载1.3、懒加载的优点1.4、懒加载的原理1.5、懒加载的实现步骤1.6、懒加载的实现方式2、预加载2.1、什么是预加载2....
    99+
    2022-11-13
  • react 实现图片正在加载中 加载完成 加载失败三个阶段的原理解析
    最近博客写道项目列表中,发现这里比较多图片,一开加载会比较慢,然后就想要用一个loading的图片来占位。与此同时,如果图片加载失败那么显示错误的图片,不显示一个原有的错误,那样比较...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作