Autoconfig

The Autoconfig feature automatically generates a configuration file used to instantiate the application and configure some of its low-level components. Currently, it defines configuration constructors, paths to components configurations, paths to locale message catalogs, and paths to modules.

Generating the autoconfig file

The autoconfig file is generated at vendor/icanboogie/autoconfig.php after the autoloader is dumped, during the post-autoload-dump event emitted by Composer. In order for the autoconfig feature to work, a script for the event must be declared in the root package of the application:

{
    "scripts": {
        "post-autoload-dump": "ICanBoogie\\Autoconfig\\Hooks::on_autoload_dump"
    }
}

Obtaining the autoconfig

The autoconfig is obtained using the get_autoconfig() function. The function also updates the app-root and app-paths values with the resolved application root and the resolved application paths respectively. Of course, the autoconfig can be used as is to instantiate the application:

<?php

namespace ICanBoogie;

$app = boot(get_autoconfig());

Additionally, the ICanBoogie\AUTOCONFIG_PATHNAME constant defines the absolute pathname to the autoconfig file.

Note: A fatal error is triggered if the autoconfig file does not exists, which might happen if the post-autoload-dump hook is missing from the composer.json file of the application.

Altering the autoconfig at runtime

Filters defined with autoconfig-filters are invoked to alter the autoconfig before it is returned by the get_autoconfig() helper. For instance, ICanBoogie uses this feature to add config directories found in the application paths (using the multi-site support).

{
    "extra": {
        "icanboogie": {
            "autoconfig-filters": [ "ICanBoogie\\Autoconfig\\Hooks::filter_autoconfig" ]
        }
    }
}

Participating in the autoconfig process

To participate in the autoconfig process, packages need to define their autoconfig fragment in the extra/icanboogie section of their composer.json file. The file must match the autoconfig schema.

The following example demonstrates how a package can specify the path to its configuration and locale messages.

{
    "extra": {
        "icanboogie": {
            "config-path": "path/to/config",
            "locale-path": "path/to/locale"
        }
    }
}

Autoconfig extensions

Extensions can participate in the autoconfig process to provide additional features. One creates an extension by extending the ExtensionAbstract class. If the extension requires a new property in the autoconfig fragments, it needs to be specified with the alter_schema() method so that the schema can be validated. Autoconfig is very strict with its schema. The extension used by icanboogie/module to alter the autoconfig with the configuration files defined by the application's modules is a good example, check it out on GitHub.

Registering an extension

Extension are registered using the composer.json file of the application or package. The following example demonstrates how to register a ModuleExtension, using the extra section:

{
    "extra": {
        "icanboogie": {
            "autoconfig-extension": "ICanBoogie\\Module\\Autoconfig\\ModuleExtension"
        }
    }
}