diff --git a/.gitattributes b/.gitattributes index 5966108..db881d6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -13,6 +13,7 @@ /docs export-ignore /images export-ignore /tests export-ignore +/workbench export-ignore /package.json export-ignore /package-lock.json export-ignore /phpstan-baseline.neon export-ignore diff --git a/README.md b/README.md index 4e26fb4..1d8581e 100644 --- a/README.md +++ b/README.md @@ -57,9 +57,9 @@ public function panel(Panel $panel): Panel } ``` -### Custom image provider +### Use your own images -You can use a different image provider by passing an instance of `ProvidesImages` to the `imageProvider` method on the plugin. +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. ```php use Swis\Filament\Backgrounds\FilamentBackgroundsPlugin; @@ -69,12 +69,16 @@ public function panel(Panel $panel): Panel return $panel ->plugins([ FilamentBackgroundsPlugin::make() - ->imageProvider(MyImageProvider::make()), + ->imageProvider( + MyImages::make() + ->directory('images/backgrounds') + ->remember(900) + ), ]) } ``` -#### Writing a custom image provider +### Writing a custom image provider To create your own image provider, you need to implement the `ProvidesImages` interface. This interface has one method, `getImage`, which should return an `Image` object. The image object takes two arguments, the first is the CSS `background-image` property, the second is the attribution text. The image will be directly used as background-image in CSS, so it should include `url()`, which allows you to even use gradients or other fancy stuff! diff --git a/src/ImageProviders/MyImages.php b/src/ImageProviders/MyImages.php new file mode 100644 index 0000000..37d8ff6 --- /dev/null +++ b/src/ImageProviders/MyImages.php @@ -0,0 +1,53 @@ +directory = $directory; + + 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()); + }); + + return new Image( + 'url("' . asset($image) . '")' + ); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index c295f61..3742c8b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -16,4 +16,12 @@ protected function getPackageProviders($app): array FilamentBackgroundsServiceProvider::class, ]; } + + /** + * @param \Illuminate\Foundation\Application $app + */ + protected function defineEnvironment($app): void + { + $app->usePublicPath(__DIR__ . '/../workbench/public/'); + } } diff --git a/tests/Unit/ImageProviders/MyImagesTest.php b/tests/Unit/ImageProviders/MyImagesTest.php new file mode 100644 index 0000000..98ad893 --- /dev/null +++ b/tests/Unit/ImageProviders/MyImagesTest.php @@ -0,0 +1,10 @@ +directory('images/backgrounds'); + $image = $provider->getImage(); + + expect($image->image)->toBeIn(['url("http://localhost/images/backgrounds/01.jpg")', 'url("http://localhost/images/backgrounds/02.jpg")']); +}); diff --git a/workbench/public/images/backgrounds/01.jpg b/workbench/public/images/backgrounds/01.jpg new file mode 100644 index 0000000..f6d8443 Binary files /dev/null and b/workbench/public/images/backgrounds/01.jpg differ diff --git a/workbench/public/images/backgrounds/02.jpg b/workbench/public/images/backgrounds/02.jpg new file mode 100644 index 0000000..fb52e3b Binary files /dev/null and b/workbench/public/images/backgrounds/02.jpg differ