国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - 分析Composer實現自動加載原理

分析Composer實現自動加載原理

2021-11-17 16:05八重櫻 PHP教程

一般在框架中都會用到composer工具, 用它來管理依賴。其中 composer有類的自動加載機制,可以加載composer下載的庫中的所有的類文件。那么composer的自動加載機制是怎么實現的呢?

1. 入口文件

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

?
1
require __DIR__.'/../vendor/autoload.php';<br>

2.  autoload.php

?
1
2
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit1215780529014c2b50a6fca7ce889273::getLoader();<br>

3. autoload_real.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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 ()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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實現自動加載原理的資料請關注服務器之家其它相關文章!

原文鏈接:https://www.cnblogs.com/a609251438/p/12659934.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久久婷婷一区二区三区不卡 | 欧美亚洲国产一区二区三区 | 日韩在线免费观看视频 | 亚洲国产精品久久 | 成人在线免费 | 国产精品免费高清 | 久久久在线免费观看 | 亚洲国产婷婷香蕉久久久久久99 | 国产精品久久久久久久久久久久冷 | 亚洲精品福利 | 欧美日韩中文 | 亚洲国产精品激情在线观看 | 欧美在线a| 狠狠久| 亚洲免费人成在线视频观看 | 亚洲综合在线播放 | 亚洲成av人影片在线观看 | 久久美 | av网站在线看 | 欧美日韩中文 | 国产欧美一区二区精品性色 | 国产在线国产 | 亚洲高清在线观看 | 久久久官网 | 黄色一级片黄色一级片 | 国产午夜在线 | 久久久久99精品 | 久久99精品视频 | 免费黄色大片 | 欧美高清在线 | 国产精品亚洲视频 | 日本久久成人 | 精品久久久久久亚洲精品 | 久久亚洲黄色 | 在线精品一区 | 成人国产精品 | 91人人爽人人爽人人精88v | 91偷拍精品一区二区三区 | 亚洲美腿 欧美 激情 另类 | 日本一区二区三区四区 | 羞羞视频在线观看 |