re: Bootstrapping a CLI PHP application in Vanilla PHP VIEW POST

FULL DISCUSSION
 

Following this on Termux on my phone & can't get Composer to work.

So here's an autoloader for anyone who needs to do this without composer:

(Include this file into the minicli file.)

<?php
/**
 * Example project-specific auto-loading implementation.
 *
 * After registering this autoload function with SPL, the namespaces on the left of the loaders array will load classes found in the paths on the right.
 *
 * @param string $class The fully-qualified class name.
 * @return void
 */

 spl_autoload_register(function ($class) {
    $loaders = [
        'Minicli\\' => '/lib/',
        'App\\' => '/app/'
    ];

foreach($loaders as $prefix => $base_dir){
    $len = strlen($prefix);
    if(strncmp($prefix, $class,$len) !== 0){
        continue;
    }
    $relative_class = substr($class, $len);

    $file = __DIR__ . $base_dir .
       str_replace('\\', '/',
         $relative_class) .
       '.php';

    if (file_exists($file)) {
        require $file;
        return;
    }
} //end foreach
});
code of conduct - report abuse