Skip to content

Commit

Permalink
feat: add MyImages image provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JaZo committed Nov 24, 2023
1 parent c40799b commit dbb2094
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!

Expand Down
53 changes: 53 additions & 0 deletions src/ImageProviders/MyImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

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;

class MyImages implements ProvidesImages
{
protected string $directory;

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

public static function make(): static
{
return app(static::class);
}

public function directory(string $directory): static
{
$this->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) . '")'
);
}
}
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
}
}
10 changes: 10 additions & 0 deletions tests/Unit/ImageProviders/MyImagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Swis\Filament\Backgrounds\ImageProviders\MyImages;

it('returns a random image', function () {
$provider = MyImages::make()->directory('images/backgrounds');
$image = $provider->getImage();

expect($image->image)->toBeIn(['url("http://localhost/images/backgrounds/01.jpg")', 'url("http://localhost/images/backgrounds/02.jpg")']);
});
Binary file added workbench/public/images/backgrounds/01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added workbench/public/images/backgrounds/02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dbb2094

Please sign in to comment.