¿Cómo agregar la carga automática que no distingue entre mayúsculas y minúsculas usando el mapa de clase generado por el compositor?
Frecuentes
Visto 5,973 veces
7
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?
4 Respuestas
7
Añada classmap
a composer.json
.
"autoload": {
"classmap": ["folder1/", "folder2/"]
},
Entonces corre composer.phar dumpautoload
crear composer/autoload_classmap.php
.
Change your code. After
require VENDOR_PATH . '/autoload.php';
Añada
$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.
respondido 29 nov., 13:00
2
You don't - with composer.
Fix the code. I.e. use Composer to create a classmap of your classes, then make a case insensitive search for all these class names, and replace them with the correct case sensitive version from the classmap.
Or create your own case-insensitive classmap loader that automatically complains if it stumples upon a classname with incorrect cases and makes you fix the software one by one - with the danger of missing some cases that will only be detected later if code changes rearrange the order of autoloaded classes.
respondido 27 nov., 13:21
2
If your production setup supports spl_autoload_register (starting from 5.1.2) - you can add own implementation of autoload alongside with composer's. Here how mine is done (it also relies on name spacing):
autoload.php
<?php
spl_autoload_register('classLoader');
function classLoader($className, $prefix = '') {
$pathParts = explode('\\', $className);
$newPathParts = array_map('strtolower', $pathParts);
$classPath = $prefix . implode(DIRECTORY_SEPARATOR, $newPathParts) . '.php';
if ( file_exists($classPath) ) {
require_once $classPath;
}
}
?>
And in your files you can mix your case whatever you like.
$obj = new View\Home\Home();
es idéntico a:
$obj = new vIEw\home\homE();
Respondido el 02 de diciembre de 16 a las 11:12
0
My solution to the problem. An additional card (lowstr keys) and another bootloader (in the usual autoloader) are being formed. Works transparently to existing code. Used after default loader and ignore VENDOR dir.
It's best to generate the map with the -a (classmap-authoritative) option -- so that the code doesn't look for other loading options - they can lead to errors.
https://gist.github.com/AndryG/9325f36a9350f2f7ccdef002c2aa0882
Respondido el 08 de junio de 22 a las 17:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php composer-php autoload or haz tu propia pregunta.
It's nice solution to rewrite whole code at once. But I love small workable changes. - secto
Make the usage of the classnames consistent, one at a time. Composer will help you detect which name to use. - Sven
I'll do it. But legacy code could contains pitfalls: generated class names, which cannot be just renamed. - secto