feat: implement custom event dispatcher and listener system
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 58s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 1m9s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 59s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 1m0s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 1m11s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m34s

This commit is contained in:
2025-11-11 10:49:42 -05:00
parent 7d0b00fb89
commit d7a2faf3a8
6 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Annotations\Events;
use Attribute;
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
readonly class ListensFor
{
public function __construct(public string $eventClass)
{
}
}

135
src/Events/Dispatcher.php Normal file
View File

@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Events;
use Illuminate\Support\Collection;
use Siteworxpro\App\Annotations\Events\ListensFor;
/**
* Class Dispatcher
*
* A custom event dispatcher that automatically registers event listeners
* based on the ListensFor attribute.
*
* @package Siteworxpro\App\Events
*/
class Dispatcher implements \Illuminate\Contracts\Events\Dispatcher
{
/**
* @var array $listeners Registered event listeners
*/
private array $listeners = [];
/**
* @var Collection $pushed Pushed events collection
*/
private Collection $pushed;
/**
* @var string LISTENERS_NAMESPACE The namespace where listeners are located
*/
private const string LISTENERS_NAMESPACE = 'Siteworxpro\\App\\Events\\Listeners\\';
public function __construct()
{
$this->pushed = new Collection();
$this->registerListeners();
}
private function registerListeners(): void
{
// traverse the Listeners directory and register all listeners
$listenersPath = __DIR__ . '/Listeners';
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($listenersPath));
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$relativePath = str_replace($listenersPath . '/', '', $file->getPathname());
$className = self::LISTENERS_NAMESPACE . str_replace(['/', '.php'], ['\\', ''], $relativePath);
if (class_exists($className)) {
$reflectionClass = new \ReflectionClass($className);
$attributes = $reflectionClass->getAttributes(ListensFor::class);
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$eventClass = $instance->eventClass;
$this->listen($eventClass, new $className());
}
}
}
}
}
public function listen($events, $listener = null): void
{
$this->listeners[$events][] = $listener;
}
public function hasListeners($eventName): bool
{
return isset($this->listeners[$eventName]) && !empty($this->listeners[$eventName]);
}
public function subscribe($subscriber): void
{
$this->listeners = array_merge($this->listeners, (array) $subscriber);
}
public function until($event, $payload = [])
{
return $this->dispatch($event, $payload, true);
}
public function dispatch($event, $payload = [], $halt = false): array|null
{
if (is_object($event)) {
$eventClass = get_class($event);
} else {
$eventClass = $event;
}
$listeners = $this->listeners[$eventClass] ?? null;
if ($listeners === null) {
return null;
}
$responses = [];
foreach ($listeners as $listener) {
$response = $listener($event, $payload);
$responses[] = $response;
if ($halt && $response !== null) {
return $response;
}
}
return $responses;
}
public function push($event, $payload = []): void
{
$this->pushed->put($event, $payload);
}
public function flush($event): void
{
if ($this->pushed->has($event)) {
$payload = $this->pushed->get($event);
$this->dispatch($event, $payload);
$this->pushed->forget([$event]);
}
}
public function forget($event): void
{
$this->pushed->forget([$event]);
}
public function forgetPushed(): void
{
$this->pushed = new Collection();
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Events\Listeners\Database;
use Illuminate\Database\Events\ConnectionEstablished;
use Illuminate\Database\Events\ConnectionEvent;
use Siteworxpro\App\Annotations\Events\ListensFor;
use Siteworxpro\App\Events\Listeners\Listener;
use Siteworxpro\App\Services\Facades\Logger;
#[ListensFor(ConnectionEstablished::class)]
class Connected extends Listener
{
/**
* @param ConnectionEvent $event
* @param array $payload
* @return null
*/
public function __invoke($event, array $payload = []): null
{
Logger::info("Database connection event", [get_class($event), $event->connectionName]);
return null;
}
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Events\Listeners;
abstract class Listener implements ListenerInterface
{
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Events\Listeners;
interface ListenerInterface
{
public function __invoke(mixed $event, array $payload = []): mixed;
}

View File

@@ -6,6 +6,7 @@ use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager;
use Illuminate\Support\ServiceProvider;
use Siteworx\Config\Config as SWConfig;
use Siteworxpro\App\Events\Dispatcher;
use Siteworxpro\App\Services\Facade;
use Siteworxpro\App\Services\Facades\Config;
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
@@ -69,6 +70,7 @@ class Kernel
private static function bootModelCapsule(): void
{
$capsule = new Manager();
$capsule->setEventDispatcher(new Dispatcher());
$capsule->addConnection(Config::get('db'));
$capsule->setAsGlobal();
$capsule->bootEloquent();