You've already forked Php-Template
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
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:
15
src/Annotations/Events/ListensFor.php
Normal file
15
src/Annotations/Events/ListensFor.php
Normal 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
135
src/Events/Dispatcher.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/Events/Listeners/Database/Connected.php
Normal file
28
src/Events/Listeners/Database/Connected.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/Events/Listeners/Listener.php
Normal file
9
src/Events/Listeners/Listener.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Siteworxpro\App\Events\Listeners;
|
||||||
|
|
||||||
|
abstract class Listener implements ListenerInterface
|
||||||
|
{
|
||||||
|
}
|
||||||
10
src/Events/Listeners/ListenerInterface.php
Normal file
10
src/Events/Listeners/ListenerInterface.php
Normal 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;
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ use Illuminate\Container\Container;
|
|||||||
use Illuminate\Database\Capsule\Manager;
|
use Illuminate\Database\Capsule\Manager;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworx\Config\Config as SWConfig;
|
use Siteworx\Config\Config as SWConfig;
|
||||||
|
use Siteworxpro\App\Events\Dispatcher;
|
||||||
use Siteworxpro\App\Services\Facade;
|
use Siteworxpro\App\Services\Facade;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
||||||
@@ -69,6 +70,7 @@ class Kernel
|
|||||||
private static function bootModelCapsule(): void
|
private static function bootModelCapsule(): void
|
||||||
{
|
{
|
||||||
$capsule = new Manager();
|
$capsule = new Manager();
|
||||||
|
$capsule->setEventDispatcher(new Dispatcher());
|
||||||
$capsule->addConnection(Config::get('db'));
|
$capsule->addConnection(Config::get('db'));
|
||||||
$capsule->setAsGlobal();
|
$capsule->setAsGlobal();
|
||||||
$capsule->bootEloquent();
|
$capsule->bootEloquent();
|
||||||
|
|||||||
Reference in New Issue
Block a user