PHP code example of rokke / contracts

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

    

rokke / contracts example snippets


use Rokke\Contracts\Module\ModuleInterface;

final class HttpModule implements ModuleInterface
{
    public function name(): string { return 'http'; }
    public function boot(): void { /* register services */ }
    public function start(): void { /* start server */ }
    public function stop(): void { /* graceful shutdown */ }
    public function health(): bool { return true; }
    public function dependencies(): array { return []; }
}

use Rokke\Contracts\Lifecycle\LifecycleEventsInterface;

$lifecycle->onRunning(function (): void {
    // application is fully started
});

$lifecycle->onStopping(function (): void {
    // drain in-flight requests
});

$result = $pipeline
    ->send($request)
    ->through([$authMiddleware, $loggingMiddleware])
    ->then(fn ($req) => $handler->handle($req));

$container->singleton(CacheInterface::class, RedisCache::class);
$container->pooled(DatabaseInterface::class, fn () => new Connection(), min: 2, max: 10);

$connection = $resources->acquire('database');
try {
    // use $connection
} finally {
    $resources->release('database', $connection);
}

$context->set('user', $authenticatedUser);
$user = $context->get('user');

$bus->dispatchSync(new UserRegistered($user));
$bus->dispatchBackground(new SendWelcomeEmail($user));

$dsn    = $config->env('DATABASE_URL');
$apiKey = $config->secret('STRIPE_KEY');
$debug  = $config->get('app.debug', false);

$now = $clock->now(); // DateTimeImmutable