OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)
This commit is contained in:
90
app/Core/Events/DispatchesEvents.php
Normal file
90
app/Core/Events/DispatchesEvents.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Core\Events;
|
||||
|
||||
trait DispatchesEvents
|
||||
{
|
||||
private static string $event_context = '';
|
||||
|
||||
/**
|
||||
* dispatches an event with context
|
||||
*/
|
||||
public static function dispatch_event(string $hook, mixed $available_params = [], string|int|null $function = null): void
|
||||
{
|
||||
EventDispatcher::dispatch_event($hook, $available_params, static::get_event_context($function));
|
||||
}
|
||||
|
||||
// The new dispatchEvent method is below. We're keeping both for backwards compatibility until v4.0
|
||||
// Temporary for backwards compatibility
|
||||
public static function dispatchEvent(string $hook, mixed $available_params = [], string|int|null $function = null): void
|
||||
{
|
||||
EventDispatcher::dispatch_event($hook, $available_params, static::get_event_context($function));
|
||||
}
|
||||
|
||||
/**
|
||||
* dispatches a filter with context
|
||||
*/
|
||||
public static function dispatch_filter(string $hook, mixed $payload, mixed $available_params = [], string|int|null $function = null): mixed
|
||||
{
|
||||
return EventDispatcher::dispatch_filter($hook, $payload, $available_params, static::get_event_context($function));
|
||||
}
|
||||
|
||||
// The new dispatchEvent method is below. We're keeping both for backwards compatibility until v4.0
|
||||
// Temporary for backwards compatibility
|
||||
public static function dispatchFilter(string $hook, mixed $payload, mixed $available_params = [], string|int|null $function = null): mixed
|
||||
{
|
||||
return EventDispatcher::dispatch_filter($hook, $payload, $available_params, static::get_event_context($function));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the context of the event
|
||||
*/
|
||||
protected static function get_event_context($function): string
|
||||
{
|
||||
if (empty(self::$event_context)) {
|
||||
self::$event_context = static::set_class_context();
|
||||
}
|
||||
|
||||
$eventContext = self::$event_context.'.';
|
||||
|
||||
if (! empty($function) && is_string($function) && ! is_numeric($function)) {
|
||||
|
||||
$function = $function;
|
||||
|
||||
// If context starts with leantime, the full context was provided by caller
|
||||
if (str_starts_with($function, 'leantime.')) {
|
||||
$eventContext = '';
|
||||
}
|
||||
|
||||
} else {
|
||||
$function = static::get_function_context(is_numeric($function) ? (int) $function : null);
|
||||
}
|
||||
|
||||
return $eventContext.$function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the class Context based on path, this uses the same method as the autoloader
|
||||
* Helps create unique strings for events/filters
|
||||
*/
|
||||
protected static function set_class_context(): string
|
||||
{
|
||||
return str_replace('\\', '.', strtolower(static::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the caller function name.
|
||||
*
|
||||
* Uses debug_backtrace with limited depth and no args instead of
|
||||
* Exception::getTrace() to avoid the overhead of creating a full
|
||||
* exception object on every event dispatch (~60 times per request).
|
||||
*/
|
||||
protected static function get_function_context(?int $functionInt = null): string
|
||||
{
|
||||
$tracePointer = is_int($functionInt) ? $functionInt : 3;
|
||||
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $tracePointer + 1);
|
||||
|
||||
return $trace[$tracePointer]['function'] ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user