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