PHP code example of patterns / event-channel

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

    

patterns / event-channel example snippets


// Before — the transport leaks into every script
$redis = new \Workerman\Redis\Client('redis://127.0.0.1:6379');   // loop-bound
$redis->subscribe(['stock.updated'], function ($channel, $msg) { /* ... */ });

// a cron script cannot use that: there is no event loop here
$redis = new \Redis();                                            // different client
$redis->pSubscribe(['stock.updated'], function ($pattern, $channel, $msg) { ... });

// After — the script only knows the contract
$channel->connect();
$channel->on('stock.updated', function (Event $event): void { /* ... */ });
$channel->publish($event);

namespace Patterns;

interface Event            { public function type(): string; }
interface EventPublisher   { public function publish(Event $event): void; }
interface EventSubscriber  { public function on(string $type, callable $handler): callable; }
interface EventProvider    { public function channel(string $topic): EventChannel; }

interface EventChannel extends EventPublisher, EventSubscriber
{
    public function connect(): void;
    public function close(): void;
}