<?php
namespace Yesf;
class Plugin {
protected static $plugins = [];
public static function register(string $event, callable $callback) {
if (!isset(self::$plugins[$event])) {
self::$plugins[$event] = [];
}
self::$plugins[$event][] = $callback;
}
public static function clear(string $event = '') {
if (empty($event)) {
self::$plugins = [];
} else {
self::$plugins[$event] = [];
}
}
public static function trigger(string $event, $data = []) {
$result = null;
if (isset(self::$plugins[$event])) {
foreach (self::$plugins[$event] as $callback) {
$result = $callback(...$data);
if ($result !== null) {
break;
}
}
}
return $result;
}
}