溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Composer實(shí)現(xiàn)自動加載原理是什么

發(fā)布時間:2021-01-27 11:06:28 來源:億速云 閱讀:151 作者:小新 欄目:軟件技術(shù)

這篇文章主要介紹了Composer實(shí)現(xiàn)自動加載原理是什么,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

composer 自動加載原理

以在 Laravel 框架中為例:

1.首先在入口文件(/public/index.php)中引入了 autoload.php

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

2.  我們看看 autoload.php 的內(nèi)容

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

3.  我們再看看 autoload_real.php 的內(nèi)容

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

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

4. findFile () 函數(shù)先在 classMap 中查找,如果找不到的話就會嘗試在 apcu 緩存中查找,如果還是找不到的話就會調(diào)用 findFileWithExtension () 函數(shù)查找,如果找到了就會將該文件加到 apcu 緩存,如果找不到的話就會在 missingClasses 數(shù)組中設(shè)一個標(biāo)記表示識這個類找不到

findFileWithExtension()方法根據(jù)之前通過      ?>   (loader?>set(namespace,    ?)和path)和loader->setPsr4(         ,namespace,path)方法設(shè)置的信息找出類文件的路徑信息

5.  在上面有的地方用到了 spl_autoload_register 和 spl_autoload_unregister 函數(shù)

1.  spl_autoload_register 函數(shù)

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

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

2.prepend

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

3.如果在你的程序中已經(jīng)實(shí)現(xiàn)了 autoload () 函數(shù),它必須顯式注冊到 autoload () 隊列中。因為 spl_autoload_register () 函數(shù)會將 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 — 注銷已注冊的 autoload () 函數(shù),如果該函數(shù)隊列處于激活狀態(tài),并且在給定函數(shù)注銷后該隊列變?yōu)榭?,則該函數(shù)隊列將會變?yōu)闊o效。如果該函數(shù)注銷后使得自動裝載函數(shù)隊列無效,即使存在有 autoload 函數(shù)它也不會自動激活。

bool spl_autoload_unregister ( mixed $autoload_function )

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Composer實(shí)現(xiàn)自動加載原理是什么”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI