PHP code example of vented / laravel-plenum

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

    

vented / laravel-plenum example snippets


Plenum::redis()->set('cache:key', 'value');

use Vented\Plenum\Contracts\RoutingStrategy;
use Vented\Plenum\Strategies\AuthUserStrategy;
use Vented\Plenum\Strategies\CompositeStrategy;
use Vented\Plenum\Strategies\SessionOnlyStrategy;

$this->app->singleton(RoutingStrategy::class, fn ($app) => new CompositeStrategy(
    $app->make(AuthUserStrategy::class),
    $app->make(SessionOnlyStrategy::class),
));

use Vented\Plenum\Contracts\RoutingStrategy;

final class ProjectStrategy implements RoutingStrategy
{
    public function resolve(): int|string|null
    {
        return request()->route('project')?->id;
    }

    public function name(): string
    {
        return 'project';
    }
}

// In a service provider:
$this->app->bind(RoutingStrategy::class, ProjectStrategy::class);

use Illuminate\Support\Facades\Schedule;

Schedule::command('plenum:probe')->everyTenSeconds();

use Vented\Plenum\Contracts\HealthChecker;

final class SpockLagAwareHealthChecker implements HealthChecker
{
    public function probe(string $driver, string $node, ConnectionDriver $resolver): NodeStatus
    {
        // Run SELECT lag_bytes FROM spock.lag_tracker and mark down if above threshold.
    }
    // ... other methods
}

final class ProcessProjectReport implements ShouldQueue
{
    public function __construct(public readonly int $projectId) {}

    public function handle(): void
    {
        Plenum::execute('database', $this->projectId, function () {
            // All Eloquent / DB:: calls in here go to the routed node,
            // and a failover candidate is tried automatically if it fails.
        });
    }
}

Event::listen(NodeMarkedDown::class, fn ($e) => Slack::alert(
    "{$e->driver} node {$e->node} marked down: {$e->reason}"
));

// In a service provider's boot() method
use Vented\Plenum\Facades\Plenum;

Plenum::auth(fn ($request) => $request->user()?->can('viewPlenum') ?? false);

use Vented\Plenum\Testing\FakeStrategy;
use Vented\Plenum\Testing\FakeHealthChecker;

$this->app->bind(RoutingStrategy::class, fn () => new FakeStrategy('test-key-42'));
$this->app->bind(HealthChecker::class, FakeHealthChecker::class);
bash
php artisan vendor:publish --tag="plenum-config"
bash
php artisan vendor:publish --tag="plenum-views"