Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markers for Cached Components #13

Closed
wants to merge 12 commits into from
15 changes: 15 additions & 0 deletions config/permanent-cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'components' => [
// Add markers around the rendered value of Cached Components,
// this helps to identify the cached value in the rendered HTML.

// Which is useful for debugging and testing, but also for updating
// the cached value inside another cache when using nested caches
'markers' => [
'enabled' => env('PERMANENT_CACHE_MARKERS_ENABLED', false),
'hash' => env('PERMANENT_CACHE_MARKERS_HASH', false),
],
],
];
28 changes: 26 additions & 2 deletions src/CachedComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@
$this->isUpdating ||
$this->shouldBeUpdating()
) {
return parent::resolveView();
return $this->renderOutput(
parent::resolveView()
);
}

if (null !== $cachedValue = $this->get($this->getParameters())) {

Check failure on line 27 in src/CachedComponent.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Vormkracht10\PermanentCache\CachedComponent::resolveView() should return Closure|Illuminate\Contracts\Support\Htmlable|Illuminate\Contracts\View\View|string but return statement is missing.
return new HtmlString((string) $cachedValue);
return new HtmlString($this->renderOutput((string) $cachedValue));
}
}

public function getMarker(): string
{
[$cacheDriver, $cacheKey] = $this::store($this->getParameters());

$marker = $cacheDriver.':'.$cacheKey;

if (config('permanent-cache.components.markers.hash')) {
$marker = md5($marker);
}

return '<!--##########'.$marker.'##########-->';
}

public function renderOutput($value): HtmlString
{
if (config('permanent-cache.components.markers.enabled')) {
$value = $this->getMarker().$value.$this->getMarker();
}

return new HtmlString($value);
}
}
11 changes: 7 additions & 4 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final public function handle($event = null): void
PermanentCacheUpdating::dispatch($this);

$value = is_subclass_of(static::class, CachedComponent::class)
? Blade::renderComponent($this)
? (string) Blade::renderComponent($this)
: $this->run($event);

if (is_null($value)) {
Expand All @@ -94,7 +94,7 @@ public function getParameters()
->getProperties(\ReflectionProperty::IS_PUBLIC))
->filter(fn (\ReflectionProperty $p) => $p->class === static::class)
->mapWithKeys(fn (\ReflectionProperty $p) => [$p->name => $p->getValue($this)])
->toArray();
->all();
}

/**
Expand Down Expand Up @@ -152,8 +152,11 @@ final public static function get($default = null, bool $update = false): mixed

$cache = Cache::driver($driver);

if ($update && ! $cache->has($cacheKey)) {
static::update($parameters ?? [])->onConnection('sync');
if (
$update ||
! $cache->has($cacheKey)
) {
static::update($parameters ?? [])?->onConnection('sync');
}

return $cache->get($cacheKey, $default);
Expand Down
3 changes: 2 additions & 1 deletion src/PermanentCacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class PermanentCacheServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package->name('laravel-permanent-cache');
$package->name('laravel-permanent-cache')
->hasConfigFile();
}

public function registeringPackage()
Expand Down
Loading