Skip to content

Commit c906603

Browse files
committed
add readme
1 parent a9fa06c commit c906603

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Laravel Hook System
2+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/quix-labs/laravel-hook-system.svg?style=flat-square)](https://packagist.org/packages/quix-labs/laravel-hook-system)
3+
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/quix-labs/laravel-hook-system/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/quix-labs/laravel-hook-system/actions?query=workflow%3Arun-tests+branch%3Amain)
4+
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/quix-labs/laravel-hook-system/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/quix-labs/laravel-hook-system/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
5+
[![Total Downloads](https://img.shields.io/packagist/dt/quix-labs/laravel-hook-system.svg?style=flat-square)](https://packagist.org/packages/quix-labs/laravel-hook-system)
6+
___
7+
The `quix-labs/laravel-hook-system` package provides a hook system for Laravel.
8+
9+
This system allows intercepting and manipulating specific actions in your application.
10+
___
11+
12+
## Requirements
13+
* PHP >= 8.1
14+
* Laravel 10.x|11.x
15+
16+
## Installation
17+
18+
You can install this package via Composer:
19+
20+
```bash
21+
composer require quix-labs/laravel-hook-system
22+
```
23+
24+
## Hook usage
25+
26+
### Creating a Hook
27+
28+
A hook is a class that extends `QuixLabs\LaravelHookSystem\Hook`:
29+
30+
```php
31+
class GetString extends \QuixLabs\LaravelHookSystem\Hook
32+
{
33+
public function __construct(public string &$string)
34+
{
35+
}
36+
}
37+
```
38+
39+
### Registering Hooks
40+
41+
In the `register` method of your ServiceProvider:
42+
43+
```php
44+
use QuixLabs\LaravelHookSystem\Facades\HookManager;
45+
use Workbench\App\Hooks\GetString;
46+
47+
class YourProvider{
48+
public function register()
49+
{
50+
HookManager::registerHook(GetString::class);
51+
}
52+
}
53+
54+
```
55+
56+
### Executing a Hook
57+
58+
To execute a hook, `QuixLabs\LaravelHookSystem\Hook` implements the static `send` method:
59+
60+
```php
61+
class YourController
62+
{
63+
public function index()
64+
{
65+
$string = "";
66+
\Workbench\App\Hooks\GetString::send($string);
67+
return $string;
68+
}
69+
}
70+
```
71+
72+
## Interceptor usage
73+
74+
### Creating an Interceptor
75+
76+
An interceptor is a class with a static method intercepted via an `#[Intercept]` attribute:
77+
78+
```php
79+
use Illuminate\Support\Str;
80+
use QuixLabs\LaravelHookSystem\Enums\ActionWhenMissing;
81+
use QuixLabs\LaravelHookSystem\Utils\Intercept;
82+
83+
class AppendRandomString
84+
{
85+
#[Intercept(\Workbench\App\Hooks\GetString::class)]
86+
public static function appendRandomString(GetString $hook): void
87+
{
88+
$hook->string .= Str::random(16);
89+
}
90+
91+
# You can specify action when hook not found (THROW_ERROR, SKIP or REGISTER_HOOK)
92+
#[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::THROW_ERROR)]
93+
public static function appendStringRequired(GetString $hook): void
94+
{
95+
$hook->string .= Str::random(16);
96+
}
97+
98+
# You can also specify execution priority using third argument
99+
#[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::SKIP, 100)]
100+
public static function appendRandomStringAtTheEnd(GetString $hook): void
101+
{
102+
$hook->string .= Str::random(16);
103+
}
104+
}
105+
```
106+
107+
### Registering Interceptors
108+
109+
In the `boot` method of your ServiceProvider:
110+
111+
```php
112+
use QuixLabs\LaravelHookSystem\Facades\HookManager;
113+
114+
class YourProvider{
115+
public function boot()
116+
{
117+
HookManager::registerInterceptor(\App\Interceptors\AppendRandomString::class);
118+
}
119+
}
120+
```
121+
122+
## Artisan Commands
123+
124+
The package adds three Artisan commands to manage the hooks:
125+
126+
* `hooks:status` : Displays the status of hooks and interceptors.
127+
* `hooks:cache` : Caches the hooks and interceptors.
128+
* `hooks:clear` : Clears the hooks and interceptors cache.
129+
130+
131+
## Changelog
132+
133+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
134+
135+
## Credits
136+
137+
- [COLANT Alan](https://github.com/alancolant)
138+
- [All Contributors](../../contributors)
139+
140+
## License
141+
142+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

tests/tests.zip

3.85 KB
Binary file not shown.

0 commit comments

Comments
 (0)