Skip to content

Commit

Permalink
improve event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 10, 2024
1 parent 240afc1 commit 9a90165
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vormkracht10\PermanentCache;

use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Event;
use SplObjectStorage;

Expand Down Expand Up @@ -36,15 +37,17 @@ public function caches($registeredCaches): self
$cache = array_key_first($parameters);
$parameters = array_shift($parameters);
} else {
$cache = \Arr::first($parameters);
$cache = Arr::first($parameters);
$parameters = [];
}
}

$cacheInstance = $this->app->make($cache, $parameters);

if ([] !== $events = $cacheInstance->getListenerEvents()) {
Event::listen($events, fn ($event) => $cacheInstance->handle($event));
foreach($events as $event) {
Event::listen($event, fn () => $cacheInstance->handle(new $event));
}
}

$this->caches[$cacheInstance] = $parameters;
Expand All @@ -57,7 +60,7 @@ public function caches($registeredCaches): self
/**
* Update all registered permanent caches
*/
public function update()
public function update(): void
{
foreach ($this->caches as $cache) {
$cache->update();
Expand Down

0 comments on commit 9a90165

Please sign in to comment.