Map-based autoloader across php and phar resources

⌈⌋ branch:  Canonic Autoloader


Artifact Content

Artifact 243f82d7475b6b34ae3b4a57094391957e56dd91:

Wiki page [Canonic_Autoloader] by mario 2014-02-13 21:48:06.
D 2014-02-13T21:48:06.554
L Canonic_Autoloader
N text/x-markdown
U mario
W 1443
The `Canonic_Autoloader` class in the [phar stub](wiki/split) implements the autoloader callback.

It's itself not a whole lot of code.

  *  The constructor just receives an `array` to populate the internal classmap from.
  *  It looks for an identifier (class / func / const) declaration.
  *  Turning relative paths or phar:// references into absolute ones is the bulkiest part.
  *  And it contains some logic for autoupdating setups.

The vast remainder in the stub is a lot of setup and relocatability code:

  *  The static `::hookup()` just registers the autoloader, and is automatically run on [including the phar](wiki/include_once).
  *  With `::update()` the map gets recreated.
  *  And `::pharBase()` just helps with figuring out if the stub is contained in a phar, or everything [split up](wiki/split) into separate scripts.

### Manual instantiation

If you rip out the automatic `hookup` code, then you can still easily register the autoloader. For a separated setup you'd use:

    spl_autoload_register(
        new Canonic_Autoloader(
            include("./vendor/autoload.map.php"),
            "./vendor/autoload.php"
        )
    );

The first argument being the array result from the map file. The second parameter refers to the base location of the autoloader/map script (just the base directory is actually required).

For manual updating see [`Canonic_Classmap`](wiki/Canonic_Classmap).
Z cc0e6e86a705f3f3cd499c63c5b104b7