Skip to content

Commit

Permalink
refactor: move caching to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JaZo committed Nov 26, 2023
1 parent 8f526bf commit 12d7bfd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,26 @@ public function panel(Panel $panel): Panel
}
```

### Remember

You can specify a cache time in seconds using the `remember` method on the plugin. This is especially useful if you use an image provider that uses an API or other external source, so you don't hit the API on every request!

```php
use Swis\Filament\Backgrounds\FilamentBackgroundsPlugin;

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
FilamentBackgroundsPlugin::make()
->remember(900),
])
}
```

### Use your own images

You can use your own images by passing an instance of `MyImages` to the `imageProvider` method on the plugin. This provider allows you to specify a directory (inside your public directory) where your images are stored. The images will be randomly picked from this directory. You can also specify a cache time in seconds, so the images are not picked on every request.
You can use your own images by passing an instance of `MyImages` to the `imageProvider` method on the plugin. This provider allows you to specify a directory (inside your public directory) where your images are stored. The images will be randomly picked from this directory.

```php
use Swis\Filament\Backgrounds\FilamentBackgroundsPlugin;
Expand All @@ -72,7 +89,6 @@ public function panel(Panel $panel): Panel
->imageProvider(
MyImages::make()
->directory('images/backgrounds')
->remember(900)
),
])
}
Expand Down Expand Up @@ -105,6 +121,8 @@ class MyImageProvider implements ProvidesImages
}
```

N.B. Make sure you [cache the result](#remember) if you use an API or other external sources, so you don't hit the API on every request!

## Testing

```bash
Expand Down
24 changes: 19 additions & 5 deletions src/FilamentBackgroundsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Filament\Panel;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\HtmlString;
use Swis\Filament\Backgrounds\Contracts\ProvidesImages;
use Swis\Filament\Backgrounds\ImageProviders\CuratedBySwis;
Expand All @@ -14,6 +15,8 @@ class FilamentBackgroundsPlugin implements Plugin
{
protected ProvidesImages $imageProvider;

protected \DateInterval | \DateTimeInterface | int $ttl = 0;

protected bool $showAttribution = true;

public function getId(): string
Expand Down Expand Up @@ -71,7 +74,7 @@ protected function getAssets(): array
*/
protected function getCssVariables(): array
{
$image = $this->getImageProvider()->getImage();
$image = $this->getImage();

return array_filter([
'filament-backgrounds-image' => new HtmlString($image->image),
Expand All @@ -80,19 +83,30 @@ protected function getCssVariables(): array
]);
}

public function imageProvider(ProvidesImages $imageProvider): self
protected function getImage(): Image
{
return Cache::remember(
'filament-backgrounds:image',
$this->ttl,
fn () => ($this->imageProvider ?? CuratedBySwis::make())->getImage()
);
}

public function imageProvider(ProvidesImages $imageProvider): static
{
$this->imageProvider = $imageProvider;

return $this;
}

public function getImageProvider(): ProvidesImages
public function remember(\DateInterval | \DateTimeInterface | int $ttl): static
{
return $this->imageProvider ?? CuratedBySwis::make();
$this->ttl = $ttl;

return $this;
}

public function showAttribution(bool $showAttribution): self
public function showAttribution(bool $showAttribution): static
{
$this->showAttribution = $showAttribution;

Expand Down
18 changes: 2 additions & 16 deletions src/ImageProviders/MyImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Swis\Filament\Backgrounds\ImageProviders;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Swis\Filament\Backgrounds\Contracts\ProvidesImages;
use Swis\Filament\Backgrounds\Image;
Expand All @@ -12,8 +11,6 @@ class MyImages implements ProvidesImages
{
protected string $directory;

protected \DateInterval | \DateTimeInterface | int $ttl = 0;

public static function make(): static
{
return app(static::class);
Expand All @@ -26,25 +23,14 @@ public function directory(string $directory): static
return $this;
}

public function remember(\DateInterval | \DateTimeInterface | int $ttl): static
{
$this->ttl = $ttl;

return $this;
}

public function getImage(): Image
{
if (! isset($this->directory)) {
throw new \RuntimeException('No image directory set, please provide a directory using the directory() method.');
}

/** @var string $image */
$image = Cache::remember('filament-backgrounds:my-images:image', $this->ttl, function () {
$images = app(Filesystem::class)->files(public_path($this->directory));

return Str::replaceStart(public_path(), '', $images[array_rand($images)]->getPathname());
});
$images = app(Filesystem::class)->files(public_path($this->directory));
$image = Str::replaceStart(public_path(), '', $images[array_rand($images)]->getPathname());

return new Image(
'url("' . asset($image) . '")'
Expand Down

0 comments on commit 12d7bfd

Please sign in to comment.