Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeghe committed Sep 19, 2024
0 parents commit df4d014
Show file tree
Hide file tree
Showing 13 changed files with 595 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
/vendor/
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Hadi Akbarzadeh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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
```
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "nabeghe/hooker",
"description": "A simple hook system library (actions and filters) based on Symfony EventDispatcher.",
"type": "library",
"version": "0.2.0",
"homepage": "https://github.com/nabeghe/hooker",
"license": "MIT",
"autoload": {
"psr-4": {
"Nabeghe\\Hooker\\": "src/"
}
},
"authors": [
{
"name": "Hadi Akbarzadeh",
"email": "[email protected]",
"homepage": "https://elatel.ir",
"role": "Developer"
}
],
"require": {
"symfony/event-dispatcher": "^7.1"
}
}
225 changes: 225 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions examples/1-action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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 'Priority 2: '.$action['protocol'].$action['url'].PHP_EOL;
}, 2); // priority 2

$hooker->listen('your_custom_action_name', function (Action $action) {
echo 'Priority 4: '.$action['protocol'].$action['url'].PHP_EOL;
}, 4); // priority 4

$hooker->listen('your_custom_action_name', function (Action $action) {
echo 'Priority 3: '.$action['protocol'].$action['url'].PHP_EOL;
}, 3); // priority 3

$hooker->listen('your_custom_action_name', function (Action $action) {
echo 'Priority 1: '.$action['protocol'].$action['url'].PHP_EOL;
}, 1); // priority 1

$hooker->listen('your_custom_action_name', function (Action $action) {
echo 'Priority 5: '.$action['protocol'].$action['url'].PHP_EOL;
}, 5); // priority 5

$hooker->action('your_custom_action_name', ['url' => 'https://github.com/nabeghe/hooker']);
23 changes: 23 additions & 0 deletions examples/2-filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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($filter['default']);
}
});

$hooker->listen('your_custom_action_name', function (Filter $filter) {
$filter->setValue($filter->getValue() + 2);
});

$hooker->listen('your_custom_action_name', function (Filter $filter) {
$filter->setValue($filter->getValue() + 3);
});

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

0 comments on commit df4d014

Please sign in to comment.