|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Pages; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Markdown\Models\FrontMatter; |
|
8
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
9
|
|
|
use Hyde\Pages\Contracts\DynamicPage; |
|
10
|
|
|
use Illuminate\Support\Facades\View; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A virtual page is a page that does not have a source file. |
|
14
|
|
|
* |
|
15
|
|
|
* @experimental This feature is experimental and may change substantially before the 1.0.0 release. |
|
16
|
|
|
* |
|
17
|
|
|
* This can be useful for creating pagination pages and the like. |
|
18
|
|
|
* When used in a package, it's on the package developer to ensure |
|
19
|
|
|
* that the virtual page is registered with Hyde, usually within the |
|
20
|
|
|
* boot method of the package's service provider so it can be compiled. |
|
21
|
|
|
*/ |
|
22
|
|
|
class VirtualPage extends HydePage implements DynamicPage |
|
23
|
|
|
{ |
|
24
|
|
|
public static string $sourceDirectory = ''; |
|
25
|
|
|
public static string $outputDirectory = ''; |
|
26
|
|
|
public static string $fileExtension = ''; |
|
27
|
|
|
|
|
28
|
|
|
protected string $contents; |
|
29
|
|
|
protected string $view; |
|
30
|
|
|
|
|
31
|
|
|
public static function make(string $identifier = '', FrontMatter|array $matter = [], string $contents = '', string $view = ''): static |
|
32
|
|
|
{ |
|
33
|
|
|
return new static($identifier, $matter, $contents, $view); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new virtual page instance. |
|
38
|
|
|
* |
|
39
|
|
|
* The virtual page class offers two content options. You can either pass a string to the $contents parameter, |
|
40
|
|
|
* Hyde will then save that literally as the page's contents. Alternatively, you can pass a view name to the $view parameter, |
|
41
|
|
|
* and Hyde will use that view to render the page contents with the supplied front matter during the static site build process. |
|
42
|
|
|
* |
|
43
|
|
|
* Note that $contents take precedence over $view, so if you pass both, only $contents will be used. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $identifier The identifier of the page. This is used to generate the route key which is used to create the output filename. |
|
46
|
|
|
* If the identifier for a virtual page is "foo/bar" the page will be saved to "_site/foo/bar.html". |
|
47
|
|
|
* @param \Hyde\Markdown\Models\FrontMatter|array $matter The front matter of the page. When using the Blade view rendering option, |
|
48
|
|
|
* this will be passed to the view. |
|
49
|
|
|
* @param string $contents The contents of the page. This will be saved as-is to the output file. |
|
50
|
|
|
* @param string $view The view key for the view to use to render the page contents. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(string $identifier, FrontMatter|array $matter = [], string $contents = '', string $view = '') |
|
53
|
|
|
{ |
|
54
|
|
|
parent::__construct($identifier, $matter); |
|
55
|
|
|
|
|
56
|
|
|
$this->contents = $contents; |
|
57
|
|
|
$this->view = $view; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getContents(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->contents; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getBladeView(): string |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->view; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function compile(): string |
|
71
|
|
|
{ |
|
72
|
|
|
if (! $this->contents && $this->view) { |
|
73
|
|
|
return View::make($this->getBladeView(), $this->matter->toArray())->render(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->getContents(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|