search hotlink icon profile hotlink icon
Package avatar Public | 1.1.x - 1.x.x

On This Page

Fusion automatically indexes all auto-loadable and preloadable code in your package, where individual, case-insensitive package identifier segments either match or form a prefix of the namespace segments.

Note

To illustrate loadable code, we use the Box - PHP DI Container package, which has the identifier valvoid/box.

Lazy Auto-Loadable Code

The lazy category includes all code for class, trait, enum, and interface that can be loaded on demand by a registered autoloader. In our example package, the main class uses a namespace that exactly matches the identifier segments:

                // exact match of identifier segments
namespace Valvoid\Box;

class Box {}
            

Note

Separators / and \ are ignored during the comparison.

Because of this match, it is mapped relative to the package root in the lazy.php file inside the package's stateful directory:

                return [
    'Valvoid\Box\Box' => '/src/Box.php'
];
            

Use this requireable file to include or reference individual class files, or to confirm that Fusion has correctly indexed your lazy-loadable code and built the autoloader prefixes.

Preloadable ASAP Code

The as-soon-as-possible (ASAP) category includes all code defined in function declarations that cannot be loaded by a registered autoloader. While our example package Box does not contain any ASAP code, an example with a namespace that starts with the identifier segments (prefix match) could look like:

                // starts with identifier segments (prefix match)
namespace Valvoid\Box\Util;

function doSomething(): void {}
            

Because of this prefix match, it would be listed relative to the package root in the asap.php file inside the package's stateful directory:

                return [
    '/src/Util/whatever.php',
];
            

Use this requireable file to include individual function files, or to verify that Fusion has recognized your ASAP code for immediate inclusion.

Common Autoloader Files

Based on extracted per-package files, lazy.php and asap.php, Fusion also automatically generates stateful files to manage code inclusion and autoloading:

Prefixes File

The prefixes.php file contains all namespace prefixes collected from every package in the dependency chain. Each package receives the complete map of prefixes, allowing it to build its own autoloader if needed, independent of the root package. For example, in the next major release of Fusion, it will use our example Box package as a dependency, declared in the production metadata fusion.json as follows:

                {
    "id": "valvoid/fusion",
    "structure": {
        "/dependencies": "valvoid.com/valvoid/box/1.0.0"
    }
}
            

Both packages would then have access to the following prefixes, from their own stateful directory:

                return [
    'Valvoid\Fusion\Tests' => '/tests',
    'Valvoid\Fusion' => '/src',
    'Valvoid\Box' => '/dependencies/valvoid/box/src'
];
            

Note

The map is sorted to check the longest prefix first.

Autoloader File

The PrefixAutoloader.php file is generated for the root package and uses the merged prefixes.php map to make all lazy-loadable code from every package available. When you create an instance and call its register method, it also immediately loads all ASAP files from all packages:

                $autoloader = new PrefixAutoloader;
$autoloader->register();
            

Use this file from the stateful directory of your root package to automatically load all lazy and ASAP code for your project if the default loadable behavior is enough for your needs.