-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPageRouter.php
101 lines (82 loc) · 3.05 KB
/
PageRouter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Hyde\RealtimeCompiler\Routing;
use Desilva\Microserve\Request;
use Desilva\Microserve\Response;
use Hyde\Foundation\Facades\Routes;
use Hyde\Pages\Concerns\BaseMarkdownPage;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\RealtimeCompiler\Http\LiveEditController;
use Hyde\Framework\Features\Documentation\DocumentationSearchPage;
use Hyde\Pages\Concerns\HydePage;
use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel;
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses;
use Hyde\RealtimeCompiler\Http\DashboardController;
use Hyde\RealtimeCompiler\Http\HtmlResponse;
use Hyde\Hyde;
/**
* Handle routing for a web page request.
*/
class PageRouter
{
use SendsErrorResponses;
use InteractsWithLaravel;
protected Request $request;
public function __construct(Request $request)
{
$this->request = $request;
$this->bootApplication();
}
protected function handlePageRequest(): Response
{
if ($this->request->path === '/dashboard' && DashboardController::enabled()) {
return (new DashboardController($this->request))->handle();
}
if ($this->request->path === '/_hyde/live-edit' && LiveEditController::enabled()) {
return (new LiveEditController($this->request))->handle();
}
return new HtmlResponse(200, 'OK', [
'body' => $this->getHtml($this->getPageFromRoute()),
]);
}
protected function normalizePath(string $path): string
{
// If URL ends in .html, strip it
if (str_ends_with($path, '.html')) {
$path = substr($path, 0, -5);
}
// If the path is empty, serve the index file
if (empty($path) || $path == '/') {
$path = '/index';
}
return ltrim($path, '/');
}
protected function getHtml(HydePage $page): string
{
if ($page->identifier === 'index' && DashboardController::enabled()) {
return DashboardController::renderIndexPage($page);
}
if (config('hyde.server.save_preview')) {
$contents = file_get_contents(StaticPageBuilder::handle($page));
} else {
Hyde::shareViewData($page);
$contents = $page->compile();
}
if ($page instanceof BaseMarkdownPage && LiveEditController::enabled()) {
$contents = LiveEditController::injectLiveEditScript($contents);
}
return $contents;
}
public static function handle(Request $request): Response
{
return (new self($request))->handlePageRequest();
}
protected function getPageFromRoute(): HydePage
{
if (unslash($this->request->path) === DocumentationSearchPage::routeKey() && DocumentationSearchPage::enabled()) {
// Since this page is not present within the search index (as it's normally generated by a build task)
// we instantiate and return it here.
return new DocumentationSearchPage();
}
return Routes::getOrFail($this->normalizePath($this->request->path))->getPage();
}
}