Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1 complete #1

Merged
merged 13 commits into from
Nov 3, 2024
Empty file added .github/renovate.json
Empty file.
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI
on:
push:
branches:
- master
- develop
- alpha
- beta

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.OBLAKBOT_PAT }}
- name: Import GPG key
uses: crazy-max/ghaction-import-gpg@v6
id: gpg
with:
gpg_private_key: ${{ secrets.OBLAKBOT_GPG_KEY }}
passphrase: ${{ secrets.OBLAKBOT_GPG_PASS }}
git_config_global: true
git_user_signingkey: true
git_commit_gpgsign: true
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v4
with:
extra_plugins: |
@semantic-release/github
@semantic-release/exec
env:
GIT_AUTHOR_NAME: ${{ steps.gpg.outputs.name}}
GIT_AUTHOR_EMAIL: ${{ steps.gpg.outputs.email}}
GIT_COMMITTER_NAME: ${{ steps.gpg.outputs.name}}
GIT_COMMITTER_EMAIL: ${{ steps.gpg.outputs.email}}
GITHUB_TOKEN: ${{ secrets.OBLAKBOT_PAT }}
6 changes: 3 additions & 3 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[
"@semantic-release/exec",
{
"prepareCmd": "zip -r '/tmp/release.zip' ./src README.md"
"prepareCmd": "zip -r '/tmp/release.zip' ./src README.md ./composer.json"
}
],
[
Expand All @@ -29,8 +29,8 @@
"assets": [
{
"path": "/tmp/release.zip",
"name": "xwp-hook-invoker-${nextRelease.version}.zip",
"label": "xWP Hook Invoker v${nextRelease.version}"
"name": "xwp-di-v${nextRelease.version}.zip",
"label": "xWP Dependency Injection v${nextRelease.version}"
}
]
}
Expand Down
119 changes: 115 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,120 @@
<div align="center">

<h1 align="center" style="border-bottom: none;">WordPress Hook Dependency Injection</h1>
<h1 align="center" style="border-bottom: none; margin-bottom: 0px">XWP-DI</h1>
<h3 align="center" style="margin-top: 0px">Dependency Injection Container for WordPress</h3>

![Packagist Version](https://img.shields.io/packagist/v/oblak/wp-hook-di)
![Packagist PHP Version](https://img.shields.io/packagist/dependency-v/oblak/wp-hook-di/php)
[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)
[![Packagist Version](https://img.shields.io/packagist/v/x-wp/di?label=Release&style=flat-square)](https://packagist.org/packages/x-wp/di)
![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)
![Static Badge](https://img.shields.io/badge/WP-%3E%3D6.4-3858e9?style=flat-square&logo=wordpress&logoSize=auto)
[![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)

</div>

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.

## Key Features

1. Reliable - Powered by [PHP-DI](https://php-di.org/), a mature and feature-rich dependency injection container.
2. Interoperable - Provides PSR-11 compliant container interface.
3. Easy to use - Reduces the boilerplate code required to manage dependencies and hook callbacks.
4. Customizable - Allows various configuration options to customize the container behavior.
5. Flexible - Enables advanced hook callback mechanisms.
6. Fast - Dependencies are resolved only when needed, and the container can be compiled for better performance.

## Installation

You can install this package via composer:

```bash
composer require x-wp/di
```

> [!TIP]
> We recommend using the `automattic/jetpack-autoloader` with this package to prevent autoloading issues.

## Usage

Below is a simple example to demonstrate how to use this library in your plugin or theme.

### Creating the Application and Container

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.

```php
<?php

use XWP\DI\Decorators\Module;

#[Module(
container: 'my-plugin', // Unique identifier for the container
hook: 'plugins_loaded', // Hook to initialize the a
priority: 10, // Hook priority
imports: array(), // List of classnames imported by this module
handlers: array(), // List of classnames which are used as handlers
)]
class My_Plugin {
/**
* Returns the PHP-DI container definition.
*
* @see https://php-di.org/doc/php-definitions.html
*
* @return array<string,mixed>
*/
public static function configure(): array {
return array(
'my.def' => \DI\value('my value'),
);
}
}
```

After defining the module, you can create the application using the `xwp_create_app` function.

```php
<?php

xwp_create_app(
array(
'id' => 'my-plugin',
'module' => My_Plugin::class,
'compile' => false,
);
);

```

### Using handlers and callbacks

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.

```php
<?php

use XWP\DI\Decorators\Action;
use XWP\DI\Decorators\Filter;
use XWP\DI\Decorators\Handler;

#[Handler(
tag: 'init',
priority: 20,
container: 'my-plugin',
context: Handler::CTX_FRONTEND,
)]
class My_Handler {
#[Filter( tag: 'body_class', priority: 10 )]
public function change_body_class( array $classes ): array {
$classes[] = 'my-class';

return $classes;
}

#[Action( tag: 'wp_enqueue_scripts', priority: 10 )]
public function enqueue_scripts(): void {
wp_enqueue_script('my-script', 'path/to/my-script.js', array(), '1.0', true);
}
}
```

## Documentation

For more information, please refer to the [official documentation](https://extended.wp.rs/dependency-injection).
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"php": ">=8.0",
"automattic/jetpack-constants": "^2",
"php-di/php-di": "^7",
"symfony/polyfill-php81": "^1.31",
"x-wp/helper-classes": "^1.13",
"x-wp/helper-functions": "^1.13"
},
Expand All @@ -36,8 +37,15 @@
"swissspidy/phpstan-no-private": "^0.2",
"szepeviktor/phpstan-wordpress": "^1.3"
},
"conflict": {
"oblak/wp-hook-di": "*"
},
"provide": {
"x-wp/di-implementation": "self.version"
"psr/container-implementation": "^1.0",
"x-wp/di-implementation": "^1.0"
},
"replace": {
"x-wp/hook-invoker": "*"
},
"suggest": {
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package."
Expand All @@ -52,14 +60,14 @@
]
},
"config": {
"platform": {
"php": "8.0"
},
"allow-plugins": {
"automattic/jetpack-autoloader": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
},
"platform": {
"php": "8.0"
},
"sort-packages": true
}
}
Loading