Skip to content

Commit

Permalink
add Cached(Value) class
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Dec 8, 2023
1 parent 801877c commit 32c5636
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config/permanent-cache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [
'store' => env('PERMANENT_CACHE_STORE', 'file'),
'default_store' => env('DEFAULT_PERMANENT_CACHE_STORE', 'file'),
];
93 changes: 93 additions & 0 deletions src/Cached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Vormkracht10\PermanentCache;

use Illuminate\Support\Facades\Cache;
use ReflectionClass;
use ReflectionException;
use Vormkracht10\PermanentCache\Events\UpdatingPermanentCacheEvent;

/**
* @method mixed run()
*
* @template T
*/
abstract class Cached
{
/**
* The driver and identifier that will be used to cache this value.
* This value should be in the format 'driver:identifier'.
*
* @var string|null
*/
protected $cache = null;

/**
* The event that this cacher will listen for, this is optional
* as it can also be inferred by type hinting an argument
* in the run method.
*
* @var class-string<T>|null
*/
protected $event = null;

/**
* @param T $event
* @return void
*/
public final function handle($event = null): void
{
[$driver, $ident] = self::parseCacheString($this->cache);

Cache::driver($driver)->forever($ident,
$this->run($event),
);
}

public final static function get()
{
$cache = (new ReflectionClass(static::class))
->getProperty('cache')
->getDefaultValue();

[$driver, $ident] = self::parseCacheString($cache);

return Cache::driver($driver)->get($ident);
}

/**
* @return class-string<T>
*
* @throws ReflectionException
*/
public final static function getListenerEvent(): string
{
$reflection = new ReflectionClass(static::class);

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

return $concrete ?? ($reflection
->getMethod('run')
->getParameters()
[0] ?? null)
?->getType()
?->getName()
?? UpdatingPermanentCacheEvent::class;
}

/**
* @param string $cache
*
* @return array{string, string}
*/
private static function parseCacheString(string $cache): array
{
[$driver, $ident] = explode(':', $cache) + [1 => null];

if (is_null($ident)) {
[$driver, $ident] = [config('permanent-cache.default_store'), $driver];
}

return [$driver, $ident];
}
}
28 changes: 6 additions & 22 deletions src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ class PermanentCache
{
protected array $cachers = [];

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

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

Expand All @@ -26,26 +30,6 @@ public function caches(array $cachers): self
return $this;
}

/**
* @return class-string|false
*
* @throws \ReflectionException
* @throws \Exception
*/
protected function resolveEventType(string $class): string|false
{
if (! method_exists($class, 'run')) {
throw new \Exception('Every cacher needs a run method.');
}

return ((new \ReflectionClass($class))
->getMethod('run')
->getParameters()[0] ?? null)
?->getType()
?->getName()
?? false;
}

/**
* @return Collection<class-string, array<class-string>>
*/
Expand Down

0 comments on commit 32c5636

Please sign in to comment.