-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FetchMetadata service and a placeholder (#369)
So I can add live metadata to blog posts (more like to just one blog post) with placeholders like ``` /--- **FETCH_METADATA:all** **FETCH_METADATA:Sec-Fetch-Dest** \--- ``` or ``` ''**FETCH_METADATA:Sec-Fetch-Dest**'' ``` etc.
- Loading branch information
Showing
12 changed files
with
181 additions
and
8 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
site/app/Formatter/Placeholders/FetchMetadataTexyFormatterPlaceholder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Formatter\Placeholders; | ||
|
||
use MichalSpacekCz\Http\FetchMetadata\FetchMetadata; | ||
use MichalSpacekCz\Http\FetchMetadata\FetchMetadataHeader; | ||
use Override; | ||
|
||
/** | ||
* Inserts live `Sec-Fetch-*` (fetch metadata) headers into blog posts for example. | ||
* | ||
* All headers: | ||
* /--- | ||
* **FETCH_METADATA:all** | ||
* \--- | ||
* Single header: | ||
* /--- | ||
* **FETCH_METADATA:Sec-Fetch-Dest** | ||
* \--- | ||
* Inline values: | ||
* ''**FETCH_METADATA:Sec-Fetch-Dest**'' | ||
*/ | ||
readonly class FetchMetadataTexyFormatterPlaceholder implements TexyFormatterPlaceholder | ||
{ | ||
|
||
public function __construct( | ||
private FetchMetadata $fetchMetadata, | ||
) { | ||
} | ||
|
||
|
||
#[Override] | ||
public static function getId(): string | ||
{ | ||
return 'FETCH_METADATA'; | ||
} | ||
|
||
|
||
#[Override] | ||
public function replace(string $value): string | ||
{ | ||
if ($value === 'all') { | ||
$headers = $this->fetchMetadata->getAllHeaders(); | ||
} else { | ||
$header = FetchMetadataHeader::from($value); | ||
$headers = [$header->value => $this->fetchMetadata->getHeader($header)]; | ||
} | ||
$result = []; | ||
foreach ($headers as $header => $value) { | ||
$result[] = "{$header}: {$value}"; | ||
} | ||
return implode("\n", $result); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...pp/Formatter/TexyFormatterPlaceholder.php → ...Placeholders/TexyFormatterPlaceholder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../TrainingDateTexyFormatterPlaceholder.php → .../TrainingDateTexyFormatterPlaceholder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\FetchMetadata; | ||
|
||
use Nette\Http\IRequest; | ||
|
||
readonly class FetchMetadata | ||
{ | ||
|
||
public function __construct( | ||
private IRequest $httpRequest, | ||
) { | ||
} | ||
|
||
|
||
public function getHeader(FetchMetadataHeader $header): ?string | ||
{ | ||
return $this->httpRequest->getHeader($header->value); | ||
} | ||
|
||
|
||
/** | ||
* @return array<value-of<FetchMetadataHeader>, string|null> | ||
*/ | ||
public function getAllHeaders(): array | ||
{ | ||
$headers = []; | ||
foreach (FetchMetadataHeader::cases() as $header) { | ||
$headers[$header->value] = $this->getHeader($header); | ||
} | ||
return $headers; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\FetchMetadata; | ||
|
||
enum FetchMetadataHeader: string | ||
{ | ||
|
||
case Dest = 'Sec-Fetch-Dest'; | ||
case Mode = 'Sec-Fetch-Mode'; | ||
case Site = 'Sec-Fetch-Site'; | ||
case User = 'Sec-Fetch-User'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\FetchMetadata; | ||
|
||
use MichalSpacekCz\Test\Http\Request; | ||
use MichalSpacekCz\Test\TestCaseRunner; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
/** @testCase */ | ||
class FetchMetadataTest extends TestCase | ||
{ | ||
|
||
public function __construct( | ||
private readonly Request $httpRequest, | ||
private readonly FetchMetadata $fetchMetadata, | ||
) { | ||
} | ||
|
||
|
||
public function testGetHeader(): void | ||
{ | ||
$this->httpRequest->setHeader(FetchMetadataHeader::Dest->value, 'document'); | ||
Assert::same('document', $this->fetchMetadata->getHeader(FetchMetadataHeader::Dest)); | ||
Assert::null($this->fetchMetadata->getHeader(FetchMetadataHeader::Site)); | ||
} | ||
|
||
|
||
public function testGetAllHeaders(): void | ||
{ | ||
$this->httpRequest->setHeader(FetchMetadataHeader::Dest->value, 'document'); | ||
$expectedHeaders = [ | ||
'Sec-Fetch-Dest' => 'document', | ||
'Sec-Fetch-Mode' => null, | ||
'Sec-Fetch-Site' => null, | ||
'Sec-Fetch-User' => null, | ||
]; | ||
Assert::same($expectedHeaders, $this->fetchMetadata->getAllHeaders()); | ||
Assert::null($this->fetchMetadata->getHeader(FetchMetadataHeader::Site)); | ||
|
||
$this->httpRequest->setHeader(FetchMetadataHeader::Dest->value, 'document'); | ||
$this->httpRequest->setHeader(FetchMetadataHeader::Mode->value, 'navigate'); | ||
$this->httpRequest->setHeader(FetchMetadataHeader::Site->value, 'cross-site'); | ||
$this->httpRequest->setHeader(FetchMetadataHeader::User->value, '?1'); | ||
$expectedHeaders = [ | ||
'Sec-Fetch-Dest' => 'document', | ||
'Sec-Fetch-Mode' => 'navigate', | ||
'Sec-Fetch-Site' => 'cross-site', | ||
'Sec-Fetch-User' => '?1', | ||
]; | ||
Assert::same($expectedHeaders, $this->fetchMetadata->getAllHeaders()); | ||
} | ||
|
||
} | ||
|
||
TestCaseRunner::run(FetchMetadataTest::class); |