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