HummingBirdAnimeClient/src/Ion/Event.php

49 lines
1.1 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\Ion;
/**
* A basic event handler
*/
class Event
{
2023-03-16 13:03:48 -04:00
protected static array $eventMap = [];
/**
* Subscribe to an event
*/
public static function on(string $eventName, callable $handler): void
{
if ( ! array_key_exists($eventName, static::$eventMap))
{
static::$eventMap[$eventName] = [];
}
static::$eventMap[$eventName][] = $handler;
}
/**
* Fire off an event
*/
public static function emit(string $eventName, array $args = []): void
{
// Call each subscriber with the provided arguments
if (array_key_exists($eventName, static::$eventMap))
{
2020-12-10 15:59:37 -05:00
array_walk(static::$eventMap[$eventName], static fn ($fn) => $fn(...$args));
}
}
}