AvocadoAmber/AvocadoEdition_Light/classes/event_handler.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2024-09-28 13:03:15 +09:00
<?php
/**
* THIS MODULE PROHIBITS DISTRIBUTION TO OTHERS WITHOUT AUTHOR'S PERMISSION.
* Base Module (minimum support version)
* @author arcturus (https://info.drk.st/about contact@drk.st)
*/
include_once __DIR__ . "/event.php";
class EventHandler
{
protected static $eventHandlers = [];
protected static $performanceStopwatch = [];
protected static $createdAt;
public function __construct()
{
$this->createdAt = microtime(true);
}
protected static function addStopwatch($key)
{
if (defined("__IS_DEBUG__")) {
if (count(self::$performanceStopwatch) > 0) {
$prev = end(self::$performanceStopwatch);
self::$performanceStopwatch[$key] = microtime(true) - self::$createdAt - $prev;
} else {
self::$performanceStopwatch[$key] = microtime(true) - self::$createdAt;
}
}
}
public static function addStopwatchWithCallStack($key)
{
if (defined("__IS_DEBUG__")) {
if ($key === 0)
$key = "start";
else if ($key === 1)
$key = "end";
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$cf = $backtrace[1]['function'] ?? 'global scope';
self::addStopwatch("{$cf} {$key}");
}
}
public static function addEventHandler($event, $callback, $priority = 10)
{
if (!isset(self::$eventHandlers[$event])) {
self::$eventHandlers[$event] = [];
}
self::$eventHandlers[$event][] = new Event($callback, $priority);
}
public static function triggerEvent($event, ...$data)
{
if (isset(self::$eventHandlers[$event])) {
foreach (self::$eventHandlers[$event] as $callback) {
$callback->getAction($data);
}
}
}
}