From a96b50baca86e7f7d70c0d9d48ed9e31df778a8c Mon Sep 17 00:00:00 2001 From: MHortulanus Date: Mon, 3 Jun 2024 11:44:19 +0200 Subject: [PATCH] Add example route --- .DS_Store | Bin 0 -> 6148 bytes src/CachesValue.php | 31 +++++++++++++++----------- src/PermanentCacheServiceProvider.php | 10 ++++++--- src/Routes/web.php | 7 ++++++ 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 .DS_Store create mode 100644 src/Routes/web.php diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..453d537005272042fb6656819891c59cce0e5735 GIT binary patch literal 6148 zcmeH~O-chn5QVEWkpVYZiQuLW;3hX{!+3&RK;nol5+?|{?wh%D<7GU8uewTQk^y%? zxj`0+JIkdQXxM(CF8c+U8UbH?P>!<22nq66w00|5U9D8?W=l?Z+nb9PF7{VS2 zkib79pp$mlF7Q!tw(fnNowW(&2F1+$iZx(p4=w@d*gkSzCw)Fy8-6*oxY{Zhas($cacheKey) + !$cache->has($cacheKey) ) { return static::updateAndGet($parameters ?? []); } @@ -189,14 +189,15 @@ 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"); } [$store, $cacheKey] = $this->store($this->getParameters()); return Cache::store($store)->get( - $cacheKey, $default, + $cacheKey, + $default, )?->value; } @@ -214,8 +215,8 @@ public function getShortName(): string /** @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); @@ -223,7 +224,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); @@ -266,7 +267,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, ':')); @@ -279,7 +280,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]; @@ -289,24 +290,28 @@ 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() + { } } diff --git a/src/PermanentCacheServiceProvider.php b/src/PermanentCacheServiceProvider.php index 97102f7..3a0cdb5 100644 --- a/src/PermanentCacheServiceProvider.php +++ b/src/PermanentCacheServiceProvider.php @@ -27,9 +27,13 @@ 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->loadRoutesFrom(__DIR__ . '/Routes/web.php'); + + $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/src/Routes/web.php b/src/Routes/web.php new file mode 100644 index 0000000..1154688 --- /dev/null +++ b/src/Routes/web.php @@ -0,0 +1,7 @@ +