Level-2 / Axel

A modular, extensible, fast, ultra-lightweight PHP autoloader with inbuilt caching
15 stars 5 forks source link

Allow autoload.json in arbitrary directories, do not require root namespace in path/allow arbitrary path per root namespace. #5

Closed ghedipunk closed 7 years ago

ghedipunk commented 7 years ago

Leaving this here for review, mostly.

My use case, that prompted me to make these changes in my fork

The directory structure that I'm working with here is PSR-0-like, but not compatible with PSR-0, because it doesn't use the root namespace in any folders. (Actually, it does use the root namespace in the project's root folder, but there are a couple of directories between the root folder and the folder where autoloaded classes reside.)

My directory structure:

So, because of the project's special needs, I have edited the \Axel\Module\Library class to be a bit more configurable, and maintaining backwards compatibility by making the new constructor arguments optional.

Here's /bigdumbogre/Private/bootstrap.php, as an example of how I'm using it.

<?php

$globalBasePath = __DIR__;

require_once('Classes/External/Axel/axel.php');
$axel = new \Axel\Axel;

require_once('Classes/External/Dice/Dice.php');
$dice = new \Dice\Dice();

$axel->addModule(new \Axel\Module\PSR0('/Classes/External/Transphporm/src', '\\Transphporm'));
$axel->addModule(new \Axel\Module\Library($axel, '/Classes/Foundation/Configuration', '', $globalBasePath));

session_start();

/bigdumbogre/Private/Classes/Configuration/autoload.json for this project

{
    "include": ["../Autoloader.php"],
    "modules": {
        "\\BigDumbOgre\\Foundation\\Autoloader": []
    }
}

And /bigdumbogre/index.php that is able to get an arbitrary class

<?php

require_once('Private/bootstrap.php');

$router = $dice->create('\\BigDumbOgre\\Foundation\\Router');