Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| PublicationPage | |
100.00% |
19 / 19 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| make | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parse | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| pathToIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeIdentifier | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Publications\Pages; |
| 6 | |
| 7 | use Hyde\Pages\Concerns; |
| 8 | use Illuminate\Support\Str; |
| 9 | use Hyde\Markdown\Models\Markdown; |
| 10 | use Hyde\Markdown\Models\FrontMatter; |
| 11 | use Hyde\Publications\Models\PublicationType; |
| 12 | use Hyde\Framework\Actions\MarkdownFileParser; |
| 13 | use Hyde\Framework\Concerns\ValidatesExistence; |
| 14 | use Hyde\Publications\Actions\PublicationPageCompiler; |
| 15 | |
| 16 | use function str_starts_with; |
| 17 | |
| 18 | /** |
| 19 | * Publication pages adds an easy way to create custom no-code page types, |
| 20 | * with support using a custom front matter schema and Blade templates. |
| 21 | * |
| 22 | * @see \Hyde\Publications\Testing\Feature\PublicationPageTest |
| 23 | */ |
| 24 | class PublicationPage extends Concerns\BaseMarkdownPage |
| 25 | { |
| 26 | use ValidatesExistence; |
| 27 | |
| 28 | public readonly PublicationType $type; |
| 29 | |
| 30 | public static string $sourceDirectory = ''; |
| 31 | public static string $outputDirectory = ''; |
| 32 | public static string $template; |
| 33 | |
| 34 | public static function make(string $identifier = '', FrontMatter|array $matter = [], string|Markdown $markdown = '', ?PublicationType $type = null): static |
| 35 | { |
| 36 | return new static($identifier, $matter, $markdown, $type); |
| 37 | } |
| 38 | |
| 39 | public function __construct(string $identifier = '', FrontMatter|array $matter = [], Markdown|string $markdown = '', ?PublicationType $type = null) |
| 40 | { |
| 41 | parent::__construct(static::normalizeIdentifier($type->getDirectory(), $identifier), $matter, $markdown); |
| 42 | |
| 43 | $this->type = $type; |
| 44 | } |
| 45 | |
| 46 | public function getType(): PublicationType |
| 47 | { |
| 48 | return $this->type; |
| 49 | } |
| 50 | |
| 51 | public function compile(): string |
| 52 | { |
| 53 | return PublicationPageCompiler::call($this); |
| 54 | } |
| 55 | |
| 56 | public static function parse(string $identifier): static |
| 57 | { |
| 58 | static::validateExistence(static::class, $identifier); |
| 59 | |
| 60 | $document = MarkdownFileParser::parse( |
| 61 | PublicationPage::sourcePath($identifier) |
| 62 | ); |
| 63 | |
| 64 | return new PublicationPage( |
| 65 | identifier: $identifier, |
| 66 | matter: $document->matter, |
| 67 | markdown: $document->markdown, |
| 68 | type: PublicationType::get(Str::before($identifier, '/')) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | public static function pathToIdentifier(string $path): string |
| 73 | { |
| 74 | return Str::before($path, static::fileExtension()); |
| 75 | } |
| 76 | |
| 77 | protected static function normalizeIdentifier(string $directory, string $identifier): string |
| 78 | { |
| 79 | if (str_starts_with("$identifier/", $directory)) { |
| 80 | return $identifier; |
| 81 | } |
| 82 | |
| 83 | return "$directory/$identifier"; |
| 84 | } |
| 85 | } |