Skip to content

Commit 9a5f65a

Browse files
[5.x] Add --header option to static warm command (#11763)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent b610b04 commit 9a5f65a

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Console/Commands/StaticWarm.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class StaticWarm extends Command
4242
{--include= : Only warm specific URLs}
4343
{--exclude= : Exclude specific URLs}
4444
{--max-requests= : Maximum number of requests to warm}
45+
{--header=* : Set custom header (e.g. "Authorization: Bearer your_token")}
4546
';
4647

4748
protected $description = 'Warms the static cache by visiting all URLs';
@@ -167,8 +168,10 @@ private function getRelativeUri(int $index): string
167168

168169
private function requests()
169170
{
170-
return $this->uris()->map(function ($uri) {
171-
return new Request('GET', $uri);
171+
$headers = $this->parseHeaders($this->option('header'));
172+
173+
return $this->uris()->map(function ($uri) use ($headers) {
174+
return new Request('GET', $uri, $headers);
172175
})->all();
173176
}
174177

@@ -374,4 +377,25 @@ protected function additionalUris(): Collection
374377

375378
return $uris->map(fn ($uri) => URL::makeAbsolute($uri));
376379
}
380+
381+
private function parseHeaders($headerOptions): array
382+
{
383+
$headers = [];
384+
if (empty($headerOptions)) {
385+
return $headers;
386+
}
387+
if (! is_array($headerOptions)) {
388+
$headerOptions = [$headerOptions];
389+
}
390+
foreach ($headerOptions as $header) {
391+
if (strpos($header, ':') !== false) {
392+
[$key, $value] = explode(':', $header, 2);
393+
$headers[trim($key)] = trim($value);
394+
} else {
395+
$this->line("<fg=yellow;options=bold>Warning:</> Invalid header format: '$header'. Headers should be in 'Key: Value' format.");
396+
}
397+
}
398+
399+
return $headers;
400+
}
377401
}

tests/Console/Commands/StaticWarmTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,25 @@ public static function queueConnectionsProvider()
271271
];
272272
}
273273

274+
#[Test]
275+
public function it_sets_custom_headers_on_requests()
276+
{
277+
config(['statamic.static_caching.strategy' => 'half']);
278+
279+
$mock = Mockery::mock(\GuzzleHttp\Client::class);
280+
$mock->shouldReceive('send')->andReturnUsing(function ($request) {
281+
$this->assertEquals('Bearer testtoken', $request->getHeaderLine('Authorization'));
282+
$this->assertEquals('Bar', $request->getHeaderLine('X-Foo'));
283+
284+
return Mockery::mock(\GuzzleHttp\Psr7\Response::class);
285+
});
286+
$this->app->instance(\GuzzleHttp\Client::class, $mock);
287+
288+
$this->artisan('statamic:static:warm', [
289+
'--header' => ['Authorization: Bearer testtoken', 'X-Foo: Bar'],
290+
])->assertExitCode(0);
291+
}
292+
274293
private function createPage($slug, $attributes = [])
275294
{
276295
$this->makeCollection()->save();

0 commit comments

Comments
 (0)