Skip to content

Commit 3cea339

Browse files
authoredNov 3, 2024··
Merge pull request #1 from x-wp/beta
Version 1 complete
2 parents dbbd5cb + 321ce93 commit 3cea339

25 files changed

+1168
-117
lines changed
 

‎.github/renovate.json

Whitespace-only changes.

‎.github/workflows/release.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- develop
7+
- alpha
8+
- beta
9+
10+
jobs:
11+
release:
12+
name: Release
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
token: ${{ secrets.OBLAKBOT_PAT }}
20+
- name: Import GPG key
21+
uses: crazy-max/ghaction-import-gpg@v6
22+
id: gpg
23+
with:
24+
gpg_private_key: ${{ secrets.OBLAKBOT_GPG_KEY }}
25+
passphrase: ${{ secrets.OBLAKBOT_GPG_PASS }}
26+
git_config_global: true
27+
git_user_signingkey: true
28+
git_commit_gpgsign: true
29+
- name: Semantic Release
30+
uses: cycjimmy/semantic-release-action@v4
31+
with:
32+
extra_plugins: |
33+
@semantic-release/github
34+
@semantic-release/exec
35+
env:
36+
GIT_AUTHOR_NAME: ${{ steps.gpg.outputs.name}}
37+
GIT_AUTHOR_EMAIL: ${{ steps.gpg.outputs.email}}
38+
GIT_COMMITTER_NAME: ${{ steps.gpg.outputs.name}}
39+
GIT_COMMITTER_EMAIL: ${{ steps.gpg.outputs.email}}
40+
GITHUB_TOKEN: ${{ secrets.OBLAKBOT_PAT }}

‎.releaserc

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
[
2121
"@semantic-release/exec",
2222
{
23-
"prepareCmd": "zip -r '/tmp/release.zip' ./src README.md"
23+
"prepareCmd": "zip -r '/tmp/release.zip' ./src README.md ./composer.json"
2424
}
2525
],
2626
[
@@ -29,8 +29,8 @@
2929
"assets": [
3030
{
3131
"path": "/tmp/release.zip",
32-
"name": "xwp-hook-invoker-${nextRelease.version}.zip",
33-
"label": "xWP Hook Invoker v${nextRelease.version}"
32+
"name": "xwp-di-v${nextRelease.version}.zip",
33+
"label": "xWP Dependency Injection v${nextRelease.version}"
3434
}
3535
]
3636
}

‎README.md

+115-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,120 @@
11
<div align="center">
22

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>
45

5-
![Packagist Version](https://img.shields.io/packagist/v/oblak/wp-hook-di)
6-
![Packagist PHP Version](https://img.shields.io/packagist/dependency-v/oblak/wp-hook-di/php)
7-
[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)
6+
[![Packagist Version](https://img.shields.io/packagist/v/x-wp/di?label=Release&style=flat-square)](https://packagist.org/packages/x-wp/di)
7+
![Packagist PHP Version](https://img.shields.io/packagist/dependency-v/x-wp/di/php?label=PHP&logo=php&logoColor=white&logoSize=auto&style=flat-square)
8+
![Static Badge](https://img.shields.io/badge/WP-%3E%3D6.4-3858e9?style=flat-square&logo=wordpress&logoSize=auto)
9+
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/x-wp/di/release.yml?label=Build&event=push&style=flat-square&logo=githubactions&logoColor=white&logoSize=auto)](https://github.com/x-wp/di/actions/workflows/release.yml)
810

911
</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).

‎composer.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"php": ">=8.0",
2525
"automattic/jetpack-constants": "^2",
2626
"php-di/php-di": "^7",
27+
"symfony/polyfill-php81": "^1.31",
2728
"x-wp/helper-classes": "^1.13",
2829
"x-wp/helper-functions": "^1.13"
2930
},
@@ -36,8 +37,15 @@
3637
"swissspidy/phpstan-no-private": "^0.2",
3738
"szepeviktor/phpstan-wordpress": "^1.3"
3839
},
40+
"conflict": {
41+
"oblak/wp-hook-di": "*"
42+
},
3943
"provide": {
40-
"x-wp/di-implementation": "self.version"
44+
"psr/container-implementation": "^1.0",
45+
"x-wp/di-implementation": "^1.0"
46+
},
47+
"replace": {
48+
"x-wp/hook-invoker": "*"
4149
},
4250
"suggest": {
4351
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."
@@ -52,14 +60,14 @@
5260
]
5361
},
5462
"config": {
55-
"platform": {
56-
"php": "8.0"
57-
},
5863
"allow-plugins": {
5964
"automattic/jetpack-autoloader": true,
6065
"dealerdirect/phpcodesniffer-composer-installer": true,
6166
"phpstan/extension-installer": true
6267
},
68+
"platform": {
69+
"php": "8.0"
70+
},
6371
"sort-packages": true
6472
}
6573
}

0 commit comments

Comments
 (0)
Please sign in to comment.