diff --git a/.gitignore b/.gitignore index a7f372d..e26945a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpstan.neon testbench.yaml vendor node_modules +.DS_Store diff --git a/routes/permanent-cache.php b/routes/permanent-cache.php new file mode 100644 index 0000000..d3f2207 --- /dev/null +++ b/routes/permanent-cache.php @@ -0,0 +1,27 @@ + $class */ + $class = decrypt($class, false); + + $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 80e9659..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); } @@ -196,7 +192,8 @@ final protected function value($default = null): mixed [$store, $cacheKey] = $this->store($this->getParameters()); return Cache::store($store)->get( - $cacheKey, $default, + $cacheKey, + $default, )?->value; } @@ -211,7 +208,8 @@ 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)) { @@ -309,4 +307,16 @@ public function addMarkers($value): mixed 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(); + + 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 97102f7..a35d080 100644 --- a/src/PermanentCacheServiceProvider.php +++ b/src/PermanentCacheServiceProvider.php @@ -15,8 +15,9 @@ public function configurePackage(Package $package): void $package->name('laravel-permanent-cache') ->hasCommands( PermanentCachesStatusCommand::class, - UpdatePermanentCachesCommand::class + UpdatePermanentCachesCommand::class, ) + ->hasRoute('permanent-cache') ->hasConfigFile(); } @@ -27,9 +28,10 @@ public function registeringPackage() public function bootingPackage() { - $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))) + $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))), ); } } 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;