Skip to content

Commit

Permalink
Merge pull request #6 from vormkracht10/feature/listening-for-multipl…
Browse files Browse the repository at this point in the history
…e-events

add the ability to listen for multiple events via the `$event` property.
  • Loading branch information
markvaneijk authored Jan 4, 2024
2 parents 6ab42d5 + 34b1fff commit 2c2653b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/Cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use ReflectionClass;

Expand Down Expand Up @@ -52,8 +53,9 @@ final public function handle($event = null): void
[$driver, $ident] = self::parseCacheString($this->store
?? throw new \Exception('The $store property in ['.static::class.'] must be overridden'),
);

Cache::driver($driver)->forever($ident,
/** @phpstan-ignore-next-line */
/** @phpstan-ignore-next-line */
$this->run($event),
);
}
Expand Down Expand Up @@ -111,20 +113,20 @@ public static function schedule($callback)
/**
* Get the event (if any) this cacher listens for.
*
* @return class-string<E>|null
* @return array<int, class-string<E>>
*/
final public static function getListenerEvent(): ?string
final public static function getListenerEvent(): array
{
$reflection = new ReflectionClass(static::class);

$concrete = $reflection->getProperty('event')->getDefaultValue();
$concrete = Arr::wrap($reflection->getProperty('event')->getDefaultValue());

/** @phpstan-ignore-next-line */
return $concrete ?? ($reflection
return $concrete ?: Arr::wrap(($reflection
->getMethod('run')
->getParameters()[0] ?? null)
?->getType()
?->getName();
?->getName());
}

/**
Expand Down
26 changes: 7 additions & 19 deletions src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,32 @@ class PermanentCache
*/
protected array $cachers = [];

/**
* @var array<class-string, array<class-string<Cached>>>
*/
protected array $static = [];

/**
* @param array<int, class-string<Cached>> $cachers
* @return $this
*/
public function caches(array $cachers): self
{
foreach ($cachers as $cacher) {
$event = $cacher::getListenerEvent();

if (is_null($event)) {
$static[] = $cacher;

continue;
}
$events = $cacher::getListenerEvent();

$resolved[$event][] = $cacher;
$resolved[$cacher] = $events;

Event::listen($event, $cacher);
Event::listen($events, $cacher);
}

$this->cachers = array_merge($this->cachers, $resolved ?? []);
$this->static = array_merge($this->static, $static ?? []);

return $this;
}

public function staticCaches(): array
public function configuredCaches(): array
{
return $this->static;
return $this->cachers;
}

public function configuredCaches(): array
public function staticCaches(): array
{
return $this->cachers;
return array_filter($this->cachers);
}
}

0 comments on commit 2c2653b

Please sign in to comment.