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