From c596273defbbf857a93b2b0b06ee29e014169451 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Mon, 2 Dec 2024 20:35:43 +0100 Subject: [PATCH] [5.x] Add an `--uncached` option to the static warm command (#11188) Co-authored-by: Jason Varga --- src/Console/Commands/StaticWarm.php | 6 ++++++ tests/Console/Commands/StaticWarmTest.php | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 7737f9d380..7bb26f59b6 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -9,6 +9,7 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use Illuminate\Console\Command; +use Illuminate\Http\Request as HttpRequest; use Illuminate\Routing\Route; use Illuminate\Support\Collection; use Statamic\Console\EnhancesCommands; @@ -36,6 +37,7 @@ class StaticWarm extends Command {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} + {--uncached : Only warm uncached URLs} '; protected $description = 'Warms the static cache by visiting all URLs'; @@ -178,6 +180,10 @@ private function uris(): Collection ->merge($this->additionalUris()) ->unique() ->reject(function ($uri) use ($cacher) { + if ($this->option('uncached') && $cacher->hasCachedPage(HttpRequest::create($uri))) { + return true; + } + Site::resolveCurrentUrlUsing(fn () => $uri); return $cacher->isExcluded($uri); diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 15d8b40a36..daa5eb03d3 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -4,10 +4,12 @@ use Facades\Tests\Factories\EntryFactory; use Illuminate\Support\Facades\Queue; +use Mockery; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use Statamic\Console\Commands\StaticWarmJob; use Statamic\Facades\Collection; +use Statamic\StaticCaching\Cacher; use Tests\PreventSavingStacheItemsToDisk; use Tests\TestCase; @@ -41,6 +43,21 @@ public function it_warms_the_static_cache() ->assertExitCode(0); } + #[Test] + public function it_only_visits_uncached_urls_when_the_eco_option_is_used() + { + $mock = Mockery::mock(Cacher::class); + $mock->shouldReceive('hasCachedPage')->times(2)->andReturn(true, false); + $mock->allows('isExcluded')->andReturn(false); + app()->instance(Cacher::class, $mock); + + config(['statamic.static_caching.strategy' => 'half']); + + $this->artisan('statamic:static:warm', ['--uncached' => true]) + ->expectsOutput('Visiting 1 URLs...') + ->assertExitCode(0); + } + #[Test] public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync() {