1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Concerns; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\SourceFileParser; |
6
|
|
|
use Hyde\Framework\Concerns\FrontMatter\Schemas\PageSchema; |
7
|
|
|
use Hyde\Framework\Contracts\CompilableContract; |
8
|
|
|
use Hyde\Framework\Contracts\PageContract; |
9
|
|
|
use Hyde\Framework\Foundation\PageCollection; |
10
|
|
|
use Hyde\Framework\Hyde; |
11
|
|
|
use Hyde\Framework\Models\FrontMatter; |
12
|
|
|
use Hyde\Framework\Models\Metadata\Metadata; |
13
|
|
|
use Hyde\Framework\Models\Route; |
14
|
|
|
use Hyde\Framework\Services\DiscoveryService; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* To ensure compatibility with the Hyde Framework, all Page Models should extend this class. |
18
|
|
|
* Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers. |
19
|
|
|
* To learn about what the methods do, see the PHPDocs in the PageContract. |
20
|
|
|
* |
21
|
|
|
* Unlike other frameworks, in general you don't instantiate pages yourself in Hyde, |
22
|
|
|
* instead, the page models acts as blueprints defining information for Hyde to |
23
|
|
|
* know how to parse a file, and what data around it should be generated. |
24
|
|
|
* |
25
|
|
|
* To create a parsed file instance, you'd typically just create a source file, |
26
|
|
|
* and you can then access the parsed file from the HydeKernel's page index. |
27
|
|
|
* |
28
|
|
|
* @see \Hyde\Framework\Contracts\PageContract |
29
|
|
|
* @see \Hyde\Framework\Concerns\AbstractMarkdownPage |
30
|
|
|
* @see \Hyde\Framework\Testing\Feature\AbstractPageTest |
31
|
|
|
*/ |
32
|
|
|
abstract class AbstractPage implements PageContract, CompilableContract |
33
|
|
|
{ |
34
|
|
|
use PageSchema; |
35
|
|
|
|
36
|
|
|
public static string $sourceDirectory; |
37
|
|
|
public static string $outputDirectory; |
38
|
|
|
public static string $fileExtension; |
39
|
|
|
public static string $template; |
40
|
|
|
|
41
|
|
|
public string $identifier; |
42
|
|
|
public string $routeKey; |
43
|
|
|
|
44
|
|
|
public FrontMatter $matter; |
45
|
|
|
public Metadata $metadata; |
46
|
|
|
|
47
|
|
|
public function __construct(string $identifier = '', FrontMatter|array $matter = []) |
48
|
|
|
{ |
49
|
|
|
$this->identifier = $identifier; |
50
|
|
|
$this->routeKey = $this->getCurrentPagePath(); |
51
|
|
|
|
52
|
|
|
$this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter); |
|
|
|
|
53
|
|
|
$this->constructPageSchemas(); |
54
|
|
|
$this->metadata = new Metadata($this); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function constructPageSchemas(): void |
58
|
|
|
{ |
59
|
|
|
$this->constructPageSchema(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @inheritDoc */ |
63
|
|
|
final public static function getSourceDirectory(): string |
64
|
|
|
{ |
65
|
|
|
return unslash(static::$sourceDirectory); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @inheritDoc */ |
69
|
|
|
final public static function getOutputDirectory(): string |
70
|
|
|
{ |
71
|
|
|
return unslash(static::$outputDirectory); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @inheritDoc */ |
75
|
|
|
final public static function getFileExtension(): string |
76
|
|
|
{ |
77
|
|
|
return '.'.ltrim(static::$fileExtension, '.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @inheritDoc */ |
81
|
|
|
public static function parse(string $slug): PageContract |
82
|
|
|
{ |
83
|
|
|
return (new SourceFileParser(static::class, $slug))->get(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @inheritDoc */ |
87
|
|
|
public static function files(): array|false |
88
|
|
|
{ |
89
|
|
|
return DiscoveryService::getSourceFileListForModel(static::class); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @inheritDoc */ |
93
|
|
|
public static function all(): PageCollection |
94
|
|
|
{ |
95
|
|
|
return Hyde::pages()->getPages(static::class); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** @inheritDoc */ |
99
|
|
|
public static function qualifyBasename(string $basename): string |
100
|
|
|
{ |
101
|
|
|
return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @inheritDoc */ |
105
|
|
|
public static function getOutputLocation(string $basename): string |
106
|
|
|
{ |
107
|
|
|
// Using the trim function we ensure we don't have a leading slash when the output directory is the root directory. |
108
|
|
|
return trim( |
109
|
|
|
static::getOutputDirectory().'/'.unslash($basename), |
110
|
|
|
'/' |
111
|
|
|
).'.html'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @inheritDoc */ |
115
|
|
|
public function get(string $key = null, mixed $default = null): mixed |
116
|
|
|
{ |
117
|
|
|
if ($key !== null && property_exists($this, $key) && isset($this->$key)) { |
118
|
|
|
return $this->$key; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->matter($key, $default); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** @inheritDoc */ |
125
|
|
|
public function matter(string $key = null, mixed $default = null): mixed |
126
|
|
|
{ |
127
|
|
|
return $this->matter->get($key, $default); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @inheritDoc */ |
131
|
|
|
public function has(string $key, bool $strict = false): bool |
132
|
|
|
{ |
133
|
|
|
if ($strict) { |
134
|
|
|
return property_exists($this, $key) || $this->matter->has($key); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return ! blank($this->get($key)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** @inheritDoc */ |
141
|
|
|
public function getIdentifier(): string |
142
|
|
|
{ |
143
|
|
|
return $this->identifier; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** @inheritDoc */ |
147
|
|
|
public function getSourcePath(): string |
148
|
|
|
{ |
149
|
|
|
return static::qualifyBasename($this->identifier); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** @inheritDoc */ |
153
|
|
|
public function getOutputPath(): string |
154
|
|
|
{ |
155
|
|
|
return $this->getCurrentPagePath().'.html'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** @inheritDoc */ |
159
|
|
|
public function getCurrentPagePath(): string |
160
|
|
|
{ |
161
|
|
|
return trim(static::getOutputDirectory().'/'.$this->identifier, '/'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** @inheritDoc */ |
165
|
|
|
public function getRouteKey(): string |
166
|
|
|
{ |
167
|
|
|
return $this->routeKey; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** @inheritDoc */ |
171
|
|
|
public function getRoute(): Route |
172
|
|
|
{ |
173
|
|
|
return new Route($this); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** @inheritDoc */ |
177
|
|
|
public function htmlTitle(): string |
178
|
|
|
{ |
179
|
|
|
return config('site.name', 'HydePHP').' - '.$this->title; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** @inheritDoc */ |
183
|
|
|
public function getBladeView(): string |
184
|
|
|
{ |
185
|
|
|
return static::$template; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** @inheritDoc */ |
189
|
|
|
abstract public function compile(): string; |
190
|
|
|
|
191
|
|
|
public function renderPageMetadata(): string |
192
|
|
|
{ |
193
|
|
|
return $this->metadata->render(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function showInNavigation(): bool |
197
|
|
|
{ |
198
|
|
|
return ! $this->navigation['hidden']; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function navigationMenuPriority(): int |
202
|
|
|
{ |
203
|
|
|
return $this->navigation['priority']; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function navigationMenuTitle(): string |
207
|
|
|
{ |
208
|
|
|
return $this->navigation['title']; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|