Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
.DS_Store
27 changes: 27 additions & 0 deletions routes/permanent-cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Vormkracht10\PermanentCache\CachesValue;

Route::get('/permanent-cache/{class}/update', function (Request $request, string $class) {
/** @var class-string<CachesValue> $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');
26 changes: 18 additions & 8 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ final public static function update($parameters = [])
{
$instance = app()->make(static::class, $parameters);

dispatch(
$instance
);
dispatch($instance);
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)) {
Expand Down Expand Up @@ -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])]);
}
}
2 changes: 1 addition & 1 deletion src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/PermanentCacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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))),
);
}
}
1 change: 0 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vormkracht10\PermanentCache\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Vormkracht10\PermanentCache\PermanentCacheServiceProvider;

Expand Down