Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

php - How to add case insensitive autoloading using composer generated classmap?

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

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.

How to use composer classmap and insensitive autoload?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add classmap to composer.json.

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

Then run composer.phar dumpautoload to create composer/autoload_classmap.php.

Change your code. After

require VENDOR_PATH . '/autoload.php';

Add

$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...