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
{
}
}