PHP code example of hongxunpan / simple-event

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

    

hongxunpan / simple-event example snippets




namespace App\Events;

use DateTimeImmutable;
use HongXunPan\SimpleEvent\Event;

final readonly class SampleOccurred implements Event
{
    public const int VERSION = 1;

    public function __construct(
        public int $sampleId,
        public int $userId,
        public DateTimeImmutable $occurredAt,
    ) {
    }
}



namespace App\Listeners;

use App\Events\SampleOccurred;

final class WriteSampleAuditLog
{
    public function handle(SampleOccurred $event): void
    {
        // 写入示例审计事实。
    }
}



namespace App\Listeners;

use App\Events\SampleOccurred;
use HongXunPan\SimpleEvent\Listener\ShouldQueue;

final class SendSampleNotification implements ShouldQueue
{
    public function handle(SampleOccurred $event): void
    {
        // 异步副作用必须按业务唯一事实保证幂等。
    }
}



use App\Events\SampleOccurred;
use App\Listeners\WriteSampleAuditLog;

return [
    'listeners' => [
        SampleOccurred::class => [
            WriteSampleAuditLog::class,
        ],
    ],
];

event(new SampleOccurred(
    sampleId: 1,
    userId: 10001,
    occurredAt: new DateTimeImmutable(),
));

use HongXunPan\SimpleEvent\Listener\ShouldHandleBestEffort;

final class OptionalListener implements ShouldHandleBestEffort
{
    public function handle(SampleOccurred $event): void
    {
    }
}

return [
    'connections' => [
        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'port' => (int) env('REDIS_PORT', 6379),
            'database' => 0,
            'prefix' => 'app:',
        ],
    ],
];



use App\Events\SampleOccurred;
use App\Listeners\SendSampleNotification;
use HongXunPan\SimpleEvent\Driver\Redis\RedisStreamDriver;

return [
    'listeners' => [
        SampleOccurred::class => [
            SendSampleNotification::class,
        ],
    ],
    'driver' => [
        'class' => RedisStreamDriver::class,
        'options' => [
            'connection' => 'default',
            'stream' => 'business-events',
            'group' => 'application',
            'failed_stream' => 'business-events-failed',
            'block_ms' => 5000,
            'batch_size' => 10,
            'claim_idle_ms' => 60000,
            'failed_max_length' => 10000,
        ],
    ],
];

$processed = app(
    HongXunPan\SimpleEvent\Worker\EventWorker::class,
)->runOnce();

app(HongXunPan\SimpleEvent\Worker\EventWorker::class)->run(
    static fn (): bool => $shouldStop,
);

$context = app(HongXunPan\SimpleEvent\Execution\EventExecutionContext::class);
$fields = $context->snapshot();
bash
composer le module:enable event
bash
composer le module:enable redis
php bin/simple module:enable event
bash
SIMPLE_EVENT_AUTOLOAD=/项目/vendor/autoload.php php tests/TestRunner.php