Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
StaticPageBuilder
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 __invoke
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 save
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 compileView
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 compilePost
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 compilePage
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 compileDocs
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Hyde\Framework;
4
5use Hyde\Framework\Actions\MarkdownConverter;
6use Hyde\Framework\Concerns\InteractsWithDirectories;
7use Hyde\Framework\Models\BladePage;
8use Hyde\Framework\Models\DocumentationPage;
9use Hyde\Framework\Models\MarkdownDocument;
10use Hyde\Framework\Models\MarkdownPage;
11use Hyde\Framework\Models\MarkdownPost;
12
13/**
14 * Converts a Page Model into a static HTML page.
15 */
16class StaticPageBuilder
17{
18    use InteractsWithDirectories;
19
20    /**
21     * @var string Absolute path to the directory to place compiled files in.
22     */
23    public static string $outputPath;
24
25    /**
26     * Construct the class.
27     *
28     * @param  MarkdownDocument|BladePage  $page  the Page to compile into HTML
29     * @param  bool  $selfInvoke  if set to true the class will invoke when constructed
30     */
31    public function __construct(protected MarkdownDocument|BladePage $page, bool $selfInvoke = false)
32    {
33        $this->needsDirectory(static::$outputPath);
34
35        if ($selfInvoke) {
36            $this->__invoke();
37        }
38    }
39
40    /**
41     * Run the page builder.
42     *
43     * @return bool|int|void
44     */
45    public function __invoke()
46    {
47        view()->share('page', $this->page);
48        view()->share('currentPage', $this->page->getCurrentPagePath());
49
50        if ($this->page instanceof BladePage) {
51            return $this->save($this->page->view, $this->compileView());
52        }
53
54        if ($this->page instanceof MarkdownPost) {
55            $this->needsDirectory(Hyde::getSiteOutputPath('posts'));
56
57            return $this->save('posts/'.$this->page->slug, $this->compilePost());
58        }
59
60        if ($this->page instanceof MarkdownPage) {
61            return $this->save($this->page->slug, $this->compilePage());
62        }
63
64        if ($this->page instanceof DocumentationPage) {
65            $this->needsDirectory(Hyde::getSiteOutputPath(Hyde::docsDirectory()));
66
67            return $this->save(Hyde::docsDirectory().'/'.$this->page->slug, $this->compileDocs());
68        }
69    }
70
71    /**
72     * Save the compiled HTML to file.
73     *
74     * @param  string  $location  of the output file relative to the site output directory
75     * @param  string  $contents  to save to the file
76     * @return string the path to the saved file (since v0.32.x)
77     */
78    private function save(string $location, string $contents): string
79    {
80        $path = Hyde::getSiteOutputPath("$location.html");
81
82        file_put_contents($path, $contents);
83
84        return $path;
85    }
86
87    /**
88     * Compile a custom Blade View into HTML.
89     *
90     * @return string
91     */
92    private function compileView(): string
93    {
94        return view($this->page->view)->render();
95    }
96
97    /**
98     * Compile a Post into HTML using the Blade View.
99     *
100     * @return string
101     */
102    private function compilePost(): string
103    {
104        return view('hyde::layouts/post')->with([
105            'title' => $this->page->title,
106            'markdown' => MarkdownConverter::parse($this->page->body),
107        ])->render();
108    }
109
110    /**
111     * Compile a Markdown Page into HTML using the Blade View.
112     *
113     * @return string
114     */
115    private function compilePage(): string
116    {
117        return view('hyde::layouts/page')->with([
118            'title' => $this->page->title,
119            'markdown' => MarkdownConverter::parse($this->page->body),
120        ])->render();
121    }
122
123    /**
124     * Compile a Documentation page into HTML using the Blade View.
125     *
126     * @return string
127     */
128    private function compileDocs(): string
129    {
130        return view('hyde::layouts/docs')->with([
131            'title' => $this->page->title,
132            'markdown' => MarkdownConverter::parse($this->page->body, DocumentationPage::class),
133        ])->render();
134    }
135}