1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\RealtimeCompiler\Routing; |
6
|
|
|
|
7
|
|
|
use Desilva\Microserve\Request; |
8
|
|
|
use Desilva\Microserve\Response; |
9
|
|
|
use Hyde\RealtimeCompiler\RealtimeCompiler; |
10
|
|
|
use Hyde\RealtimeCompiler\Actions\AssetFileLocator; |
11
|
|
|
use Hyde\RealtimeCompiler\Concerns\SendsErrorResponses; |
12
|
|
|
use Hyde\RealtimeCompiler\Models\FileObject; |
13
|
|
|
use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel; |
14
|
|
|
|
15
|
|
|
class Router |
16
|
|
|
{ |
17
|
|
|
use SendsErrorResponses; |
18
|
|
|
use InteractsWithLaravel; |
19
|
|
|
|
20
|
|
|
protected Request $request; |
21
|
|
|
|
22
|
|
|
public function __construct(Request $request) |
23
|
|
|
{ |
24
|
|
|
$this->request = $request; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function handle(): Response |
28
|
|
|
{ |
29
|
|
|
if ($this->shouldProxy($this->request)) { |
30
|
|
|
return $this->proxyStatic(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->bootApplication(); |
34
|
|
|
|
35
|
|
|
$virtualRoutes = app(RealtimeCompiler::class)->getVirtualRoutes(); |
36
|
|
|
|
37
|
|
|
if (isset($virtualRoutes[$this->request->path])) { |
38
|
|
|
return $virtualRoutes[$this->request->path]($this->request); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return PageRouter::handle($this->request); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* If the request is not for a web page, we assume it's |
46
|
|
|
* a static asset, which we instead want to proxy. |
47
|
|
|
*/ |
48
|
|
|
protected function shouldProxy(Request $request): bool |
49
|
|
|
{ |
50
|
|
|
// Always proxy media files. This condition is just to improve performance |
51
|
|
|
// without having to check the file extension. |
52
|
|
|
if (str_starts_with($request->path, '/media/')) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Get the requested file extension |
57
|
|
|
$extension = pathinfo($request->path)['extension'] ?? null; |
58
|
|
|
|
59
|
|
|
// If the extension is not set (pretty url), or is .html, |
60
|
|
|
// we assume it's a web page which we need to compile. |
61
|
|
|
if ($extension === null || $extension === 'html') { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Don't proxy the search.json file, as it's generated on the fly. |
66
|
|
|
if (str_ends_with($request->path, 'search.json')) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// The page is not a web page, so we assume it should be proxied. |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Proxy a static file or return a 404. |
76
|
|
|
*/ |
77
|
|
|
protected function proxyStatic(): Response |
78
|
|
|
{ |
79
|
|
|
$path = AssetFileLocator::find($this->request->path); |
80
|
|
|
|
81
|
|
|
if ($path === null) { |
82
|
|
|
return $this->notFound(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$file = new FileObject($path); |
86
|
|
|
|
87
|
|
|
return (new Response(200, 'OK', [ |
88
|
|
|
'body' => $file->getStream(), |
89
|
|
|
]))->withHeaders([ |
90
|
|
|
'Content-Type' => $file->getMimeType(), |
91
|
|
|
'Content-Length' => $file->getContentLength(), |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|