1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Contracts; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\SourceFileParser; |
6
|
|
|
use Hyde\Framework\Concerns\CanBeInNavigation; |
7
|
|
|
use Hyde\Framework\Concerns\HasPageMetadata; |
8
|
|
|
use Hyde\Framework\Models\FrontMatter; |
9
|
|
|
use Hyde\Framework\Models\Route; |
10
|
|
|
use Hyde\Framework\Services\DiscoveryService; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* To ensure compatibility with the Hyde Framework, all Page Models should extend this class. |
15
|
|
|
* |
16
|
|
|
* Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers. |
17
|
|
|
* |
18
|
|
|
* To learn about what the methods do, see the PHPDocs in the PageContract. |
19
|
|
|
* |
20
|
|
|
* @see \Hyde\Framework\Contracts\PageContract |
21
|
|
|
* @see \Hyde\Framework\Contracts\AbstractMarkdownPage |
22
|
|
|
* @test \Hyde\Framework\Testing\Feature\AbstractPageTest |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractPage implements PageContract, CompilableContract |
25
|
|
|
{ |
26
|
|
|
use HasPageMetadata; |
|
|
|
|
27
|
|
|
use CanBeInNavigation; |
|
|
|
|
28
|
|
|
|
29
|
|
|
public static string $sourceDirectory; |
30
|
|
|
public static string $outputDirectory; |
31
|
|
|
public static string $fileExtension; |
32
|
|
|
public static string $template; |
33
|
|
|
|
34
|
|
|
public string $identifier; |
35
|
|
|
public FrontMatter $matter; |
36
|
|
|
|
37
|
|
|
/** @inheritDoc */ |
38
|
|
|
final public static function getSourceDirectory(): string |
39
|
|
|
{ |
40
|
|
|
return unslash(static::$sourceDirectory); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @inheritDoc */ |
44
|
|
|
final public static function getOutputDirectory(): string |
45
|
|
|
{ |
46
|
|
|
return unslash(static::$outputDirectory); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @inheritDoc */ |
50
|
|
|
final public static function getFileExtension(): string |
51
|
|
|
{ |
52
|
|
|
return '.'.ltrim(static::$fileExtension, '.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @inheritDoc */ |
56
|
|
|
public static function parse(string $slug): PageContract |
57
|
|
|
{ |
58
|
|
|
return (new SourceFileParser(static::class, $slug))->get(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @inheritDoc */ |
62
|
|
|
public static function files(): array|false |
63
|
|
|
{ |
64
|
|
|
return DiscoveryService::getSourceFileListForModel(static::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @inheritDoc */ |
68
|
|
|
public static function all(): Collection |
69
|
|
|
{ |
70
|
|
|
$collection = new Collection(); |
71
|
|
|
|
72
|
|
|
foreach (static::files() as $basename) { |
73
|
|
|
$collection->push(static::parse($basename)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $collection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @inheritDoc */ |
80
|
|
|
public static function qualifyBasename(string $basename): string |
81
|
|
|
{ |
82
|
|
|
return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @inheritDoc */ |
86
|
|
|
public static function getOutputLocation(string $basename): string |
87
|
|
|
{ |
88
|
|
|
// Using the trim function we ensure we don't have a leading slash when the output directory is the root directory. |
89
|
|
|
return trim( |
90
|
|
|
static::getOutputDirectory().'/'.unslash($basename), |
91
|
|
|
'/' |
92
|
|
|
).'.html'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function __construct(string $identifier = '', FrontMatter|array $matter = []) |
96
|
|
|
{ |
97
|
|
|
$this->identifier = $identifier; |
98
|
|
|
$this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @interitDoc */ |
102
|
|
|
public function __get(string $name) |
103
|
|
|
{ |
104
|
|
|
return $this->matter->get($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @inheritDoc */ |
108
|
|
|
public function __set(string $name, $value): void |
109
|
|
|
{ |
110
|
|
|
$this->matter->set($name, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @inheritDoc */ |
114
|
|
|
public function matter(string $key = null, mixed $default = null): mixed |
115
|
|
|
{ |
116
|
|
|
return $this->matter->get($key, $default); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @inheritDoc */ |
120
|
|
|
public function getSourcePath(): string |
121
|
|
|
{ |
122
|
|
|
return static::qualifyBasename($this->identifier); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @inheritDoc */ |
126
|
|
|
public function getOutputPath(): string |
127
|
|
|
{ |
128
|
|
|
return static::getCurrentPagePath().'.html'; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @inheritDoc */ |
132
|
|
|
public function getCurrentPagePath(): string |
133
|
|
|
{ |
134
|
|
|
return trim(static::getOutputDirectory().'/'.$this->identifier, '/'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** @inheritDoc */ |
138
|
|
|
public function getRoute(): Route |
139
|
|
|
{ |
140
|
|
|
return new Route($this); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** @inheritDoc */ |
144
|
|
|
public function htmlTitle(?string $title = null): string |
145
|
|
|
{ |
146
|
|
|
$pageTitle = $title ?? $this->title ?? null; |
|
|
|
|
147
|
|
|
|
148
|
|
|
return $pageTitle |
149
|
|
|
? config('site.name', 'HydePHP').' - '.$pageTitle |
150
|
|
|
: config('site.name', 'HydePHP'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** @inheritDoc */ |
154
|
|
|
public function getBladeView(): string |
155
|
|
|
{ |
156
|
|
|
return static::$template; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** @inheritDoc */ |
160
|
|
|
abstract public function compile(): string; |
161
|
|
|
} |
162
|
|
|
|