Map-based autoloader across php and phar resources

⌈⌋ ⎇ branch:  Canonic Autoloader


Update of "Canonic_Autoloader"

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

Artifact ID: f2a30ff5a3d5af05783139a2fc923b216a8ecb33
Page Name:Canonic_Autoloader
Date: 2014-02-15 02:21:03
Original User: mario
Mimetype:text/x-markdown
Parent: 243f82d7475b6b34ae3b4a57094391957e56dd91 (diff)
Content

The Canonic_Autoloader class in the phar stub 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.
  • 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 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.