欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分析Composer實(shí)現(xiàn)自動(dòng)加載原理

 更新時(shí)間:2021年05月29日 15:14:21   作者:八重櫻  
一般在框架中都會(huì)用到composer工具, 用它來(lái)管理依賴。其中 composer有類的自動(dòng)加載機(jī)制,可以加載composer下載的庫(kù)中的所有的類文件。那么composer的自動(dòng)加載機(jī)制是怎么實(shí)現(xiàn)的呢?

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 自動(dòng)加載的重點(diǎn),首先在 autoload.php 中調(diào)用

ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273::getLoader () 方法,getLoader () 首先判斷當(dāng)前l(fā)oader是不是null,如果不為null就直接返回,否則就初始化一個(gè)ClassLoader類給賦值給 loader 是不是 null,如果不為 null 就直接返回,否則就初始化一個(gè) ClassLoader 類給賦值給 loader,接著將 autoload_namespaces.php、autoload_psr4.php、autoload_classmap.php 文件中的內(nèi)容加入到 $loader 中對(duì)應(yīng)的數(shù)組中,然后給注冊(cè) loadClass 函數(shù),將 autoload_files.php 中的所有路徑所示的文件都包含進(jìn)來(lái),當(dāng)在 new 一個(gè)類的時(shí)候如果沒(méi)有找到相關(guān)的類就會(huì)觸發(fā)這個(gè) loadClass 函數(shù),在 loadClass () 又調(diào)用了 findFile () 去查找相應(yīng)的文件,找到相應(yīng)文件后就會(huì)返回該文件,然后 loadClass 調(diào)用 includeFile () 方法將該文件 include 進(jìn)去,否則 findFile 返回 false,這樣就完成了自動(dòng)加載

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 () 函數(shù)先在 classMap 中查找,如果找不到的話就會(huì)嘗試在 apcu 緩存中查找,如果還是找不到的話就會(huì)調(diào)用 findFileWithExtension () 函數(shù)查找,如果找到了就會(huì)將該文件加到apcu緩存,如果找不到的話就會(huì)在 missingClasses 數(shù)組中設(shè)一個(gè)標(biāo)記表示識(shí)這個(gè)類找不到findFileWithExtension()方法根據(jù)之前通過(guò)loader->set(loader−>set(namespace, path)和path)和loader->setPsr4(namespace,namespace,path)方法設(shè)置的信息找出類文件的路徑信息

5. spl_autoload_register和spl_autoload_unregister函數(shù)

1. spl_autoload_register 函數(shù)

1.spl_autoload_register — 注冊(cè)給定的函數(shù)作為 __autoload 的實(shí)現(xiàn),

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

2.prepend

如果是 true,spl_autoload_register () 會(huì)添加函數(shù)到隊(duì)列之首,而不是隊(duì)列尾部。

3.如果在你的程序中已經(jīng)實(shí)現(xiàn)了 autoload () 函數(shù),它必須顯式注冊(cè)到 autoload () 隊(duì)列中。因?yàn)?spl_autoload_register () 函數(shù)會(huì)將 Zend Engine 中的__autoload () 函數(shù)取代為 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 函數(shù)

spl_autoload_unregister — 注銷已注冊(cè)的 autoload () 函數(shù),如果該函數(shù)隊(duì)列處于激活狀態(tài),并且在給定函數(shù)注銷后該隊(duì)列變?yōu)榭眨瑒t該函數(shù)隊(duì)列將會(huì)變?yōu)闊o(wú)效。如果該函數(shù)注銷后使得自動(dòng)裝載函數(shù)隊(duì)列無(wú)效,即使存在有 autoload 函數(shù)它也不會(huì)自動(dòng)激活。

bool spl_autoload_unregister ( mixed $autoload_function )

以上就是分析Composer實(shí)現(xiàn)自動(dòng)加載原理的詳細(xì)內(nèi)容,更多關(guān)于Composer實(shí)現(xiàn)自動(dòng)加載原理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • phplock(php進(jìn)程鎖) v1.0 beta1

    phplock(php進(jìn)程鎖) v1.0 beta1

    PHP在多進(jìn)程模式下(并發(fā)的web訪問(wèn))由于沒(méi)有內(nèi)置的鎖支持,在處理一些資源的之后,很容易出現(xiàn)并發(fā)性問(wèn)題。
    2009-11-11
  • Windows上php5.6操作mongodb數(shù)據(jù)庫(kù)示例【配置、連接、獲取實(shí)例】

    Windows上php5.6操作mongodb數(shù)據(jù)庫(kù)示例【配置、連接、獲取實(shí)例】

    這篇文章主要介紹了Windows上php5.6操作mongodb數(shù)據(jù)庫(kù),結(jié)合實(shí)例形式簡(jiǎn)單分析了Windows平臺(tái)上使用php5.6針對(duì)MongoDB數(shù)據(jù)庫(kù)的配置、連接、獲取實(shí)例等相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • 詳解Swoole TCP流數(shù)據(jù)邊界問(wèn)題解決方案

    詳解Swoole TCP流數(shù)據(jù)邊界問(wèn)題解決方案

    本文主要介紹了Swoole TCP流數(shù)據(jù)邊界問(wèn)題解決方案,對(duì)Swoole感興趣的同學(xué),可以參考下。
    2021-05-05
  • php+html5使用FormData對(duì)象提交表單及上傳圖片的方法

    php+html5使用FormData對(duì)象提交表單及上傳圖片的方法

    這篇文章主要介紹了php+html5使用FormData對(duì)象提交表單及上傳圖片的方法,實(shí)例分析了FormData對(duì)象的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-02-02
  • 基于PHP做個(gè)圖片防盜鏈

    基于PHP做個(gè)圖片防盜鏈

    本文主要介紹了Referer原理與圖片防盜鏈實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Referer頭信息原理與圖片防盜鏈判定、實(shí)現(xiàn)方法,并附帶一個(gè)Http請(qǐng)求封裝類,需要的朋友可以參考下
    2022-12-12
  • PHP中生成UUID自定義函數(shù)分享

    PHP中生成UUID自定義函數(shù)分享

    這篇文章主要介紹了PHP中生成UUID自定義函數(shù)分享,本文提供的代碼可以生成一個(gè) UUID version 4,,需要的朋友可以參考下
    2015-06-06
  • PHP也能干大事之PHP中的編碼解碼詳解

    PHP也能干大事之PHP中的編碼解碼詳解

    這篇文章主要介紹了PHP也能干大事之PHP中的編碼解碼詳解,本文講解了ASCII編解碼、URL編解碼、Base64編解碼、HTML實(shí)體編解碼、二進(jìn)制、八進(jìn)制、十進(jìn)制、十六進(jìn)制相互轉(zhuǎn)換等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • php中cookie與session的區(qū)別點(diǎn)總結(jié)

    php中cookie與session的區(qū)別點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于php中cookie與session的區(qū)別點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。
    2021-12-12
  • PHP安全配置

    PHP安全配置

    PHP其實(shí)不過(guò)是Web服務(wù)器的一個(gè)模塊功能,所以首先要保證Web服務(wù)器的安全。當(dāng)然Web服務(wù)器要安全又必須是先保證系統(tǒng)安全,這樣就扯遠(yuǎn)了,無(wú)窮無(wú)盡
    2006-12-12
  • Laravel?Eloquent?ORM高級(jí)部分解析

    Laravel?Eloquent?ORM高級(jí)部分解析

    這篇文章主要為大家介紹了Laravel?Eloquent?ORM高級(jí)部分解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論