Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Mar 10, 2025
1 parent 610754e commit 729b17a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 141 deletions.
5 changes: 2 additions & 3 deletions .idea/leantime-oss.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 6 additions & 138 deletions app/Core/Events/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Events\QueuedClosure;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\ReflectsClosures;
Expand Down Expand Up @@ -84,30 +85,6 @@ public static function findEventListeners(string $eventName, array $registry): a
return $matches;
}

public function add_event_listener( $eventName,
string|callable|object $handler,
int $priority = 10
): void {
$this->addEventListener($eventName, $handler, $priority);
}

//Support laravel event listeners
public function listen($events, $listener = null)
{
if ($events instanceof \Closure) {
return collect($this->firstClosureParameterTypes($events))
->each(function ($event) use ($events) {
$this->listen($event, $events);
});
} elseif ($events instanceof QueuedClosure) {
return collect($this->firstClosureParameterTypes($events->closure))
->each(function ($event) use ($events) {
$this->listen($event, $events->resolve());
});
} elseif ($listener instanceof QueuedClosure) {
$listener = $listener->resolve();
}

public function dispatch(
$event,
$payload = [],
Expand All @@ -122,7 +99,7 @@ public static function dispatch_filter(
mixed $payload = '',
mixed $available_params = [],
mixed $context = ''
): mixed {
): void {
$filtername = "$context.$filtername";

if (! in_array($filtername, self::$available_hooks['filters'])) {
Expand Down Expand Up @@ -218,22 +195,6 @@ private static function defineParams(mixed $paramAttr, string $eventName): array
// return $finalParams;
}

/**
* Parse the given event and payload and prepare them for dispatching.
*
* @param mixed $event
* @param mixed $payload
* @return array
*/
protected static function parseEventAndPayload($event, $payload)
{
if (is_object($event)) {
[$payload, $event] = [[$event], get_class($event)];
}

return [$event, Arr::wrap($payload)];
}

/**
* Executes all the handlers for a given hook
*
Expand Down Expand Up @@ -335,7 +296,8 @@ private static function executeHandlers(

}
} catch (\TypeError $e) {
error_log('TypeError in event handler: '.$e->getMessage());

Log::error('TypeError in event handler: '.$e->getMessage());
if (! isset($filteredPayload) && $index === 0) {
$filteredPayload = $payload;
}
Expand Down Expand Up @@ -582,8 +544,8 @@ public static function add_filter_listener(
$listenerSource = 'leantime'
): void {

if (! array_key_exists($filtername, $this->filterRegistry)) {
$this->filterRegistry[$filtername] = [];
if (! array_key_exists($filtername, self::$filterRegistry)) {
self::$filterRegistry[$filtername] = [];
}
self::$filterRegistry[$filtername][] = ['listener' => $listener, 'priority' => $priority, 'source' => $listenerSource];
}
Expand Down Expand Up @@ -753,11 +715,6 @@ private function sortByPriority(string $type, string $hookName): void
}
}

public static function getEventRegistry(): array
{
return self::$eventRegistry;
}

public function getEventRegistry(): array
{
return $this->eventRegistry;
Expand Down Expand Up @@ -844,37 +801,6 @@ public function until($event, $payload = [])
return $this->dispatch($event, $payload, true);
}

public function dispatch(
$event,
$payload = [],
$halt = false
) {

// When the given "event" is actually an object we will assume it is an event
// object and use the class as the event name and this event itself as the
// payload to the handler, which makes object based events quite simple.
[$isEventObject, $event, $payload] = [
is_object($event),
...$this->parseEventAndPayload($event, $payload),
];

// If the event is not intended to be dispatched unless the current database
// transaction is successful, we'll register a callback which will handle
// dispatching this event on the next successful DB transaction commit.
if ($isEventObject &&
$payload[0] instanceof ShouldDispatchAfterCommit &&
! is_null($transactions = $this->resolveTransactionManager())) {
$transactions->addCallback(
fn () => $this->invokeListeners($event, $payload, $halt)
);

return null;
}

return $this->invokeListeners($event, $payload, $halt);

}

protected function invokeListeners($event, $payload, $halt = false)
{
if ($this->shouldBroadcast($payload)) {
Expand Down Expand Up @@ -927,14 +853,6 @@ public function dispatchEvent(

}

public function dispatch_event(
string $eventName,
mixed $payload = [],
string $context = ''
): void {
$this->dispatchEvent($eventName, $payload, $context);
}

/**
* Dispatches a filter to manipulate a variable somewhere
*
Expand Down Expand Up @@ -962,14 +880,6 @@ public function dispatchFilter(
return $this->executeHandlers($matchedEvents, 'filters', $filtername, $payload, $available_params);
}

public function dispatch_filter(string $filtername,
mixed $payload = '',
mixed $available_params = [],
mixed $context = '')
{
return $this->dispatchFilter($filtername, $payload, $available_params, $context);
}

/**
* Parse the given event and payload and prepare them for dispatching.
*
Expand Down Expand Up @@ -1014,48 +924,6 @@ protected function broadcastEvent($event)
parent::broadcastEvent($event);
}

/**
* Finds event listeners by event names,
* Allows listeners with wildcards
*/
public function findEventListeners(string $eventName, array $registry): array
{
$matches = [];

foreach ($registry as $key => $value) {
preg_match_all('/\{RGX:(.*?):RGX\}/', $key, $regexMatches);

$key = strtr($key, [
...collect($regexMatches[0] ?? [])->mapWithKeys(fn ($match, $i) => [$match => "REGEX_MATCH_$i"])->toArray(),
'*' => 'RANDOM_STRING',
'?' => 'RANDOM_CHARACTER',
]);

// escape the non regex characters
$pattern = preg_quote($key, '/');

$pattern = strtr($pattern, [
'RANDOM_STRING' => '.*?', // 0 or more (lazy) - asterisk (*)
'RANDOM_CHARACTER' => '.', // 1 character - question mark (?)
...collect($regexMatches[1] ?? [])->mapWithKeys(fn ($match, $i) => ["REGEX_MATCH_$i" => $match])->toArray(),
]);

if (preg_match("/^$pattern$/", $eventName)) {

foreach ($value as &$listener) {
$listener['handler'] = $this->makeListener($listener['handler'], $listener['isWild'] ?? false);
}
//$value['handler'] = $this->makeListener($value['handler'], $value['isWild']);

$matches = array_merge($matches, $value);
}
}

return class_exists($eventName, false)
? $this->addInterfaceListeners($eventName, $matches)
: $matches;
}

/**
* Remove a set of listeners from the dispatcher.
*
Expand Down

0 comments on commit 729b17a

Please sign in to comment.