From ea67f6dfc3493d24690827f3699e96968c0abe14 Mon Sep 17 00:00:00 2001 From: David <75451291+david-d-h@users.noreply.github.com> Date: Sat, 15 Jun 2024 15:54:05 +0200 Subject: [PATCH] finish route that allows for updating a permanent cache --- routes/api.php | 14 --------- routes/permanent-cache.php | 27 +++++++++++++++++ src/CachesValue.php | 42 ++++++++++++--------------- src/PermanentCache.php | 2 +- src/PermanentCacheServiceProvider.php | 9 +++--- tests/TestCase.php | 1 - 6 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 routes/api.php create mode 100644 routes/permanent-cache.php diff --git a/routes/api.php b/routes/api.php deleted file mode 100644 index 113bbd5..0000000 --- a/routes/api.php +++ /dev/null @@ -1,14 +0,0 @@ -all())); - extract($vars); - [$class] = $vars; - - $class = new \ReflectionClass($class); - $staticClass = $class->getName(); - - return $staticClass::updateAndGet($parameters); -})->name('permanent-cache.update'); diff --git a/routes/permanent-cache.php b/routes/permanent-cache.php new file mode 100644 index 0000000..d5f0b30 --- /dev/null +++ b/routes/permanent-cache.php @@ -0,0 +1,27 @@ + $class */ + $class = decrypt($class); + + $parameters = ($parameters = $request->query('parameters')) + ? Arr::wrap(decrypt($parameters)) + : []; + + if ( + ! class_exists($class) || + ! in_array(CachesValue::class, class_uses_recursive($class)) + ) { + return response()->json([ + 'error' => 'the given class does not exist or does not use the ['.CachesValue::class.'] trait', + ], 400); + } + + $data = $class::updateAndGet($parameters ?? []); + + return response()->json(compact('data')); +})->name('permanent-cache.update'); diff --git a/src/CachesValue.php b/src/CachesValue.php index d597b20..9e8b0f8 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -127,9 +127,7 @@ final public static function update($parameters = []) { $instance = app()->make(static::class, $parameters); - dispatch( - $instance - ); + dispatch($instance); } /** @@ -139,9 +137,7 @@ final public static function updateAndGet($parameters = []) { $instance = app()->make(static::class, $parameters); - dispatch( - $instance - )->onConnection('sync'); + dispatch($instance)->onConnection('sync'); return static::get($parameters); } @@ -161,7 +157,7 @@ final public static function get($parameters = [], $default = null, bool $update if ( $update || - !$cache->has($cacheKey) + ! $cache->has($cacheKey) ) { return static::updateAndGet($parameters ?? []); } @@ -189,7 +185,7 @@ final public function getMeta($parameters = []): mixed */ final protected function value($default = null): mixed { - if (is_subclass_of(static::class, CachedComponent::class) && !is_null($default)) { + if (is_subclass_of(static::class, CachedComponent::class) && ! is_null($default)) { throw new \Exception("A cached component can't have a default return value"); } @@ -212,11 +208,12 @@ public function getShortName(): string } /// Default implementation for the `\Scheduled::schedule` method. - /** @param CallbackEvent $callback */ + + /** @param CallbackEvent $callback */ public static function schedule($callback) { - if (!is_a(static::class, Scheduled::class, true)) { - throw new \Exception("Can't schedule a cacher that does not implement the [" . Scheduled::class . '] interface'); + if (! is_a(static::class, Scheduled::class, true)) { + throw new \Exception("Can't schedule a cacher that does not implement the [".Scheduled::class.'] interface'); } $reflection = new ReflectionClass(static::class); @@ -224,7 +221,7 @@ public static function schedule($callback) $concrete = $reflection->getProperty('expression')->getDefaultValue(); if (is_null($concrete)) { - throw new \Exception('Either the Cached::$expression property or the [' . __METHOD__ . '] method must be overridden by the user.'); + throw new \Exception('Either the Cached::$expression property or the ['.__METHOD__.'] method must be overridden by the user.'); } $callback->cron($concrete); @@ -267,7 +264,7 @@ public static function getCacheKey(?array $parameters = [], ?string $store = nul ->getDefaultValue(); if ( - !is_null($store) && + ! is_null($store) && strpos($store, ':') ) { $cacheStore = substr($store, 0, strpos($store, ':')); @@ -280,7 +277,7 @@ public static function getCacheKey(?array $parameters = [], ?string $store = nul $cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(Str::snake($class))); if ($parameters) { - $cacheKey .= ':' . http_build_query($parameters); + $cacheKey .= ':'.http_build_query($parameters); } return [$cacheStore, $cacheKey]; @@ -290,36 +287,35 @@ public function getMarker(array $parameters = [], $close = false): string { [$cacheStore, $cacheKey] = $this::store($parameters ?? $this->getParameters()); - $marker = $cacheStore . ':' . $cacheKey; + $marker = $cacheStore.':'.$cacheKey; if (config('permanent-cache.components.markers.hash')) { $marker = md5($marker); } - return ''; + return ''; } public function addMarkers($value): mixed { if ( - !config('permanent-cache.components.markers.enabled') || - !is_subclass_of($this, CachedComponent::class) + ! config('permanent-cache.components.markers.enabled') || + ! is_subclass_of($this, CachedComponent::class) ) { return $value; } - return $this->getMarker() . $value . $this->getMarker(close: true); + return $this->getMarker().$value.$this->getMarker(close: true); } - public function getRefreshRoute() { $class = get_class($this); $props = collect((new ReflectionClass($this))->getProperties(\ReflectionProperty::IS_PUBLIC)) - ->where('class', __CLASS__) - ->mapWithKeys(fn ($prop) => [$prop->name => $this->{$prop->name}]) - ->toArray(); + ->where('class', __CLASS__) + ->mapWithKeys(fn ($prop) => [$prop->name => $this->{$prop->name}]) + ->toArray(); return route('permanent-cache.update', ['data' => encrypt([$class, $props])]); } diff --git a/src/PermanentCache.php b/src/PermanentCache.php index 265b403..4cfa216 100755 --- a/src/PermanentCache.php +++ b/src/PermanentCache.php @@ -45,7 +45,7 @@ public function caches($registeredCaches): self $cacheInstance = $this->app->make($cache, $parameters); if ([] !== $events = $cacheInstance->getListenerEvents()) { - foreach($events as $event) { + foreach ($events as $event) { Event::listen($event, fn ($e) => $cacheInstance->handle($e)); } } diff --git a/src/PermanentCacheServiceProvider.php b/src/PermanentCacheServiceProvider.php index ba41104..a35d080 100644 --- a/src/PermanentCacheServiceProvider.php +++ b/src/PermanentCacheServiceProvider.php @@ -15,9 +15,9 @@ public function configurePackage(Package $package): void $package->name('laravel-permanent-cache') ->hasCommands( PermanentCachesStatusCommand::class, - UpdatePermanentCachesCommand::class + UpdatePermanentCachesCommand::class, ) - ->hasRoute('api') + ->hasRoute('permanent-cache') ->hasConfigFile(); } @@ -28,11 +28,10 @@ public function registeringPackage() public function bootingPackage() { - $this->callAfterResolving( - Schedule::class, + $this->callAfterResolving(Schedule::class, fn (Schedule $schedule) => collect(Facades\PermanentCache::configuredCaches()) ->filter(fn ($cacher) => is_a($cacher, Scheduled::class)) - ->each(fn ($cacher) => $cacher->schedule($schedule->job($cacher))) + ->each(fn ($cacher) => $cacher->schedule($schedule->job($cacher))), ); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index f807998..1ce7e92 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,6 @@ namespace Vormkracht10\PermanentCache\Tests; -use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; use Vormkracht10\PermanentCache\PermanentCacheServiceProvider;