Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SourceFileParser
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 constructPage
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 parseBladePage
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 parseMarkdownPage
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Actions;
6
7use Hyde\Pages\BladePage;
8use Hyde\Pages\Concerns\HydePage;
9use Hyde\Pages\Concerns\BaseMarkdownPage;
10use Hyde\Framework\Concerns\ValidatesExistence;
11
12use 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 */
22class 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}