API reference and troubleshooting
XWP-DI v1.10.0 supports PHP 8.1 through 8.4 and PHP-DI ^7.1. Composer loads the global xwp_* functions and the three namespaced definition helpers. Release manifest
Container functions
| Function | When to use it |
|---|---|
xwp_load_app( $app, $hook = 'plugins_loaded', $priority = PHP_INT_MIN ) | Schedule container creation. Returns the boolean result from registering the WordPress action. |
xwp_create_app( $args ) | Build a DI\Container immediately, or retrieve the existing one for the same ID. |
xwp_has( $container_id ) | Check whether that container has been built in this request. |
xwp_app( $container_id ) | Retrieve an existing DI\Container. Use its get() and has() methods for individual entries. |
xwp_extend_app( $container, $module, $position = 'after', $target = null ) | Register a module class or array of module classes to import when building an extendable module. Returns void. |
xwp_decompile_app( $container_id, $immediately = false ) | Schedule deletion of the container's compile directory on shutdown, or delete it immediately. Returns a boolean. |
The array passed to the first two functions uses the settings below. For import order and a two-plugin example, see Modules and startup. Function signatures
Container settings
| Key | Default | Meaning |
|---|---|---|
id | Required | Unique string used by xwp_app(), module and handler decorators, and extensions. |
module | Required | Autoloadable root class with a #[Module] attribute. |
attributes | true | Enable PHP-DI attribute-based injection, including #[DI\Attribute\Inject]. |
autowiring | true | Enable PHP-DI autowiring from type declarations. With attributes enabled, PHP-DI still installs its attribute-based autowiring source. Set both options to false for explicit definitions only. |
compile | true when wp_get_environment_type() === 'production' | Let PHP-DI generate a container class on disk. |
compile_class | 'CompiledContainer' . strtoupper( $id ) | Name of the generated PHP class. Use a unique, valid class name when the ID contains punctuation, such as a hyphen. |
compile_dir | WP_CONTENT_DIR . '/cache/xwp-di/' . $id | Directory for this container's generated files. Give each container a directory of its own. |
proxies | false | Request that PHP-DI write lazy-proxy classes to compile_dir on PHP versions that use generated proxies. |
XWP-DI discovers its own WordPress decorators through reflection independently of the PHP-DI attributes setting. Read the effective configuration with $container->get( 'xwp.app.config' ). To expand a hook name from container entries, use $container->make( 'xwp.app.tag', array( 'tag' => '{prefix}_ready' ) ), where prefix is a defined entry. Factory defaults · Builder · PHP-DI 7.1.1 builder
WordPress definition helpers
Import these from XWP\DI and return them as entries from a module's public static function configure(): array.
use function XWP\DI\filtered;
use function XWP\DI\option;
use function XWP\DI\transient;
// Inside configure().
return array(
'catalog.page_size' => option( 'bookshop_page_size', 12 ),
'catalog.featured' => transient( 'bookshop_featured_ids', array() ),
'catalog.heading' => filtered( 'bookshop_heading', 'New books', 'catalog' ),
);
| Definition | Resolution |
|---|---|
option( $option_name, $default_value = false ) | Reads get_option(). Set the fallback inline or with ->default( $value ). |
transient( $transient_name, $default_value = false ) | Reads get_transient() and returns the fallback whenever WordPress returns false, including a stored boolean false. Supports ->default( $value ). |
filtered( $hook, $value = null, ...$args ) | Calls apply_filters( $hook, $value, ...$args ) on resolution. Adjust the definition with ->hook( $hook ), ->value( $value ), ->args( $args ) or ->arg( $arg, $index = null ). |
Use literal option, transient and filter names here. With v1.10.0 and its locked PHP-DI 7.1.1, a {prefix} expression in these helper names can raise a TypeError when the nested factory resolves. For a computed option name, use a PHP-DI factory and call get_option() inside it. PHP-DI caches a resolved entry for subsequent get() calls. Use $container->make( 'catalog.heading' ) when you need to run that filter again in the same request. Helper functions · Helper implementations · PHP-DI resolution
Decorators
Import attributes from XWP\DI\Decorators. These are the names used in v1.10.0.
| Decorator | Target and purpose |
|---|---|
Module | Class. Requires container, hook and priority. Accepts imports, handlers and extendable. |
Handler | Class. Sets the registration action (tag), container, priority, context, condition and initialization strategy. |
Action, Filter | Method. Supply a hook tag. Options include priority, context, conditional, modifiers, invoke, args and params. |
Dynamic_Action, Dynamic_Filter | Method. Supply a tag pattern and vars to register one callback for each substitution. |
Infuse | Method. Supplies container entries to the lifecycle methods can_initialize() and on_initialize(). |
Ajax_Handler, Ajax_Action | Class and method. The class names its container. The method names its action and prefix and can configure access, nonce and request variables. |
REST_Handler, REST_Route | Class and method. The class takes namespace, basename and container. The route takes route and methods, with an optional permission guard. |
CLI_Handler, CLI_Command | Class and method. Set the WP-CLI command namespace on the class and the subcommand name on the method. |
For callback signatures, execution flags and request-specific examples, see Handlers and hooks and WordPress integrations. Tagged decorator source
Module and handler functions
| Function | Result |
|---|---|
xwp_get_module( $classname ) | Registered Module decorator for a module class, or null if absent. |
xwp_register_module( $classname ) | Register a decorated module class and return its Module decorator. The builder does this for the root and its imports. |
xwp_register_hook_handler( $classname ) | Register a decorated class according to its initialization strategy. Returns the handler decorator (Can_Handle). |
xwp_load_hook_handler( $instance, $container = null ) | Register method hooks on an existing instance and return its handler decorator. Supply the container ID when the class does not declare one. |
xwp_hook_invoker() | Get the shared Invoker. Inspect a class with get_handler( $classname ) and get_hooks( $classname ). |
The invoker indexes registrations by class name across containers. Give distinct handlers their own classes when multiple containers coexist in one request. Handler functions · Invoker
Compilation and cache invalidation
If a changed definition still resolves to its previous value in production, inspect xwp.app.config for compile and compile_dir. PHP-DI reuses the generated PHP file until you remove it. Clear the container's dedicated directory when deploying changed definitions, including extensions that override a compiled entry. Rebuild on the next request. PHP-DI production deployment
Call the helper from a one-time upgrade or maintenance routine after the container has been built.
if ( xwp_has( 'bookshop' ) ) {
xwp_decompile_app( 'bookshop' );
}
The default return value of true means deletion was scheduled for shutdown. Pass immediately: true to receive the filesystem operation's result. The helper recursively removes the entire configured directory. Keep source files and other applications' caches outside it. The helper returns false if compilation is disabled or the directory is absent. A running request keeps its existing container. Decompilation source
A callback never runs
- Check
xwp_has( $id )after the scheduled creation action. If it is false, confirm your bootstrap loaded Composer and registeredxwp_load_app()before that action. - Follow the order of the module's
hook, the handler'stag, then the callback'stag. If two share an action, give registration the earlier priority.did_action( 'init' )reports how many times that action has fired in the request. WordPress action counts - Check
XWP_Context::show()in the failing request. Compare it with both the handler and method contexts. For example, an admin AJAX request matchesCTX_AJAX. Check the handler'scan_initialize()and the configuredconditionaltoo. Context detection - Inspect
xwp_hook_invoker()->get_hooks( My_Handler::class )after registration. If the method is missing, check that the module lists the handler and that the callback is a decorated, public, non-static method. If present, check the final tag, priority and WordPress arguments. Method discovery