|
1 | 1 | # Laravel Hookable |
2 | 2 |
|
3 | 3 | [](https://packagist.org/packages/saschaende/laravel-hookable) |
4 | | -[]( |
| 4 | + |
| 5 | +Laravel Hookable is a package that allows you to easily add hooks (actions and filters) to your Laravel applications, |
| 6 | +similar to the hook system in WordPress. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +You can install the package via Composer: |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require saschaende/laravel-hookable |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Adding Hooks |
| 19 | + |
| 20 | +You can add hooks using the `Hookable` facade. There are two types of hooks: actions and filters. |
| 21 | + |
| 22 | +#### Actions |
| 23 | + |
| 24 | +Actions are hooks that do not modify data. You can add an action like this: |
| 25 | + |
| 26 | +```php |
| 27 | +use SaschaEnde\Hookable\Facades\Hookable; |
| 28 | +Hookable::addAction('my_action', function ($arg1, $arg2) { |
| 29 | + // Do something with $arg1 and $arg2 |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +You can then trigger the action like this: |
| 34 | + |
| 35 | +```php |
| 36 | +Hookable::doAction('my_action', $arg1, $arg2); |
| 37 | +``` |
| 38 | + |
| 39 | +#### Filters |
| 40 | + |
| 41 | +Filters are hooks that modify data. You can add a filter like this: |
| 42 | + |
| 43 | +```php |
| 44 | +use SaschaEnde\Hookable\Facades\Hookable; |
| 45 | +Hookable::addFilter('my_filter', function ($value) { |
| 46 | + // Modify $value |
| 47 | + return $value; |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +You can then apply the filter like this: |
| 52 | + |
| 53 | +```php |
| 54 | +$value = Hookable::applyFilters('my_filter', $value); |
| 55 | +``` |
| 56 | + |
| 57 | +### Priorities and Arguments |
| 58 | + |
| 59 | +You can specify the priority and number of arguments for your hooks. |
| 60 | + |
| 61 | +```php |
| 62 | +Hookable::addAction('my_action', function ($arg1, $arg2) { |
| 63 | + // Do something |
| 64 | +}, 20); |
| 65 | +``` |
| 66 | + |
| 67 | +In this example, the action will be executed with a priority of 20 and will receive |
| 68 | +2 arguments. |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +You can run the tests using PHPUnit: |
| 73 | + |
| 74 | +```bash |
| 75 | +vendor/bin/phpunit |
| 76 | +``` |
| 77 | + |
| 78 | +## Contributing |
| 79 | + |
| 80 | +Contributions are welcome! Please submit a pull request or open an issue on GitHub. |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +This package is open-sourced software licensed under the MIT license. |
| 85 | + |
0 commit comments