This repository was archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
193 lines (159 loc) · 4.41 KB
/
index.php
File metadata and controls
193 lines (159 loc) · 4.41 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace App;
require_once __DIR__ . '/vendor/autoload.php';
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Router;
use Mimey\MimeTypes;
/**
* Sage Next.
*
* @license MIT
* @copyright 2020
*/
(new class() {
/** @var \Illuminate\Container\Container */
public $app;
/** @var \Illuminate\Http\Request */
public $request;
/** @var \Illuminate\Http\Response */
public $response;
/** @var \Mimey\MimeTypes */
public $mimes;
/** @var string */
public $staticDir;
/**
* Class constructor.
*/
public function __construct()
{
$this->staticDir = __DIR__ . '/out';
$this->distDir = __DIR__ . '/dist';
$this->app = new Container();
$this->request = Request::capture();
$this->mimes = new MimeTypes();
}
/**
* Class invocation.
*
* @return void
*/
public function __invoke(): void
{
if (is_admin()) return;
$this->app->instance('Illuminate\Http\Request', $this->request);
$this->app->instance('Illuminate\Http\Response', $this->response);
$this->events = new Dispatcher($this->app);
$this->router = new Router($this->events, $this->app);
$this->routeRequests();
}
/**
* Route requests.
*
* @return void
*/
protected function routeRequests()
{
/** Serve static assets */
if ($this->isStaticRequest()) {
$this->staticAssetRoute();
}
/** Serve index */
if ($this->request->getPathInfo() == '/') {
$this->indexRoute();
}
/** Serve NextJS generated page */
if ($this->isNextEntrypoint()) {
$this->nextRoute();
}
}
/**
* Route: Index
*/
protected function indexRoute()
{
$this->router->get('/', function () {
$response = new Response($this->getStaticContents('index.html'), 200);
$response->header('Content-Type', 'text/html');
return $response;
});
return $this->router->dispatch($this->request)->send();
}
/**
* Route: Next
*/
protected function nextRoute()
{
$this->router->any('{any}', function () {
$response = new Response(file_get_contents($this->entry), 200);
$response->header('Content-Type', 'text/html');
return $response;
})->where('any', '(.*)');
/** Dispatch response */
return $this->router->dispatch($this->request)->send();
}
/**
* Route: Static assets
*/
protected function assetRoute()
{
$this->router->get('{any}', function () {
$filePath = str_replace(
'_next/',
$this->staticDir . '/_next/',
$this->request->getPathInfo()
);
/** Handle [ and ] chars in Next wildcard static paths */
$filePath = str_replace('%5B', '[', $filePath);
$filePath = str_replace('%5D', ']', $filePath);
$mimeType = $this->mimes->getMimeType(
pathinfo($filePath, PATHINFO_EXTENSION)
);
$response = new Response(file_get_contents($filePath), 200);
$response->header('Content-Type', $mimeType);
return $response;
})->where('any', '(.*)');
return $this->router->dispatch($this->request)->send();
}
/**
* Get static file contents
*
* @param string
* @return string
*/
protected function getStaticContents($file): string
{
return file_get_contents("{$this->staticDir}/{$file}");
}
/**
* Get dist file contents
*
* @param string
* @return string
*/
protected function getDistContents($file): string
{
return file_get_contents("{$this->distDir}/{$file}");
}
/**
* Is next entrypoint
*
* @return bool
*/
private function isNextEntrypoint(): bool
{
$requestFile = rtrim($this->request->getPathInfo(), '/\\') . '.html';
return realpath($this->entry = "{$this->staticDir}/{$requestFile}");
}
/**
* Is static resource request
*
* @return bool
*/
private function isStaticRequest(): bool
{
return strpos($this->request->getPathInfo(), '_next');
}
})();