From 2c1e9ff76181d02945b0c2a631b606f74234e367 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Thu, 28 Nov 2024 08:55:40 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Add=20an=20`=E2=80=94eco`=20option=20to=20t?= =?UTF-8?q?he=20static=20warm=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Console/Commands/StaticWarm.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 7737f9d380..70ad301244 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -36,6 +36,7 @@ class StaticWarm extends Command {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} + {--eco : Only warm uncached URLs} '; protected $description = 'Warms the static cache by visiting all URLs'; @@ -178,6 +179,12 @@ private function uris(): Collection ->merge($this->additionalUris()) ->unique() ->reject(function ($uri) use ($cacher) { + if ($this->option('eco') && + $cacher->hasCachedPage(\Illuminate\Http\Request::create($uri)) + ) { + return true; + } + Site::resolveCurrentUrlUsing(fn () => $uri); return $cacher->isExcluded($uri); From 902a064f8b927c7e67d8a1286990f490e24eaac9 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Thu, 28 Nov 2024 09:24:58 +0100 Subject: [PATCH 2/4] Add a test --- tests/Console/Commands/StaticWarmTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index 15d8b40a36..bc73f4975c 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', ['--eco' => true]) + ->expectsOutput('Visiting 1 URLs...') + ->assertExitCode(0); + } + #[Test] public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync() { From e5688ba8609b825bde3ffa9fcf9936feecee11c0 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 2 Dec 2024 14:23:40 -0500 Subject: [PATCH 3/4] rename --- src/Console/Commands/StaticWarm.php | 4 ++-- tests/Console/Commands/StaticWarmTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 70ad301244..b451673baa 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -36,7 +36,7 @@ class StaticWarm extends Command {--u|user= : HTTP authentication user} {--p|password= : HTTP authentication password} {--insecure : Skip SSL verification} - {--eco : Only warm uncached URLs} + {--uncached : Only warm uncached URLs} '; protected $description = 'Warms the static cache by visiting all URLs'; @@ -179,7 +179,7 @@ private function uris(): Collection ->merge($this->additionalUris()) ->unique() ->reject(function ($uri) use ($cacher) { - if ($this->option('eco') && + if ($this->option('uncached') && $cacher->hasCachedPage(\Illuminate\Http\Request::create($uri)) ) { return true; diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index bc73f4975c..daa5eb03d3 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -53,7 +53,7 @@ public function it_only_visits_uncached_urls_when_the_eco_option_is_used() config(['statamic.static_caching.strategy' => 'half']); - $this->artisan('statamic:static:warm', ['--eco' => true]) + $this->artisan('statamic:static:warm', ['--uncached' => true]) ->expectsOutput('Visiting 1 URLs...') ->assertExitCode(0); } From 02e11c21d7dd0b936befece757a4d5ba8e6a37c1 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 2 Dec 2024 14:24:19 -0500 Subject: [PATCH 4/4] nitpick --- src/Console/Commands/StaticWarm.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index b451673baa..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; @@ -179,9 +180,7 @@ private function uris(): Collection ->merge($this->additionalUris()) ->unique() ->reject(function ($uri) use ($cacher) { - if ($this->option('uncached') && - $cacher->hasCachedPage(\Illuminate\Http\Request::create($uri)) - ) { + if ($this->option('uncached') && $cacher->hasCachedPage(HttpRequest::create($uri))) { return true; }