-
Notifications
You must be signed in to change notification settings - Fork 5
/
Lumen.php
292 lines (258 loc) · 8.55 KB
/
Lumen.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
namespace Codeception\Lib\Connector;
use Codeception\Lib\Connector\Lumen\DummyKernel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Facade;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelBrowser as Client;
use Illuminate\Http\UploadedFile;
//Alias for Symfony < 4.3
if (!class_exists('Symfony\Component\HttpKernel\HttpKernelBrowser') && class_exists('Symfony\Component\HttpKernel\Client')) {
class_alias('Symfony\Component\HttpKernel\Client', 'Symfony\Component\HttpKernel\HttpKernelBrowser');
}
class Lumen extends Client
{
/**
* @var array
*/
private $bindings = [];
/**
* @var array
*/
private $contextualBindings = [];
/**
* @var array
*/
private $instances = [];
/**
* @var array
*/
private $applicationHandlers = [];
/**
* @var \Laravel\Lumen\Application
*/
private $app;
/**
* @var \Codeception\Module\Lumen
*/
private $module;
/**
* @var bool
*/
private $firstRequest = true;
/**
* @var object
*/
private $oldDb;
/**
* Constructor.
*
* @param \Codeception\Module\Lumen $module
*/
public function __construct($module)
{
$this->module = $module;
$components = parse_url($this->module->config['url']);
$server = ['HTTP_HOST' => $components['host']];
// Pass a DummyKernel to satisfy the arguments of the parent constructor.
// The actual kernel object is set in the initialize() method.
parent::__construct(new DummyKernel(), $server);
// Parent constructor defaults to not following redirects
$this->followRedirects(true);
$this->initialize();
}
/**
* Execute a request.
*
* @param SymfonyRequest $request
* @return Response
*/
protected function doRequest($request)
{
if (!$this->firstRequest) {
$this->initialize($request);
}
$this->firstRequest = false;
$this->applyBindings();
$this->applyContextualBindings();
$this->applyInstances();
$this->applyApplicationHandlers();
$request = Request::createFromBase($request);
$response = $this->kernel->handle($request);
$method = new \ReflectionMethod(get_class($this->app), 'callTerminableMiddleware');
$method->setAccessible(true);
$method->invoke($this->app, $response);
return $response;
}
/**
* Initialize the Lumen framework.
*
* @param SymfonyRequest|null $request
*/
private function initialize($request = null)
{
// Store a reference to the database object
// so the database connection can be reused during tests
$this->oldDb = null;
if (isset($this->app['db']) && $this->app['db']->connection()) {
$this->oldDb = $this->app['db'];
}
if (class_exists(Facade::class)) {
// If the container has been instantiated ever,
// we need to clear its static fields before create new container.
Facade::clearResolvedInstances();
}
$this->app = $this->kernel = require $this->module->config['bootstrap_file'];
// in version 5.6.*, lumen introduced the same design pattern like Laravel
// to load all service provider we need to call on Laravel\Lumen\Application::boot()
if (method_exists($this->app, 'boot')) {
$this->app->boot();
}
// Lumen registers necessary bindings on demand when calling $app->make(),
// so here we force the request binding before registering our own request object,
// otherwise Lumen will overwrite our request object.
$this->app->make('request');
$request = $request ?: SymfonyRequest::create($this->module->config['url']);
$this->app->instance('Illuminate\Http\Request', Request::createFromBase($request));
// Reset the old database if there is one
if ($this->oldDb) {
$this->app->singleton('db', function () {
return $this->oldDb;
});
Model::setConnectionResolver($this->oldDb);
}
$this->module->setApplication($this->app);
}
/**
* Make sure files are \Illuminate\Http\UploadedFile instances with the private $test property set to true.
* Fixes issue https://github.com/Codeception/Codeception/pull/3417.
*
* @param array $files
* @return array
*/
protected function filterFiles(array $files)
{
$files = parent::filterFiles($files);
if (! class_exists('Illuminate\Http\UploadedFile')) {
// The \Illuminate\Http\UploadedFile class was introduced in Laravel 5.2.15,
// so don't change the $files array if it does not exist.
return $files;
}
return $this->convertToTestFiles($files);
}
/**
* @param array $files
* @return array
*/
private function convertToTestFiles(array $files)
{
$filtered = [];
foreach ($files as $key => $value) {
if (is_array($value)) {
$filtered[$key] = $this->convertToTestFiles($value);
} else {
$filtered[$key] = UploadedFile::createFromBase($value, true);
}
}
return $filtered;
}
/**
* Apply the registered application handlers.
*/
private function applyApplicationHandlers()
{
foreach ($this->applicationHandlers as $handler) {
call_user_func($handler, $this->app);
}
}
/**
* Apply the registered Laravel service container bindings.
*/
private function applyBindings()
{
foreach ($this->bindings as $abstract => $binding) {
list($concrete, $shared) = $binding;
$this->app->bind($abstract, $concrete, $shared);
}
}
/**
* Apply the registered Laravel service container contextual bindings.
*/
private function applyContextualBindings()
{
foreach ($this->contextualBindings as $concrete => $bindings) {
foreach ($bindings as $abstract => $implementation) {
$this->app->addContextualBinding($concrete, $abstract, $implementation);
}
}
}
/**
* Apply the registered Laravel service container instance bindings.
*/
private function applyInstances()
{
foreach ($this->instances as $abstract => $instance) {
$this->app->instance($abstract, $instance);
}
}
//======================================================================
// Public methods called by module
//======================================================================
/**
* Register a Laravel service container binding that should be applied
* after initializing the Laravel Application object.
*
* @param $abstract
* @param $concrete
* @param bool $shared
*/
public function haveBinding($abstract, $concrete, $shared = false)
{
$this->bindings[$abstract] = [$concrete, $shared];
}
/**
* Register a Laravel service container contextual binding that should be applied
* after initializing the Laravel Application object.
*
* @param $concrete
* @param $abstract
* @param $implementation
*/
public function haveContextualBinding($concrete, $abstract, $implementation)
{
if (! isset($this->contextualBindings[$concrete])) {
$this->contextualBindings[$concrete] = [];
}
$this->contextualBindings[$concrete][$abstract] = $implementation;
}
/**
* Register a Laravel service container instance binding that should be applied
* after initializing the Laravel Application object.
*
* @param $abstract
* @param $instance
*/
public function haveInstance($abstract, $instance)
{
$this->instances[$abstract] = $instance;
}
/**
* Register a handler than can be used to modify the Laravel application object after it is initialized.
* The Laravel application object will be passed as an argument to the handler.
*
* @param $handler
*/
public function haveApplicationHandler($handler)
{
$this->applicationHandlers[] = $handler;
}
/**
* Clear the registered application handlers.
*/
public function clearApplicationHandlers()
{
$this->applicationHandlers = [];
}
}