|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\RealtimeCompiler\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Desilva\Microserve\Request; |
|
6
|
|
|
use Desilva\Microserve\Response; |
|
7
|
|
|
use Hyde\Framework\Actions\StaticPageBuilder; |
|
8
|
|
|
use Hyde\Framework\Features\Documentation\DocumentationSearchPage; |
|
9
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
10
|
|
|
use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel; |
|
11
|
|
|
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses; |
|
12
|
|
|
use Hyde\RealtimeCompiler\Http\DashboardController; |
|
13
|
|
|
use Hyde\RealtimeCompiler\Http\HtmlResponse; |
|
14
|
|
|
use Hyde\Support\Models\Route; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handle routing for a web page request. |
|
18
|
|
|
*/ |
|
19
|
|
|
class PageRouter |
|
20
|
|
|
{ |
|
21
|
|
|
use SendsErrorResponses; |
|
22
|
|
|
use InteractsWithLaravel; |
|
23
|
|
|
|
|
24
|
|
|
protected Request $request; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Request $request) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->request = $request; |
|
29
|
|
|
$this->bootApplication(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function handlePageRequest(): Response |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->request->path === '/dashboard' && DashboardController::enabled()) { |
|
35
|
|
|
return new HtmlResponse(200, 'OK', [ |
|
36
|
|
|
'body' => (new DashboardController())->show(), |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return new HtmlResponse(200, 'OK', [ |
|
41
|
|
|
'body' => $this->getHtml($this->getPageFromRoute()), |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function normalizePath(string $path): string |
|
46
|
|
|
{ |
|
47
|
|
|
// If URL ends in .html, strip it |
|
48
|
|
|
if (str_ends_with($path, '.html')) { |
|
49
|
|
|
$path = substr($path, 0, -5); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// If the path is empty, serve the index file |
|
53
|
|
|
if (empty($path) || $path == '/') { |
|
54
|
|
|
$path = '/index'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return ltrim($path, '/'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getHtml(HydePage $page): string |
|
61
|
|
|
{ |
|
62
|
|
|
if ($page->identifier === 'index' && DashboardController::enabled()) { |
|
63
|
|
|
return DashboardController::renderIndexPage($page); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return file_get_contents((new StaticPageBuilder($page))->__invoke()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function handle(Request $request): Response |
|
70
|
|
|
{ |
|
71
|
|
|
return (new self($request))->handlePageRequest(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function getPageFromRoute(): HydePage |
|
75
|
|
|
{ |
|
76
|
|
|
if (unslash($this->request->path) === DocumentationSearchPage::routeKey() && DocumentationSearchPage::enabled()) { |
|
77
|
|
|
// Since this page is not present within the search index (as it's normally generated by a build task) |
|
78
|
|
|
// we instantiate and return it here. |
|
79
|
|
|
return new DocumentationSearchPage(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return Route::getOrFail($this->normalizePath($this->request->path))->getPage(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|