pixelplugin/wp-container is a WordPress plugin that provides a global PSR-compatible
dependency container accordingly to WordPress Container API.
composer require pixelplugin/wp-containerhttps://packagist.org/packages/pixelplugin/wp-container
- Install this plugin
- Require pixelplugin/wp-container-api via composer in your project
- Now you can use the Container API
/**
* @param array $definitions container definitions.
*/
apply_filters('wp_container', $definitions);The filter should be used to configure container definitions for your code. It supports the following formats:
ClassName::classto simply add some class to the container, but normally it's not needed, because the container supports auto-wiring.InterfaceName::class => ClassName::classto specify another implementation of some class/interface.ClassName::class => $instanceto bind a class name with a pre-created instance.ClassName::class => $callableto bind a class name with some callable factory.
This filter is used on after_setup_theme action to create the container,
so the container is completely available on init hook
(or on the same after_setup_theme but with lower priority).