Modules and startup
A module has two jobs. Its static configure() supplies service definitions when XWP-DI builds the container. Its #[Module] attribute names the WordPress action on which XWP-DI instantiates the module and registers its handlers. You can therefore resolve a service before the module's own action fires.
Schedule container creation
In your plugin bootstrap, load Composer and give the container a unique ID and an autoloadable root module.
require __DIR__ . '/vendor/autoload.php';
xwp_load_app(
array(
'id' => 'bookshop',
'module' => \Bookshop\Shop_Module::class,
),
);
By default, this schedules creation on plugins_loaded at PHP_INT_MIN. The helper returns the boolean from add_action(). Later callbacks can retrieve the built DI\Container with xwp_app( 'bookshop' ). For a theme bootstrapped from functions.php, pass hook: 'after_setup_theme' because WordPress has already fired plugins_loaded.
Use xwp_create_app() instead when you need the container at the call site. Make sure any extension plugins have already registered their imports and the module's initialization action is still ahead.
$container = xwp_create_app(
array(
'id' => 'bookshop',
'module' => \Bookshop\Shop_Module::class,
),
);
$currency = $container->get( 'bookshop.currency' );
Choose one bootstrap path. Calling xwp_create_app() again with an existing ID returns the container already built for that ID. Container functions · App factory
Define services and import modules
This root module supplies the store currency and imports a catalog module. Both modules use the same container ID. With Composer mapping Bookshop\ to src/, place each class in its own file.
<?php
namespace Bookshop;
use XWP\DI\Decorators\Module;
#[Module(
container: 'bookshop',
hook: 'init',
priority: 5,
imports: array( Catalog_Module::class ),
extendable: true,
)]
final class Shop_Module {
public static function configure(): array {
return array( 'bookshop.currency' => 'USD' );
}
}
<?php
namespace Bookshop;
use XWP\DI\Decorators\Module;
use function XWP\DI\option;
#[Module( container: 'bookshop', hook: 'init', priority: 10 )]
final class Catalog_Module {
public static function configure(): array {
return array( 'bookshop.page_size' => option( 'bookshop_page_size', 12 ) );
}
}
XWP-DI collects the root's definitions first, then visits each imported module and its imports in declaration order. For an ordinary entry defined twice, the later definition wins. Each module can return scalars, arrays or PHP-DI definitions, such as \DI\autowire( Implementation::class ) under an interface name. A module containing only hooks can omit configure(). Module source · Container builder
On init at priority 5, XWP-DI gets the root module instance from the container. At priority 10 it does the same for the catalog module. To add handlers, pass a list of decorated class names in a module's handlers: argument. Set each handler's container: to the same ID. The module registers these handlers as it initializes, so schedule their registration hooks and callbacks accordingly. A module can also declare its own action and filter methods. Handlers and hooks
Run code at initialization
Use a public instance method on_initialize(): void for work that belongs on the module's WordPress action. Use a public static can_initialize(): bool to gate initialization, for example when a module needs another plugin. XWP-DI collects configure() definitions before it checks that condition, so keep optional-plugin calls out of configure() itself. For values that become available at initialization, a public static configure_async(): array contributes entries via the existing container's set() after construction and before on_initialize(). Handler lifecycle
Extend a container from another plugin
Suppose an add-on sells books in euros. It can supply a module to the existing bookshop container. Give the add-on its own Composer-autoloaded class.
<?php
namespace BookshopEU;
use XWP\DI\Decorators\Module;
#[Module( container: 'bookshop', hook: 'init', priority: 15 )]
final class Euro_Module {
public static function configure(): array {
return array( 'bookshop.currency' => 'EUR' );
}
}
In the add-on's main plugin file, register the import before the base plugin builds its container.
require __DIR__ . '/vendor/autoload.php';
xwp_extend_app(
container: 'bookshop',
module: \BookshopEU\Euro_Module::class,
position: 'after',
target: \Bookshop\Shop_Module::class,
);
The root opts in with extendable: true. The default 'after' appends the add-on after the root's other imports, so $container->get( 'bookshop.currency' ) now returns 'EUR'. Use 'before' to put it first among those imports. In both positions, the root's own definitions are collected first. The module's WordPress priority controls initialization time independently of definition order.
An omitted target applies the extension to every extendable module for that container. Specify the target when extending one module and keep the import graph free of cycles. Changes to a compiled container also require cache invalidation. Extension implementation · Compilation and troubleshooting