Skip to content

Commit

Permalink
feature to create permanent cached blade components
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed Apr 6, 2024
1 parent 9011d73 commit 1e200c8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
31 changes: 26 additions & 5 deletions src/Cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace Vormkracht10\PermanentCache;

use Closure;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use ReflectionClass;
use Vormkracht10\PermanentCache\CachedComponent;

/**
* @method mixed run()
*
* @template V
*/
abstract class Cached
trait Cached
{
use Queueable;

Expand Down Expand Up @@ -53,9 +55,21 @@ final public function handle($event = null): void
{
[$driver, $ident] = self::store();

/** @phpstan-ignore-next-line */
if (null === $update = $this->run($event)) {
return;
if(is_subclass_of($this, \Vormkracht10\PermanentCache\CachedComponent::class)) {
$method = 'render';

/** @phpstan-ignore-next-line */
if (null === $update = (string) $this->{$method}($event)) {
return;
}
}
else {
$method = 'run';

/** @phpstan-ignore-next-line */
if (null === $update = $this->{$method}($event)) {
return;
}
}

Cache::driver($driver)->forever($ident, $update);
Expand Down Expand Up @@ -145,9 +159,16 @@ final public static function getListenerEvents(): array

$concrete = Arr::wrap($reflection->getProperty('event')->getDefaultValue());

if($reflection->isSubclassOf(\Vormkracht10\PermanentCache\CachedComponent::class)) {
$method = 'render';
}
else {
$method = 'run';
}

/** @phpstan-ignore-next-line */
return $concrete ?: Arr::wrap(($reflection
->getMethod('run')
->getMethod($method)
->getParameters()[0] ?? null)
?->getType()
?->getName());
Expand Down
20 changes: 20 additions & 0 deletions src/CachedComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Vormkracht10\PermanentCache;

use Illuminate\Support\HtmlString;
use Illuminate\View\Component;

abstract class CachedComponent extends Component implements Scheduled
{
use Cached;

public function resolveView()
{
if(null !== $cache = $this->get()) {
return new HtmlString($cache);
}

return parent::resolveView();
}
}
6 changes: 3 additions & 3 deletions src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
class PermanentCache
{
/**
* @var array<class-string, array<class-string<Cached>>>
* @var array<class-string, array<class-string<Cached>, class-string<CachedComponent>>>
*/
protected array $cachers = [];

Check failure on line 12 in src/PermanentCache.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Vormkracht10\PermanentCache\PermanentCache::$cachers has invalid type Vormkracht10\PermanentCache\Cached.

/**
* @param array<int, class-string<Cached>> $cachers
* @param array<int, class-string<Cached>, class-string<CachedComponent>> $cachers
* @return $this
*/
public function caches(array $cachers): self

Check failure on line 18 in src/PermanentCache.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @param for parameter $cachers contains unresolvable type.
{
/** @var class-string<Cached> $cacher */
/** @var <class-string<Cached>, class-string<CachedComponent>> $cacher */
foreach ($cachers as $cacher) {

Check failure on line 21 in src/PermanentCache.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @var has invalid value (<class-string<Cached>, class-string<CachedComponent>> $cacher): Unexpected token "<", expected type at offset 9
$events = $cacher::getListenerEvents();

Expand Down

0 comments on commit 1e200c8

Please sign in to comment.