PHP code example of quix-labs / laravel-hook-system

1. Go to this page and download the library: Download quix-labs/laravel-hook-system library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

quix-labs / laravel-hook-system example snippets


class GetString extends \QuixLabs\LaravelHookSystem\Hook
{
    public function __construct(public string &$string)
    {
    }
}

class GetString extends \QuixLabs\LaravelHookSystem\Hook implements \QuixLabs\LaravelHookSystem\Interfaces\FullyCacheable
{
    public function __construct(public string &$string)
    {
    }
    
    public static function initialInstance(): static
    {
        $string = 'initial-state';
        return new static($string);
    }
}

use Workbench\App\Hooks\GetString;

class YourProvider{
    public function register()
    {
        \QuixLabs\LaravelHookSystem\HookRegistry::registerHook(GetString::class);
    }
}


class YourController
{
    public function index()
    {
        $string = "";
        \Workbench\App\Hooks\GetString::send($string);
        return $string;
    }
}

use Illuminate\Support\Str;
use QuixLabs\LaravelHookSystem\Enums\ActionWhenMissing;
use QuixLabs\LaravelHookSystem\Utils\Intercept;

class AppendRandomString
{
    #[Intercept(\Workbench\App\Hooks\GetString::class)]
    public static function appendRandomString(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
    
    # You can specify action when hook not found (THROW_ERROR, SKIP or REGISTER_HOOK)
    #[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::THROW_ERROR)]
    public static function appendStringRequired(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
    
    # You can also specify execution priority using third argument
    #[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::SKIP, 100)]
    public static function appendRandomStringAtTheEnd(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
    # You can prevent full cache generation (useful if the interceptor depends on context request)
    #[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::SKIP, 100, false)]
    public static function appendRandomStringAtTheEnd(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
}

class YourProvider{
    public function boot()
    {
        \QuixLabs\LaravelHookSystem\HookRegistry::registerInterceptor(\App\Interceptors\AppendRandomString::class);
    }
}