ZendFramework2 源码分析 init_autoloader.php

时间:2023-03-09 21:16:32
ZendFramework2 源码分析 init_autoloader.php
 // Composer autoloading
if (file_exists('vendor/autoload.php')) {
// 加载自动加载器
$loader = include 'vendor/autoload.php';
} if (class_exists('Zend\Loader\AutoloaderFactory')) {
    // 如果已经加载过了直接返回
return;
} $zf2Path = false; if (is_dir('vendor/ZF2/library')) {
    // 默认的框架路径
$zf2Path = 'vendor/ZF2/library';
} elseif (getenv('ZF2_PATH')) { // Support for ZF2_PATH environment variable or git submodule     // 根据环境配置获取框架路径(大写)
$zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value     // 根据环境配置获取框架路径(小写)
$zf2Path = get_cfg_var('zf2_path');
} if ($zf2Path) {
if (isset($loader)) {
       // 在定义了自动加载器的情况下自动加载
$loader->add('Zend', $zf2Path);
$loader->add('ZendXml', $zf2Path);
} else {
       // 引入加载器
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true
)
));
}
} if (!class_exists('Zend\Loader\AutoloaderFactory')) {
// 加载器加载失败抛出异常
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}