Events
The Events
class provides a simple event emitter pattern, allowing different parts of an application to communicate with each other without being tightly coupled.
Constructor
__construct
__construct(): Events
Creates a new Events instance for event management.
Example
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
'user' => 'your_username',
'pass' => 'your_password'
],
'storage_path' => __DIR__ . '/storage'
];
$schoolKit = SchoolKit::start($config);
$events = $schoolKit->events();
Methods
on
on(string $event, callable $callback): void
Registers a listener for a specific event.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$event |
string |
Yes | The name of the event |
$callback |
callable |
Yes | The function to execute when the event is emitted |
Example
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
'user' => 'your_username',
'pass' => 'your_password'
],
'storage_path' => __DIR__ . '/storage'
];
$schoolKit = SchoolKit::start($config);
$events = $schoolKit->events();
$events->on('user.created', function($payload) {
echo "New user created: " . $payload['name'];
});
emit
emit(string $event, array $payload = []): void
Emits an event, calling all registered listeners.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$event |
string |
Yes | The name of the event |
$payload |
array |
No | Data to pass to the event listeners |
Example
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
'user' => 'your_username',
'pass' => 'your_password'
],
'storage_path' => __DIR__ . '/storage'
];
$schoolKit = SchoolKit::start($config);
$events = $schoolKit->events();
// ... register listener
$events->emit('user.created', ['name' => 'John Doe']);
off
off(string $event, ?callable $callback = null): void
Removes a listener for a specific event. If no callback is provided, all listeners for the event are removed.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$event |
string |
Yes | The name of the event |
$callback |
callable|null |
No | The specific listener to remove |
Example
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
'user' => 'your_username',
'pass' => 'your_password'
],
'storage_path' => __DIR__ . '/storage'
];
$schoolKit = SchoolKit::start($config);
$events = $schoolKit->events();
$listener = function($payload) { /* ... */ };
$events->on('user.created', $listener);
// Remove the specific listener
$events->off('user.created', $listener);
// Remove all listeners for the event
$events->off('user.created');
getListeners
getListeners(string $event): array
Gets all registered listeners for a specific event.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$event |
string |
Yes | The name of the event |
Returns
An array of callables.
Example
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
'user' => 'your_username',
'pass' => 'your_password'
],
'storage_path' => __DIR__ . '/storage'
];
$schoolKit = SchoolKit::start($config);
$events = $schoolKit->events();
$events->on('user.created', function() {});
$listeners = $events->getListeners('user.created');
// count($listeners) will be 1
hasListeners
hasListeners(string $event): bool
Checks if an event has any registered listeners.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$event |
string |
Yes | The name of the event |
Returns
true
if listeners exist, false
otherwise.
Example
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
'user' => 'your_username',
'pass' => 'your_password'
],
'storage_path' => __DIR__ . '/storage'
];
$schoolKit = SchoolKit::start($config);
$events = $schoolKit->events();
$events->on('user.created', function() {});
$hasListeners = $events->hasListeners('user.created'); // true