PHP code example of waffle-commons / event-dispatcher
1. Go to this page and download the library: Download waffle-commons/event-dispatcher 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/ */
waffle-commons / event-dispatcher example snippets
use Waffle\Commons\EventDispatcher\Dispatcher\EventDispatcher;
use Waffle\Commons\EventDispatcher\Provider\ListenerProvider;
$provider = new ListenerProvider();
$provider->addListener(UserRegistered::class, function (UserRegistered $event): void {
// …
}, priority: 100); // higher priority = earlier
$dispatcher = new EventDispatcher($provider);
$event = $dispatcher->dispatch(new UserRegistered($userId));
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
final readonly class AsEventListener
{
public function __construct(
public ?string $event = null,
public int $priority = 0,
) {}
}
final class AuditListener
{
#[AsEventListener(priority: 50)]
public function onUserRegistered(UserRegistered $event): void
{
// resolved automatically from the parameter type
}
}
$provider->register(new AuditListener());
#[AsEventListener(event: UserRegistered::class, priority: 50)]
final class WelcomeMailer
{
public function send(UserRegistered $event): void { /* … */ }
}
$provider->register(new WelcomeMailer());
use Waffle\Commons\EventDispatcher\Event\AbstractStoppableEvent;
final class CancellableJob extends AbstractStoppableEvent
{
public function __construct(public readonly string $jobId) {}
}
$provider->addListener(CancellableJob::class, function (CancellableJob $e): void {
if ($shouldCancel) {
$e->stopPropagation();
}
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.