1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Framework\Actions; |
6
|
|
|
|
7
|
|
|
use Hyde\Pages\BladePage; |
8
|
|
|
use Hyde\Pages\Concerns\HydePage; |
9
|
|
|
use Hyde\Pages\Concerns\BaseMarkdownPage; |
10
|
|
|
use Hyde\Framework\Concerns\ValidatesExistence; |
11
|
|
|
|
12
|
|
|
use function is_subclass_of; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Parses a source file and returns a new page model instance for it. |
16
|
|
|
* |
17
|
|
|
* Page Parsers are responsible for parsing a source file into a Page object, |
18
|
|
|
* and may also conduct pre-processing and/or data validation/assembly. |
19
|
|
|
* |
20
|
|
|
* Note that the Page Parsers do not compile any HTML or Markdown. |
21
|
|
|
*/ |
22
|
|
|
class SourceFileParser |
23
|
|
|
{ |
24
|
|
|
use ValidatesExistence; |
25
|
|
|
|
26
|
|
|
protected string $identifier; |
27
|
|
|
protected HydePage $page; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws \Hyde\Framework\Exceptions\FileNotFoundException If the file does not exist. |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $pageClass, string $identifier) |
33
|
|
|
{ |
34
|
|
|
$this->validateExistence($pageClass, $identifier); |
35
|
|
|
$this->identifier = $identifier; |
36
|
|
|
|
37
|
|
|
$this->page = $this->constructPage($pageClass); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function constructPage(string $pageClass): HydePage|BladePage|BaseMarkdownPage |
41
|
|
|
{ |
42
|
|
|
if ($pageClass === BladePage::class) { |
43
|
|
|
return $this->parseBladePage(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (is_subclass_of($pageClass, BaseMarkdownPage::class)) { |
47
|
|
|
return $this->parseMarkdownPage($pageClass); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return new $pageClass($this->identifier); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function parseBladePage(): BladePage |
54
|
|
|
{ |
55
|
|
|
return new BladePage( |
56
|
|
|
identifier: $this->identifier, |
57
|
|
|
matter: BladeMatterParser::parseFile(BladePage::sourcePath($this->identifier)) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @param class-string<\Hyde\Pages\Concerns\BaseMarkdownPage> $pageClass */ |
|
|
|
|
62
|
|
|
protected function parseMarkdownPage(string $pageClass): BaseMarkdownPage |
63
|
|
|
{ |
64
|
|
|
$document = MarkdownFileParser::parse( |
65
|
|
|
$pageClass::sourcePath($this->identifier) |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return new $pageClass( |
69
|
|
|
identifier: $this->identifier, |
70
|
|
|
matter: $document->matter, |
71
|
|
|
markdown: $document->markdown |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function get(): HydePage |
76
|
|
|
{ |
77
|
|
|
return $this->page; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|