Skip to content

Commit

Permalink
update event dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Nov 21, 2024
1 parent 1376aa5 commit 01f826c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 172 deletions.
20 changes: 11 additions & 9 deletions app/Core/Events/DispatchesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,54 @@

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Facades\Event as EventDispatcher;
use Illuminate\Support\Facades\Event;

trait DispatchesEvents
{
private static string $event_context = '';

/**
* dispatches an event with context
* Dispatches an event with context (WordPress style)
*
*
* @throws BindingResolutionException
*/
public static function dispatch_event(string $hook, mixed $available_params = [], string|int|null $function = null): void
{
EventDispatcher::dispatch_event($hook, $available_params, static::getEventContext($function));
Event::dispatch_event($hook, $available_params, static::getEventContext($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
public static function dispatchEvent($event): void
{
EventDispatcher::dispatch_event($hook, $available_params, static::getEventContext($function));
if (is_string($event)) {
Event::dispatch($event);
}
}

public static function dispatch(mixed $event, mixed $available_params = [], string|int|null $function = null): void
{
EventDispatcher::dispatchEvent($hook, $available_params, static::getEventContext($function));
Event::dispatchEvent($hook, $available_params, static::getEventContext($function));

}

/**
* dispatches a filter with context
* Dispatches a filter with context
*
*
* @throws BindingResolutionException
*/
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::getEventContext($function));
return Event::dispatch_filter($hook, $payload, $available_params, static::getEventContext($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::getEventContext($function));
return Event::dispatch_filter($hook, $payload, $available_params, static::getEventContext($function));
}

/**
Expand Down
165 changes: 5 additions & 160 deletions app/Core/Events/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

namespace Leantime\Core\Events;

use Illuminate\Cache\Events\CacheEvent;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
use Illuminate\Events\QueuedClosure;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\ReflectsClosures;
use Leantime\Core\Configuration\Environment;
use Symfony\Component\HttpKernel\Event\ViewEvent;

/**
* EventDispatcher class - Handles all events and filters
Expand Down Expand Up @@ -48,7 +45,6 @@ class EventDispatcher extends \Illuminate\Events\Dispatcher implements Dispatche
'events' => [],
];


/**
* Adds an event listener to be registered
*/
Expand Down Expand Up @@ -180,7 +176,7 @@ public function discoverListeners(): void
$discovered = true;
}

public function getDomainPaths()
protected function getDomainPaths()
{

$domainModules = collect(glob(APP_ROOT.'/app/Domain'.'/*', GLOB_ONLYDIR));
Expand Down Expand Up @@ -323,24 +319,6 @@ private function executeHandlers(

//Regular event
if ($isEvent) {

//Some odd events
// if(is_array($eventPayload[0])){
//
// if( collect($eventPayload[0])->first() instanceof MessageLogged) {
// $eventPayload[0] = $eventPayload[0][0];
// }
//
// if( collect($eventPayload[0])->first() instanceof CacheEvent) {
// $eventPayload[0] = $eventPayload[0][0];
// }
//
// if( collect($eventPayload[0])->first() instanceof ViewEvent) {
// $eventPayload[0] = $eventPayload[0][0];
// }
//
// }

$handler(event: $hookName, payload: $eventPayload[0]);

continue;
Expand All @@ -359,105 +337,7 @@ private function executeHandlers(

return null;

// class with handle function
// An actual object was passed into the listener
// if (is_object($handler) && method_exists($handler, 'handle')) {
//
// //Regular event
// if ($isEvent) {
// $handler->handle($eventPayload);
// continue;
// }
//
// //Filter event
// $eventPayload["payload"] = $handler->handle($eventPayload);
// continue;
// }
//
// // anonymous functions was passed into listener
// if (is_callable($handler)) {
//
// $filteredPayload = $this->handleAnonListeners($isEvent, $handler, $eventName, $filteredPayload, $additionalParameters);
//
// continue;
// }
//
//
//
//
//
//
//
//
//
// if (
// in_array(true, [
// // function name as string
// is_string($handler) && function_exists($handler),
// // class instance with method name
// is_array($handler) && is_object($handler[0]) && method_exists($handler[0], $handler[1]),
// // class name with method name
// is_array($handler) && class_exists($handler[0]) && method_exists($handler[0], $handler[1]),
// ])
// ) {
// if ($isEvent) {
// call_user_func_array($handler, [$payload]);
//
// continue;
// }
//
// $filteredPayload = handleAnonListeners(
// $handler,
// [
// $index == 0 ? $payload : $filteredPayload,
// $available_params,
// ]
// );
//
// continue;
// }
// }

}

// protected function handleClassListeners($isEvent, $handler, $eventName, $payload, $additionalParameters)
// {
//
// $eventPayload = [
// 'eventName' => $eventName,
// 'payload' => $payload,
// 'availableParams' => $additionalParameters,
// ];
//
// //Is a regular event:
// if ($isEvent) {
// return $handler->handle($eventPayload);
// }
//
// //Is a filter event:
// return $handler->handle($eventPayload);
// }

// protected function handleAnonListeners($isEvent, $handler, $eventName, $payload, $additionalParameters)
// {
//
// $eventPayload = [
// 'eventName' => $eventName,
// 'payload' => $payload,
// 'availableParams' => $additionalParameters,
// ];
//
// //Is a regular event:
// if ($isEvent) {
// return $handler(event: $eventName, payload: $eventPayload);
//
// }
//
// return $handler(event: $eventName, payload: $eventPayload
// );
// }

// protected function handleStringListeners($isEvent, $handler, $eventName, $payload, $additionalParameters) {}
}

public function getEventRegistry(): array
{
Expand Down Expand Up @@ -596,30 +476,6 @@ protected function invokeListeners($event, $payload, $halt = false)

return $this->executeHandlers($matchedEvents, 'events', $event, $payload, [], $halt);

/*
foreach ($this->getListeners($event) as $listener) {
$response = $listener($event, $payload);
// If a response is returned from the listener and event halting is enabled
// we will just return this response, and not call the rest of the event
// listeners. Otherwise we will add the response on the response list.
if ($halt && ! is_null($response)) {
return $response;
}
// If a boolean false is returned from a listener, we will stop propagating
// the event to any further listeners down in the chain, else we keep on
// looping through the listeners and firing every one in our sequence.
if ($response === false) {
break;
}
$responses[] = $response;
}
return $halt ? null : $responses;
*/
}

/**
Expand Down Expand Up @@ -655,18 +511,6 @@ public function dispatchEvent(

$this->dispatch($eventName, [$payload]);

// if (! in_array($eventName, self::$available_hooks['events'])) {
// self::$available_hooks['events'][] = $eventName;
// }
//
// $matchedEvents = self::findEventListeners($eventName, self::$eventRegistry);
// if (count($matchedEvents) == 0) {
// return;
// }
//
// $payload = self::defineParams($payload);
//
// self::executeHandlers($matchedEvents, 'events', $eventName, $payload);
}

public function dispatch_event(
Expand Down Expand Up @@ -704,10 +548,11 @@ public function dispatchFilter(
return $this->executeHandlers($matchedEvents, 'filters', $filtername, $payload, $available_params);
}

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

Expand Down
2 changes: 1 addition & 1 deletion app/Core/Http/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function sendRequestThroughRouter($request)
payload: [],
function: 'handle',
))
->then(fn () => Frontcontroller::dispatch_request($request))
->then($this->dispatchToRouter())
);

return $response;
Expand Down
9 changes: 7 additions & 2 deletions app/Core/Providers/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ public function register()
{

$this->app->singleton('events', function ($app) {
return new Core\Events\EventDispatcher;
return (new Core\Events\EventDispatcher($app))->setQueueResolver(function () use ($app) {
return $app->make(QueueFactoryContract::class);
})->setTransactionManagerResolver(function () use ($app) {
return $app->bound('db.transactions')
? $app->make('db.transactions')
: null;
});
});

$this->booting(function () {

//Core\Events\EventDispatcher::discover_listeners();
Expand Down

0 comments on commit 01f826c

Please sign in to comment.