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