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