1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Illuminate; |
4
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
|
|
|
6
|
|
|
use Illuminate\Contracts\Console\Kernel as ConsoleKernel; |
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Http\Kernel as HttpKernel; |
|
|
|
|
8
|
|
|
use Illuminate\Http\Request as IlluminateRequest; |
|
|
|
|
9
|
|
|
use Swoole\Http\Response as SwooleResponse; |
10
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
|
|
|
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
|
|
|
|
12
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
|
|
|
13
|
|
|
|
14
|
|
|
class Laravel |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/**@var Container */ |
|
|
|
|
17
|
|
|
protected $currentApp; |
18
|
|
|
|
19
|
|
|
/**@var Container */ |
|
|
|
|
20
|
|
|
protected $snapshotApp; |
21
|
|
|
|
22
|
|
|
/**@var ReflectionApp */ |
|
|
|
|
23
|
|
|
protected $reflectionApp; |
24
|
|
|
|
25
|
|
|
/**@var HttpKernel */ |
|
|
|
|
26
|
|
|
protected $kernel; |
27
|
|
|
|
28
|
|
|
/**@var array */ |
|
|
|
|
29
|
|
|
protected $conf = []; |
30
|
|
|
|
31
|
|
|
/**@var array */ |
|
|
|
|
32
|
|
|
protected static $staticBlackList = [ |
33
|
|
|
'/index.php' => true, |
34
|
|
|
'/.htaccess' => true, |
35
|
|
|
'/web.config' => true, |
36
|
|
|
]; |
37
|
|
|
/**@var array */ |
|
|
|
|
38
|
|
|
protected static $staticIndexList = [ |
39
|
|
|
'index.html', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/**@var array */ |
|
|
|
|
43
|
|
|
private $rawGlobals = []; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/**@var CleanerManager */ |
|
|
|
|
46
|
|
|
protected $cleanerManager; |
47
|
|
|
|
48
|
|
|
public function __construct(array $conf = []) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->conf = $conf; |
51
|
|
|
|
52
|
|
|
// Merge $_ENV $_SERVER |
53
|
|
|
$this->rawGlobals['_SERVER'] = $_SERVER + $this->conf['_SERVER']; |
54
|
|
|
$this->rawGlobals['_ENV'] = $_ENV + $this->conf['_ENV']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function prepareLaravel() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
list($this->currentApp, $this->kernel) = $this->createAppKernel(); |
60
|
|
|
|
61
|
|
|
$this->reflectionApp = new ReflectionApp($this->currentApp); |
62
|
|
|
|
63
|
|
|
$this->saveSnapshot(); |
64
|
|
|
|
65
|
|
|
// Create cleaner manager |
66
|
|
|
$this->cleanerManager = new CleanerManager($this->currentApp, $this->snapshotApp, $this->conf); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function saveSnapshot() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->snapshotApp = clone $this->currentApp; |
72
|
|
|
|
73
|
|
|
$instances = $this->reflectionApp->instances(); |
74
|
|
|
|
75
|
|
|
foreach ($instances as $key => $value) { |
76
|
|
|
$this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function createAppKernel() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
// Register autoload |
83
|
|
|
self::autoload($this->conf['root_path']); |
84
|
|
|
|
85
|
|
|
// Make kernel for Laravel |
86
|
|
|
$app = require $this->conf['root_path'] . '/bootstrap/app.php'; |
|
|
|
|
87
|
|
|
$kernel = $this->conf['is_lumen'] ? null : $app->make(HttpKernel::class); |
88
|
|
|
|
89
|
|
|
// Boot |
90
|
|
|
if ($this->conf['is_lumen']) { |
91
|
|
|
$this->configureLumen($app); |
92
|
|
|
if (method_exists($app, 'boot')) { |
93
|
|
|
$app->boot(); |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
$app->make(ConsoleKernel::class)->bootstrap(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return [$app, $kernel]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function configureLumen(Container $app) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$cfgPaths = [ |
105
|
|
|
// Framework default configuration |
106
|
|
|
$this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/', |
107
|
|
|
// App configuration |
108
|
|
|
$this->conf['root_path'] . '/config/', |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$keys = []; |
112
|
|
|
foreach ($cfgPaths as $cfgPath) { |
113
|
|
|
$configs = (array)glob($cfgPath . '*.php'); |
114
|
|
|
foreach ($configs as $config) { |
115
|
|
|
$config = substr(basename($config), 0, -4); |
116
|
|
|
$keys[$config] = $config; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($keys as $key) { |
121
|
|
|
$app->configure($key); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function autoload($rootPath) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$autoload = $rootPath . '/bootstrap/autoload.php'; |
128
|
|
|
if (file_exists($autoload)) { |
129
|
|
|
require_once $autoload; |
|
|
|
|
130
|
|
|
} else { |
131
|
|
|
require_once $rootPath . '/vendor/autoload.php'; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getRawGlobals() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
return $this->rawGlobals; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function handleDynamic(IlluminateRequest $request) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
ob_start(); |
143
|
|
|
|
144
|
|
|
if ($this->conf['is_lumen']) { |
145
|
|
|
$response = $this->currentApp->dispatch($request); |
146
|
|
|
if ($response instanceof SymfonyResponse) { |
147
|
|
|
$content = $response->getContent(); |
148
|
|
|
} else { |
149
|
|
|
$content = $response; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->reflectionApp->callTerminableMiddleware($response); |
153
|
|
|
} else { |
154
|
|
|
$response = $this->kernel->handle($request); |
155
|
|
|
$content = $response->getContent(); |
156
|
|
|
$this->kernel->terminate($request, $response); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// prefer content in response, secondly ob |
160
|
|
|
if (!($response instanceof StreamedResponse) && (string)$content === '' && ob_get_length() > 0) { |
161
|
|
|
$response->setContent(ob_get_contents()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
ob_end_clean(); |
165
|
|
|
|
166
|
|
|
return $response; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function handleStatic(IlluminateRequest $request) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$uri = $request->getRequestUri(); |
172
|
|
|
$uri = (string)str_replace("\0", '', urldecode($uri)); |
173
|
|
|
if (isset(self::$staticBlackList[$uri]) || strpos($uri, '/..') !== false) { |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$requestFile = $this->conf['static_path'] . $uri; |
178
|
|
|
if (is_file($requestFile)) { |
179
|
|
|
return $this->createStaticResponse($requestFile, $request); |
180
|
|
|
} |
181
|
|
|
if (is_dir($requestFile)) { |
182
|
|
|
$indexFile = $this->lookupIndex($requestFile); |
183
|
|
|
if ($indexFile === false) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
return $this->createStaticResponse($indexFile, $request); |
187
|
|
|
} |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
protected function lookupIndex($folder) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$folder = rtrim($folder, '/') . '/'; |
194
|
|
|
foreach (self::$staticIndexList as $index) { |
195
|
|
|
$tmpFile = $folder . $index; |
196
|
|
|
if (is_file($tmpFile)) { |
197
|
|
|
return $tmpFile; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function createStaticResponse($requestFile, IlluminateRequest $request) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$response = new BinaryFileResponse($requestFile); |
206
|
|
|
$response->prepare($request); |
207
|
|
|
$response->isNotModified($request); |
208
|
|
|
return $response; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function clean() |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$this->cleanerManager->clean(); |
214
|
|
|
$this->cleanerManager->cleanControllers(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function cleanProviders() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
$this->cleanerManager->cleanProviders(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function fireEvent($name, array $params = []) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
$params[] = $this->currentApp; |
225
|
|
|
return method_exists($this->currentApp['events'], 'dispatch') ? |
226
|
|
|
$this->currentApp['events']->dispatch($name, $params) : $this->currentApp['events']->fire($name, $params); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function bindRequest(IlluminateRequest $request) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
$this->currentApp->instance('request', $request); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function bindSwoole($swoole) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$this->currentApp->singleton('swoole', function () use ($swoole) { |
|
|
|
|
237
|
|
|
return $swoole; |
238
|
|
|
}); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function bindSwooleHttpResponse(SwooleResponse $response) |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
$this->currentApp->instance('swoole-http-response', $response); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function saveSession() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
if (isset($this->currentApp['session'])) { |
249
|
|
|
$this->currentApp['session']->save(); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|