jobmetric / laravel-event-system
This is a event system management package for Laravel that you can use in your projects.
Installs: 50
Dependents: 1
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/jobmetric/laravel-event-system
Requires
- php: >=8.1
- jobmetric/laravel-package-core: ^1.33
- laravel/framework: >=9.19
README
Laravel Event System
Laravel Event System is a flexible and elegant package designed to simplify dynamic event management. It provides a layer on top of Laravel’s native event system, allowing for the registration and removal of events at runtime — perfect for modular applications and plugin-based systems.
📦 Installation
Install the package via Composer:
composer require jobmetric/laravel-event-system
Then publish the migration and run it:
php artisan migrate
⚙️ Configuration
The default configuration can be published using:
php artisan vendor:publish --tag=event-system-config
Config Options
return [ "cache_time" => env("EVENT_SYSTEM_CACHE_TIME", 60), "cache_key" => env("EVENT_SYSTEM_CACHE_KEY", "event_system_cache"), "tables" => [ 'event' => 'events', ], ];
🚀 Usage
You can dynamically register or remove events at runtime using the helper functions:
Register an Event
addEventSystem('user.created', App\Events\UserCreated::class, App\Listeners\SendWelcomeEmail::class, 'Triggered when a new user registers');
Remove an Event
removeEventSystem('user.created');
Event Bus & Registry
The package ships with a small registry and bus to dispatch domain events by stable keys instead of hard‑coding class names.
use JobMetric\EventSystem\Contracts\DomainEvent; use JobMetric\EventSystem\Support\EventRegistry; use JobMetric\EventSystem\Facades\EventBus; class UserRegistered implements DomainEvent { public function __construct(public int $userId) {} public static function key(): string { return 'user.registered'; } } $registry = app(EventRegistry::class); $registry->register(UserRegistered::class); // Later: dispatch by the stable key (use helper below if you prefer) EventBus::dispatchByKey(UserRegistered::key(), 7); // Or dispatch a concrete event instance directly EventBus::dispatch(new UserRegistered(7));
EventRegistry::forKey($key)resolves the event class for a key.EventRegistry::keyFor($event)returns the key for a registered event class or instance.
Helper Functions
addEventSystem($name, $event, $listener, $priority = 0, $description = null, $status = true)registers an event/listener row.deleteEventSystem($name)removes a row by name.eventKey($key, ...$payload)dispatches an event via its registry key usingEventBus::dispatchByKey. The helper forwards the payload as a single array argument to the event constructor, so shape your constructor accordingly (e.g.__construct(array $data)).
🎧 System Events
The package also emits its own events:
| Event | Description |
|---|---|
EventSystemStoreEvent |
Dispatched after an event has been registered |
EventSystemDeletingEvent |
Dispatched before an event is removed |
EventSystemDeletedEvent |
Dispatched after an event has been removed |
Contributing
Thank you for considering contributing to the Laravel Event System! The contribution guide can be found in the CONTRIBUTING.md.
License
The MIT License (MIT). Please see License File for more information.