|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Concerns\HydePage; |
|
6
|
|
|
use Hyde\Framework\Concerns\JsonSerializesArrayable; |
|
7
|
|
|
use Hyde\Framework\Exceptions\RouteNotFoundException; |
|
8
|
|
|
use Hyde\Framework\Foundation\RouteCollection; |
|
9
|
|
|
use Hyde\Framework\Hyde; |
|
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The Route class bridges the gaps between Hyde pages and their respective compiled static webpages |
|
14
|
|
|
* by providing helper methods and information allowing you to easily access and interact with the |
|
15
|
|
|
* various paths associated with a page, both source and compiled file paths as well as the URL. |
|
16
|
|
|
* |
|
17
|
|
|
* @see \Hyde\Framework\Testing\Feature\RouteTest |
|
18
|
|
|
*/ |
|
19
|
|
|
class Route implements \Stringable, \JsonSerializable, Arrayable |
|
20
|
|
|
{ |
|
21
|
|
|
use JsonSerializesArrayable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The source model for the route. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Hyde\Framework\Concerns\HydePage |
|
27
|
|
|
*/ |
|
28
|
|
|
protected HydePage $sourceModel; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The unique route key for the route. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string The route key. Generally <output-directory/slug>. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected string $routeKey; |
|
36
|
|
|
|
|
37
|
|
|
protected string $sourcePath; |
|
38
|
|
|
protected string $outputPath; |
|
39
|
|
|
protected string $uriPath; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Construct a new Route instance for the given page model. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Hyde\Framework\Concerns\HydePage $page |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(HydePage $page) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->sourceModel = $page; |
|
49
|
|
|
$this->routeKey = $page->getRouteKey(); |
|
50
|
|
|
$this->sourcePath = $page->getSourcePath(); |
|
51
|
|
|
$this->outputPath = $page->getOutputPath(); |
|
52
|
|
|
$this->uriPath = $page->getLink(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Cast a route object into a string that can be used in a href attribute. |
|
57
|
|
|
* Should be the same as getLink(). |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __toString(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getLink(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the instance as an array. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array<string, string> |
|
68
|
|
|
*/ |
|
69
|
|
|
public function toArray(): array |
|
70
|
|
|
{ |
|
71
|
|
|
return [ |
|
|
|
|
|
|
72
|
|
|
'routeKey' => $this->routeKey, |
|
73
|
|
|
'sourcePath' => $this->sourcePath, |
|
74
|
|
|
'outputPath' => $this->outputPath, |
|
75
|
|
|
'sourceModel' => $this->sourceModel::class, |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Resolve a site web link to the file, using pretty URLs if enabled. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string Relative URL path to the route site file. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getLink(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return Hyde::relativeLink($this->uriPath); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the page type for the route. |
|
91
|
|
|
* |
|
92
|
|
|
* @return class-string<\Hyde\Framework\Concerns\HydePage> |
|
|
|
|
|
|
93
|
|
|
*/ |
|
94
|
|
|
public function getPageType(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->sourceModel::class; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the source model for the route. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Hyde\Framework\Concerns\HydePage |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getSourceModel(): HydePage |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->sourceModel; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get the unique route key for the route. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string The route key. Generally <output-directory/slug>. |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getRouteKey(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->routeKey; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the path to the source file. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string Path relative to the root of the project. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getSourcePath(): string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->sourcePath; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get the path to the output file. |
|
131
|
|
|
* |
|
132
|
|
|
* @return string Path relative to the site output directory. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getOutputPath(): string |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->outputPath; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the qualified URL for the route, using pretty URLs if enabled. |
|
141
|
|
|
* |
|
142
|
|
|
* @return string Fully qualified URL using the configured base URL. |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getQualifiedUrl(): string |
|
145
|
|
|
{ |
|
146
|
|
|
return Hyde::url($this->outputPath); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param \Hyde\Framework\Models\Route|string $route A route instance or route key string |
|
|
|
|
|
|
151
|
|
|
*/ |
|
152
|
|
|
public function is(Route|string $route): bool |
|
153
|
|
|
{ |
|
154
|
|
|
if ($route instanceof Route) { |
|
155
|
|
|
return $this->getRouteKey() === $route->getRouteKey(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $this->getRouteKey() === $route; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get a route from the route index for the specified route key. |
|
163
|
|
|
* |
|
164
|
|
|
* Alias for static::getFromKey(). |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $routeKey Example: posts/foo.md |
|
167
|
|
|
* @return \Hyde\Framework\Models\Route |
|
168
|
|
|
* |
|
169
|
|
|
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function get(string $routeKey): static |
|
172
|
|
|
{ |
|
173
|
|
|
return static::getFromKey($routeKey); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get a route from the route index for the specified route key. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $routeKey Example: posts/foo, posts.foo |
|
180
|
|
|
* @return \Hyde\Framework\Models\Route |
|
181
|
|
|
* |
|
182
|
|
|
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function getFromKey(string $routeKey): static |
|
185
|
|
|
{ |
|
186
|
|
|
return Hyde::routes()->get(str_replace('.', '/', $routeKey)) |
|
187
|
|
|
?? throw new RouteNotFoundException($routeKey); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Get a route from the route index for the specified source file path. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $sourceFilePath Example: _posts/foo.md |
|
194
|
|
|
* @return \Hyde\Framework\Models\Route |
|
195
|
|
|
* |
|
196
|
|
|
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function getFromSource(string $sourceFilePath): static |
|
199
|
|
|
{ |
|
200
|
|
|
return Hyde::routes()->first(function (Route $route) use ($sourceFilePath) { |
|
201
|
|
|
return $route->getSourcePath() === $sourceFilePath; |
|
202
|
|
|
}) ?? throw new RouteNotFoundException($sourceFilePath); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get a route from the route index for the supplied page model. |
|
207
|
|
|
* |
|
208
|
|
|
* @param \Hyde\Framework\Concerns\HydePage $page |
|
209
|
|
|
* @return \Hyde\Framework\Models\Route |
|
210
|
|
|
*/ |
|
211
|
|
|
public static function getFromModel(HydePage $page): Route |
|
212
|
|
|
{ |
|
213
|
|
|
return $page->getRoute(); |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get all routes from the route index. |
|
218
|
|
|
* |
|
219
|
|
|
* @return \Hyde\Framework\Foundation\RouteCollection<\Hyde\Framework\Models\Route> |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function all(): RouteCollection |
|
222
|
|
|
{ |
|
223
|
|
|
return Hyde::routes(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get the current route for the page being rendered. |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function current(): Route |
|
230
|
|
|
{ |
|
231
|
|
|
return Hyde::currentRoute() ?? throw new RouteNotFoundException('current'); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Get the home route, usually the index page route. |
|
236
|
|
|
*/ |
|
237
|
|
|
public static function home(): Route |
|
238
|
|
|
{ |
|
239
|
|
|
return static::getFromKey('index'); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Determine if the supplied route key exists in the route index. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $routeKey |
|
246
|
|
|
* @return bool |
|
247
|
|
|
*/ |
|
248
|
|
|
public static function exists(string $routeKey): bool |
|
249
|
|
|
{ |
|
250
|
|
|
return Hyde::routes()->has($routeKey); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|