diff --git a/README.md b/README.md index 7a5eaf8..a383144 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,11 @@ This Statamic utility searches images and picture sources in your Statamic Entries and adds each url to a Redis queue. The queue can then be processed to retrieve each url and initiate Glide generation. -The purpose of this package is to pre-generate all website images, and alleviate some of the pressure that image-heavy websites put on the server. You will usually only run these commands once on initial deployment of the site, or after any major restructuring of asset filenames and folders. +The purpose of this package is to pre-generate all website images, and alleviate some of the pressure that image-heavy websites put on the server. This is particularly useful when you have lots of responsive image variants or if you are using Spatie's Statamic Responsive Images package. + +You will usually only run these commands once on initial deployment of the site, or after any major restructuring of asset filenames and folders. + +The package also listens for EntrySaved events and automatically queues the entry url for processing. ## Installation @@ -24,7 +28,7 @@ This package utilises a Redis queue called **gliderequester**. You must have Red ## Usage -This package provides an artisan command that be used like so +A control panel utility is provided for easy usage. An artisan command is also provided and can be used like so: `php artisan glide:request` diff --git a/config/statamic-glide-requester.php b/config/statamic-glide-requester.php index a773cb9..4cbd42d 100644 --- a/config/statamic-glide-requester.php +++ b/config/statamic-glide-requester.php @@ -30,7 +30,7 @@ | size for a responsive image, in which case the values below might be | | 'post_data_attributes' => [ - | 'data-img-id', + | 'data-asset-id', | ], | | 'asset_view_path' => '/lightbox-image-asset' diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 23f3252..b6816b7 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -1,5 +1,5 @@ @extends('statamic::layout') -@section('title', __('Rebuild Search')) +@section('title', __('Glide Requester')) @section('content') @@ -15,8 +15,8 @@
-

Click the button to run the artisan command and it will search for all pictures and image elements in your entries and assets and queue them up for retrieval. This should greatly reduce initial page load times when lots of new images have been added.

-

Don't forget to set up your config file and make sure your redis worker is running on the gliderequester queue.

+

Click the button to queue all entries for image processing. You should only do this when lots of new images are added to multiple entries.

+

This action will clear the current queue. Don't forget to set up your config file and make sure your redis worker is running on the gliderequester queue.

