Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nabeghe/hooker

A simple hook system library (actions and filters) for php based on Symfony EventDispatcher
https://github.com/nabeghe/hooker

event eventdispatcher events hook hooks php symfony

Last synced: about 1 month ago
JSON representation

A simple hook system library (actions and filters) for php based on Symfony EventDispatcher

Awesome Lists containing this project

README

        

# Hooker for PHP

> A simple hook system library (actions and filters) based on Symfony EventDispatcher.

## 🫡 Usage

### 🚀 Installation

You can install the package via composer:

```bash
composer require nabeghe/hooker
```

### Examples

Check the examples folder in the repositiry.

#### Action

```php
require 'vendor/autoload.php';

use Nabeghe\Hooker\Hooker;
use Nabeghe\Hooker\Action;

$hooker = new Hooker();

$hooker->setDefaultArgToHooks('protocol', 'https://');

$hooker->listen('your_custom_action_name', function (Action $action) {
echo $action['protocol'].$action['url'].PHP_EOL;
}, 2);

$hooker->action('your_custom_action_name', ['url' => 'https://github.com/nabeghe/hooker']);
```

#### Filter:

```php
require 'vendor/autoload.php';

use Nabeghe\Hooker\Hooker;
use Nabeghe\Hooker\Filter;

$hooker = new Hooker();

$hooker->listen('your_custom_action_name', function (Filter $filter) {
if ($filter->getValue() === null) {
$filter->setValue(8 + $filter['default']);
}
});

$value = $hooker->filter('your_custom_action_name', null, ['default' => 5]);
echo $value; // 13
```