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\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 $slug; |
26
|
|
|
protected PageContract $page; |
27
|
|
|
|
28
|
|
|
public function __construct(string $pageClass, string $slug) |
29
|
|
|
{ |
30
|
|
|
$this->validateExistence($pageClass, $slug); |
31
|
|
|
$this->slug = $slug; |
32
|
|
|
|
33
|
|
|
$this->page = $this->constructBaseModel($pageClass); |
|
|
|
|
34
|
|
|
$this->page = PageModelConstructor::run($this->page); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function constructBaseModel(string $pageClass): BladePage|AbstractMarkdownPage |
38
|
|
|
{ |
39
|
|
|
return $pageClass === BladePage::class |
40
|
|
|
? $this->parseBladePage() |
41
|
|
|
: $this->parseMarkdownPage($pageClass); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function parseBladePage(): BladePage |
45
|
|
|
{ |
46
|
|
|
return new BladePage($this->slug); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function parseMarkdownPage(string $pageClass): AbstractMarkdownPage |
50
|
|
|
{ |
51
|
|
|
/** @var AbstractMarkdownPage $pageClass */ |
52
|
|
|
$document = MarkdownFileParser::parse( |
53
|
|
|
$pageClass::qualifyBasename($this->slug) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$matter = $document->matter; |
57
|
|
|
$markdown = $document->markdown; |
58
|
|
|
|
59
|
|
|
return new $pageClass( |
60
|
|
|
identifier: $this->slug, |
61
|
|
|
matter: $matter, |
62
|
|
|
markdown: $markdown |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function get(): PageContract |
67
|
|
|
{ |
68
|
|
|
return $this->page; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|