Skip to content

Commit

Permalink
Add example route
Browse files Browse the repository at this point in the history
  • Loading branch information
arduinomaster22 committed Jun 3, 2024
1 parent 034d895 commit a96b50b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
Binary file added .DS_Store
Binary file not shown.
31 changes: 18 additions & 13 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ final public static function get($parameters = [], $default = null, bool $update

if (
$update ||
! $cache->has($cacheKey)
!$cache->has($cacheKey)
) {
return static::updateAndGet($parameters ?? []);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -214,16 +215,16 @@ 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);

$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);
Expand Down Expand Up @@ -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, ':'));
Expand All @@ -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];
Expand All @@ -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 '<!--'.($close ? '/' : '').$marker.'-->';
return '<!--' . ($close ? '/' : '') . $marker . '-->';
}

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()
{
}
}
10 changes: 7 additions & 3 deletions src/PermanentCacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
);
}
}
7 changes: 7 additions & 0 deletions src/Routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Illuminate\Support\Facades\Route;

Route::get('/example', function () {
return 'This is an example route from my package!';
});

0 comments on commit a96b50b

Please sign in to comment.