1. Go to this page and download the library: Download phariscope/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/ */
phariscope / event example snippets
namespace App\\Domain\\Account;
use Phariscope\\Event\\EventDispatcher;
use Phariscope\\Event\\Psr14\\Event;
use Phariscope\\Event\\Psr14\\ListenerInterface;
// 1) The domain event
final class AccountCreated extends Event
{
public function __construct(
public string $accountId,
\DateTimeImmutable $occurredOn = new \DateTimeImmutable()
) {
parent::__construct($occurredOn);
}
}
// 2) The aggregate that emits the event
final class Account
{
public function __construct(private string $id)
{
// ... domain invariants and state initialization ...
// At the end of construction, dispatch the domain event
EventDispatcher::instance()->dispatch(new AccountCreated($this->id));
}
}
// 3) A listener that reacts to the event
final class SendWelcomeEmailListener implements ListenerInterface
{
public function handle(Event $event): bool
{
if (!$event instanceof AccountCreated) {
return false;
}
// send email here
return true;
}
public function isSubscribedTo(Event $event): bool
{
return $event instanceof AccountCreated;
}
}
// 4) Wiring the listener and triggering the flow
$dispatcher = EventDispatcher::instance();
$dispatcher->subscribe(new SendWelcomeEmailListener());
// Somewhere in your application flow
new Account('acc-123');
// Process the queued events (unless you enabled immediate distribution)
$dispatcher->distribute();
use Phariscope\Event\EventDispatcher;
$dispatcher = EventDispatcher::instance();
$dispatcher->distributeImmediately(); // enables automatic distribute() after each dispatch
// You can disable it later if needed
$dispatcher->disableImmediateDistribution();
// And you can check the current mode
if ($dispatcher->isImmediateDistributionEnabled()) {
// ...
}
use Phariscope\Event\EventDispatcher;
use Psr\Log\NullLogger; // or Monolog\Logger
$dispatcher = EventDispatcher::instance();
$dispatcher->setLogger(new NullLogger());
use Phariscope\Event\EventDispatcher;
$dispatcher = EventDispatcher::instance();
// Remove all currently subscribed listeners
$dispatcher->clearSubscribers();
// If you also need a fresh instance (clears queue and state)
EventDispatcher::tearDown();
$dispatcher = EventDispatcher::instance();
// Avant (legacy)
Phariscope\Event\EventDispatcher::instance()->dispatch($event);
Phariscope\Event\EventDispatcher::instance()->distribute();
// Après (sémantique PSR intégrée)
$dispatcher->dispatch($event); // synchronously handled when immediate distribution is enabled
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.