如何使用作曲家生成的类图添加不区分大小写的自动加载?

2022-01-21 00:00:00 php composer-php autoload

我有一个遗留项目,在不同情况下有声明和类使用.

I have legacy project which has declartions and class usings in different cases.

我想将源升级到现代状态.首先,我想用作曲家自动加载替换传统的自动加载.但是 composer 不提供不区分大小写的自动加载.

I want upgrade source to modern state. First I want to replace legacy autoloading by composer autoloading. But composer does not provide case insensitive autoloading.

如何使用composer classmap和insensitive autoload?

How to use composer classmap and insensitive autoload?

推荐答案

classmap 添加到 composer.json.

"autoload": {
    "classmap": ["folder1/", "folder2/"]
},

然后运行composer.phar dumpautoload来创建composer/autoload_classmap.php.

更改您的代码.之后

require VENDOR_PATH . '/autoload.php';

添加

$class_map = require VENDOR_PATH . '/composer/autoload_classmap.php';
$new_class_map = array();
foreach ($class_map as $class => $file)
    $new_class_map [strtolower($class)] = $file;
unset($class_map);
spl_autoload_register(function ($class)use($new_class_map)
        {
        $class = strtolower($class);
        if (isset($new_class_map[$class]))
            {
            require_once $new_class_map[$class];
            return true;
            }
        else
            return false;
        }, true, false);
unset($new_class_map);

这是我找到的最简单的方法.

This is the simplest way I have found.

相关文章