1. Go to this page and download the library: Download ahmedosamir732/eventbus-php 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/ */
ahmedosamir732 / eventbus-php example snippets
use Idaratech\EventBus\Contracts\ContextProviderInterface;
class AppContextProvider implements ContextProviderInterface
{
public function getTenantId(): int|string|null { /* ... */ }
public function getWorkspace(): string { /* ... */ }
public function getActor(): array { /* ... */ }
public function getCorrelationId(): ?string { /* ... */ }
}
use Idaratech\EventBus\Contracts\EventBusInterface;
use Idaratech\EventBus\IntegrationEventFactory;
class SomeService
{
public function __construct(
private EventBusInterface $bus,
private IntegrationEventFactory $factory,
) {}
public function doWork(): void
{
$event = $this->factory->create(
eventType: 'user.created',
version: '1.0',
payload: ['user_id' => 123, 'email' => '[email protected]'],
);
$this->bus->publish('my-exchange', $event);
}
}
use Idaratech\EventBus\Contracts\EventHandlerInterface;
use Idaratech\EventBus\Contracts\IntegrationEventInterface;
class UserCreatedHandler implements EventHandlerInterface
{
public function supports(string $eventType): bool
{
return $eventType === 'user.created';
}
public function handle(IntegrationEventInterface $event): void
{
$payload = $event->getPayload();
// Process the event...
}
}