diff --git a/src/Console/Commands/RequestGlideImages.php b/src/Console/Commands/RequestGlideImages.php index a5a263b..6a95f13 100644 --- a/src/Console/Commands/RequestGlideImages.php +++ b/src/Console/Commands/RequestGlideImages.php @@ -3,9 +3,7 @@ namespace stuartcusackie\StatamicGlideRequester\Console\Commands; use Illuminate\Console\Command; -use Statamic\Facades\Entry; -use Statamic\Facades\Asset; -use stuartcusackie\StatamicGlideRequester\Jobs\FindElementsAtUrl; +use stuartcusackie\StatamicGlideRequester\StatamicGlideRequester; class RequestGlideImages extends Command { @@ -35,17 +33,7 @@ class RequestGlideImages extends Command */ public function handle() { - $this->call('queue:clear', ['connection' => 'redis', '--queue' => 'gliderequester']); - - foreach(Entry::all() as $entry) { - - if($entry->url) { - FindElementsAtUrl::dispatch(url($entry->url)); - $this->urls++; - } - } - - $this->info($this->urls . ' urls queued for processing. You can now run the gliderequester queue on redis.'); + StatamicGlideRequester::queueAllEntries(); return 0; } diff --git a/src/Http/Controllers/GlideRequesterController.php b/src/Http/Controllers/GlideRequesterController.php index 76cf47e..2136d53 100644 --- a/src/Http/Controllers/GlideRequesterController.php +++ b/src/Http/Controllers/GlideRequesterController.php @@ -4,7 +4,7 @@ use Illuminate\Http\Request; use Statamic\Http\Controllers\Controller; -use Artisan; +use stuartcusackie\StatamicGlideRequester\StatamicGlideRequester; class GlideRequesterController extends Controller { @@ -28,9 +28,8 @@ public function show(Request $request) */ public function run(Request $request) { - Artisan::call('queue:clear', ['connection' => 'redis', '--queue' => 'gliderequester', '--force' => true]); - Artisan::queue('glide:request')->onConnection('redis')->onQueue('gliderequester'); + StatamicGlideRequester::queueAllEntries(); - return back(); + return back()->withSuccess(__('All entry urls have been queued for processing.')); } } diff --git a/src/Jobs/FindElementsAtUrl.php b/src/Jobs/QueueGlideSources.php similarity index 95% rename from src/Jobs/FindElementsAtUrl.php rename to src/Jobs/QueueGlideSources.php index 106b5f9..614fa04 100644 --- a/src/Jobs/FindElementsAtUrl.php +++ b/src/Jobs/QueueGlideSources.php @@ -11,10 +11,10 @@ use Illuminate\Support\Facades\Http; use simplehtmldom\HtmlDocument; use Illuminate\Support\Str; -use stuartcusackie\StatamicGlideRequester\Jobs\VisitGlideUrl; +use stuartcusackie\StatamicGlideRequester\Jobs\VisitGlideSource; use Illuminate\Support\Facades\Log; -class FindElementsAtUrl implements ShouldQueue +class QueueGlideSources implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -132,7 +132,7 @@ protected function processElement($el) { if(Str::startsWith($path, '/img/')) { Log::info('Adding glide visit for url: ' . url($path)); - VisitGlideUrl::dispatch(url($path)); + VisitGlideSource::dispatch(url($path)); } } } diff --git a/src/Jobs/VisitGlideUrl.php b/src/Jobs/VisitGlideSource.php similarity index 95% rename from src/Jobs/VisitGlideUrl.php rename to src/Jobs/VisitGlideSource.php index b15a60e..84b238c 100644 --- a/src/Jobs/VisitGlideUrl.php +++ b/src/Jobs/VisitGlideSource.php @@ -11,7 +11,7 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; -class VisitGlideUrl implements ShouldQueue +class VisitGlideSource implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 0ec15ac..d6f8a4c 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -5,8 +5,12 @@ use Statamic\Providers\AddonServiceProvider; use Statamic\Facades\Utility; use Illuminate\Routing\Router; +use Illuminate\Support\Facades\Event; +use Statamic\Events\EntrySaved; +use stuartcusackie\StatamicGlideRequester\StatamicGlideRequester; use stuartcusackie\StatamicGlideRequester\Http\Controllers\GlideRequesterController; use stuartcusackie\StatamicGlideRequester\Console\Commands\RequestGlideImages; +use Illuminate\Support\Facades\Log; class ServiceProvider extends AddonServiceProvider { @@ -17,6 +21,7 @@ public function bootAddon() $this ->registerCommands() + ->listen() ->makeUtility(); } @@ -30,6 +35,22 @@ protected function registerCommands() return $this; } + protected function listen() { + + Event::listen(function (EntrySaved $event) { + + Log::info('Saved entry queued for glide requests'); + + if($event->entry->url) { + StatamicGlideRequester::queueUrl(url($event->entry->url)); + } + + }); + + return $this; + + } + protected function makeUtility() { Utility::make('glide-requester') diff --git a/src/StatamicGlideRequester.php b/src/StatamicGlideRequester.php new file mode 100644 index 0000000..ba0ae09 --- /dev/null +++ b/src/StatamicGlideRequester.php @@ -0,0 +1,38 @@ + 'redis', '--queue' => 'gliderequester', '--force' => true]); + + foreach(Entry::all() as $entry) { + + if($entry->url) { + QueueGlideSources::dispatch(url($entry->url)); + } + } + } + +} \ No newline at end of file