PHP code example of jonquihote / pluggable

1. Go to this page and download the library: Download jonquihote/pluggable 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/ */

    

jonquihote / pluggable example snippets




namespace App\Pluggables;

use Pluggable\Pluggable\PluggableAction;

final class OrderPlaced extends PluggableAction
{
    public function __construct(public readonly int $orderId) {}
}

$action = OrderPlaced::dispatch($order->id);



namespace App\Pluggables;

use Illuminate\Support\Collection;
use Pluggable\Pluggable\PluggableFilter;

/** @extends PluggableFilter<Collection<int, string>> */
final class NavigationItems extends PluggableFilter
{
    /** @param Collection<int, string> $items */
    public function __construct(Collection $items = new Collection)
    {
        parent::__construct($items);
    }
}

$items = NavigationItems::dispatch()->getValue();



namespace App\Providers;

use App\Listeners\AddAccountNavigation;
use App\Listeners\SendOrderReceipt;
use App\Pluggables\NavigationItems;
use App\Pluggables\OrderPlaced;
use Pluggable\Pluggable\Providers\PluggableServiceProvider;

final class AppPluggableServiceProvider extends PluggableServiceProvider
{
    protected string $module = 'app';

    protected array $actions = [
        OrderPlaced::class,
    ];

    protected array $filters = [
        NavigationItems::class,
    ];

    protected array $listen = [
        OrderPlaced::class => [
            SendOrderReceipt::class,
        ],
        NavigationItems::class => [
            [AddAccountNavigation::class, 'handle'],
        ],
    ];
}

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\AppPluggableServiceProvider::class,
];

final readonly class SendOrderReceipt
{
    public function __construct(private ReceiptSender $receipts) {}

    public function __invoke(OrderPlaced $action): void
    {
        $this->receipts->sendForOrder($action->orderId);
    }
}

protected function bootModuleEvents(): void
{
    Order::created(
        fn (Order $order) => OrderPlaced::dispatch($order->getKey()),
    );
}

pluggable()->listen(
    NavigationItems::class,
    fn (NavigationItems $filter) => $filter->getValue()->push('Account'),
    priority: 20,
);
bash
php artisan boost:update
bash
php artisan pluggable:list
php artisan pluggable:list --type=filter --module=app
bash
php artisan pluggable:inspect 'App\Pluggables\OrderPlaced'