|
1 | 1 | <div align="center">
|
2 | 2 |
|
3 |
| -<h1 align="center" style="border-bottom: none;">WordPress Hook Dependency Injection</h1> |
| 3 | +<h1 align="center" style="border-bottom: none; margin-bottom: 0px">XWP-DI</h1> |
| 4 | +<h3 align="center" style="margin-top: 0px">Dependency Injection Container for WordPress</h3> |
4 | 5 |
|
5 |
| - |
6 |
| - |
7 |
| -[](https://github.com/semantic-release/semantic-release) |
| 6 | +[](https://packagist.org/packages/x-wp/di) |
| 7 | + |
| 8 | + |
| 9 | +[](https://github.com/x-wp/di/actions/workflows/release.yml) |
8 | 10 |
|
9 | 11 | </div>
|
| 12 | + |
| 13 | +This library allows you to implement [dependency injection design pattern](https://en.wikipedia.org/wiki/Dependency_injection) in your WordPress plugin or theme. It provides a simple and easy-to-use interface to manage dependencies and hook callbacks. |
| 14 | + |
| 15 | +## Key Features |
| 16 | + |
| 17 | +1. Reliable - Powered by [PHP-DI](https://php-di.org/), a mature and feature-rich dependency injection container. |
| 18 | +2. Interoperable - Provides PSR-11 compliant container interface. |
| 19 | +3. Easy to use - Reduces the boilerplate code required to manage dependencies and hook callbacks. |
| 20 | +4. Customizable - Allows various configuration options to customize the container behavior. |
| 21 | +5. Flexible - Enables advanced hook callback mechanisms. |
| 22 | +6. Fast - Dependencies are resolved only when needed, and the container can be compiled for better performance. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +You can install this package via composer: |
| 27 | + |
| 28 | +```bash |
| 29 | +composer require x-wp/di |
| 30 | +``` |
| 31 | + |
| 32 | +> [!TIP] |
| 33 | +> We recommend using the `automattic/jetpack-autoloader` with this package to prevent autoloading issues. |
| 34 | +
|
| 35 | +## Usage |
| 36 | + |
| 37 | +Below is a simple example to demonstrate how to use this library in your plugin or theme. |
| 38 | + |
| 39 | +### Creating the Application and Container |
| 40 | + |
| 41 | +You will need a class which will be used as the entry point for your plugin/theme. This class must have a `#[Module]` attribute to define the container configuration. |
| 42 | + |
| 43 | +```php |
| 44 | +<?php |
| 45 | + |
| 46 | +use XWP\DI\Decorators\Module; |
| 47 | + |
| 48 | +#[Module( |
| 49 | + container: 'my-plugin', // Unique identifier for the container |
| 50 | + hook: 'plugins_loaded', // Hook to initialize the a |
| 51 | + priority: 10, // Hook priority |
| 52 | + imports: array(), // List of classnames imported by this module |
| 53 | + handlers: array(), // List of classnames which are used as handlers |
| 54 | +)] |
| 55 | +class My_Plugin { |
| 56 | + /** |
| 57 | + * Returns the PHP-DI container definition. |
| 58 | + * |
| 59 | + * @see https://php-di.org/doc/php-definitions.html |
| 60 | + * |
| 61 | + * @return array<string,mixed> |
| 62 | + */ |
| 63 | + public static function configure(): array { |
| 64 | + return array( |
| 65 | + 'my.def' => \DI\value('my value'), |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +After defining the module, you can create the application using the `xwp_create_app` function. |
| 72 | + |
| 73 | +```php |
| 74 | +<?php |
| 75 | + |
| 76 | +xwp_create_app( |
| 77 | + array( |
| 78 | + 'id' => 'my-plugin', |
| 79 | + 'module' => My_Plugin::class, |
| 80 | + 'compile' => false, |
| 81 | + ); |
| 82 | +); |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +### Using handlers and callbacks |
| 87 | + |
| 88 | +Handler is any class which is annotated with a `#[Handler]` attribute. Class methods can be annotated with `#[Action]` or `#[Filter]` attributes to define hook callbacks. |
| 89 | + |
| 90 | +```php |
| 91 | +<?php |
| 92 | + |
| 93 | +use XWP\DI\Decorators\Action; |
| 94 | +use XWP\DI\Decorators\Filter; |
| 95 | +use XWP\DI\Decorators\Handler; |
| 96 | + |
| 97 | +#[Handler( |
| 98 | + tag: 'init', |
| 99 | + priority: 20, |
| 100 | + container: 'my-plugin', |
| 101 | + context: Handler::CTX_FRONTEND, |
| 102 | +)] |
| 103 | +class My_Handler { |
| 104 | + #[Filter( tag: 'body_class', priority: 10 )] |
| 105 | + public function change_body_class( array $classes ): array { |
| 106 | + $classes[] = 'my-class'; |
| 107 | + |
| 108 | + return $classes; |
| 109 | + } |
| 110 | + |
| 111 | + #[Action( tag: 'wp_enqueue_scripts', priority: 10 )] |
| 112 | + public function enqueue_scripts(): void { |
| 113 | + wp_enqueue_script('my-script', 'path/to/my-script.js', array(), '1.0', true); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Documentation |
| 119 | + |
| 120 | +For more information, please refer to the [official documentation](https://extended.wp.rs/dependency-injection). |
0 commit comments