Skip to content

Commit f5c8442

Browse files
author
肖明昌
committed
支持lumen
1 parent 5bb591f commit f5c8442

File tree

10 files changed

+163
-39
lines changed

10 files changed

+163
-39
lines changed

fast

100644100755
File mode changed.

src/Commands/HttpController.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ public function startCommand()
137137
return $event;
138138
});
139139

140+
$versionInfo = $this->config('laravel')['version'];
141+
$framework = 'laravel';
142+
if (strpos(strtolower($versionInfo), 'lumen') !== false) {
143+
$framework = 'lumen';
144+
}
140145
$this->output->table($this->getInfos(false), 'fast laravel http info.');
141-
(new Manager($this->laravelApp, 'laravel'))->start();
146+
(new Manager($this->laravelApp, $framework))->start();
142147
}
143148

144149
/**
@@ -239,31 +244,31 @@ protected function getInfos($printStatus=true)
239244
{
240245
$pid = $this->getPid();
241246
$isRunning = $this->isRunning($pid);
242-
$appName = $this->config('app')['name'];
243-
$host = $this->config('server')['server']['host'];
244-
$port = $this->config('server')['server']['port'];
245-
$reactorNum = $this->config('server')['server']['options']['reactor_num'];
246-
$workerNum = $this->config('server')['server']['options']['worker_num'];
247-
$taskWorkerNum = $this->config('server')['server']['options']['task_worker_num'];
248-
$sandboxMode = $this->config('server')['sandbox_mode'] ?? true;
249-
$logFile = $this->config('server')['server']['options']['log_file'];
247+
$appName = (string)$this->config('app')['name'];
248+
$host = (string)$this->config('server')['server']['host'];
249+
$port = (string)$this->config('server')['server']['port'];
250+
$reactorNum = (string)$this->config('server')['server']['options']['reactor_num'];
251+
$workerNum = (string)$this->config('server')['server']['options']['worker_num'];
252+
$taskWorkerNum = (string)$this->config('server')['server']['options']['task_worker_num'];
253+
$sandboxMode = (string)$this->config('server')['sandbox_mode'] ?? true;
254+
$logFile = (string)$this->config('server')['server']['options']['log_file'];
250255
$data = [
251-
['id' => 1,'name' => 'App Name','value' => $appName],
252-
['id' => 1,'name' => 'PHP Version','value' => phpversion()],
253-
['id' => 2,'name' => 'Swoole Version','value' => swoole_version()],
254-
['id' => 3,'name' => 'Laravel Version','value' => $this->config('laravel')['version']],
255-
['id' => 4,'name' => 'Listen IP','value' => $host],
256-
['id' => 5,'name' => 'Listen Port','value' => $port],
257-
['id' => 6,'name' => 'Reactor Num','value' => $reactorNum],
258-
['id' => 7,'name' => 'Worker Num','value' => $workerNum],
259-
['id' => 8,'name' => 'Task Worker Num','value' => $taskWorkerNum],
260-
['id' => 9,'name' => 'Sandbox Mode','value' => $sandboxMode ? 'On' : 'Off'],
261-
['id' => 10,'name' => 'Log Path','value' => $logFile],
256+
['id' => '1','name' => 'App Name','value' => $appName],
257+
['id' => '2','name' => 'PHP Version','value' => phpversion()],
258+
['id' => '3','name' => 'Swoole Version','value' => swoole_version()],
259+
['id' => '4','name' => 'Laravel Version','value' => $this->config('laravel')['version']],
260+
['id' => '5','name' => 'Listen IP','value' => $host],
261+
['id' => '6','name' => 'Listen Port','value' => $port],
262+
['id' => '7','name' => 'Reactor Num','value' => $reactorNum],
263+
['id' => '8','name' => 'Worker Num','value' => $workerNum],
264+
['id' => '9','name' => 'Task Worker Num','value' => $taskWorkerNum],
265+
['id' => '10','name' => 'Sandbox Mode','value' => $sandboxMode ? 'On' : 'Off'],
266+
['id' => '11','name' => 'Log Path','value' => $logFile],
262267
];
263268

264269
if ($printStatus) {
265-
$data[] = ['id' => 11,'name' => 'Server Status','value' => $isRunning ? 'Online' : 'Offline'];
266-
$data[] = ['id' => 12,'name' => 'PID','value' => $isRunning ? $pid : 'None'];
270+
$data[] = ['id' => '12','name' => 'Server Status','value' => $isRunning ? 'Online' : 'Offline'];
271+
$data[] = ['id' => '13','name' => 'PID','value' => $isRunning ? $pid : 'None'];
267272
}
268273
return $data;
269274
}

src/Listeners/StartListener.php

100644100755
File mode changed.

src/Listeners/WorkerStartListener.php

100644100755
File mode changed.

src/LumenServiceProvider.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace FastLaravel\Http;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use FastLaravel\Http\Server\Manager;
7+
use FastLaravel\Http\Commands\HttpServerCommand;
8+
9+
class LumenServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Indicates if loading of the provider is deferred.
13+
*
14+
* @var bool
15+
*/
16+
protected $defer = false;
17+
18+
/**
19+
* Boot the service provider. Perform post-registration booting of services.
20+
*
21+
* @return void
22+
*/
23+
public function boot()
24+
{
25+
if ($this->app->runningInConsole() && isSwooleConsole()) {
26+
//发布配置
27+
$this->publishes([
28+
__DIR__ . '/../config/swoole_http.php' => base_path('config/swoole_http.php'),
29+
__DIR__ . '/../fast' => base_path('fast'),
30+
], 'fast-laravel');
31+
}
32+
}
33+
34+
/**
35+
* Register the service provider.
36+
*
37+
* @return void
38+
*/
39+
public function register()
40+
{
41+
if ($this->app->runningInConsole() && isSwooleConsole()) {
42+
$this->mergeConfigs();
43+
$this->registerCommands();
44+
}
45+
}
46+
47+
/**
48+
* Merge configurations.
49+
*/
50+
protected function mergeConfigs()
51+
{
52+
//合并覆盖默认配置,app中只需要填写需要修改的配置
53+
$this->mergeConfigFrom(__DIR__ . '/../config/swoole_http.php', 'swoole_http');
54+
}
55+
56+
/**
57+
* 注册 commands.
58+
*/
59+
protected function registerCommands()
60+
{
61+
$this->commands([
62+
HttpServerCommand::class,
63+
]);
64+
}
65+
66+
}

src/Middleware/TaskWorker.php

100644100755
File mode changed.

src/Server/Application.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Illuminate\Http\Response;
77
use Illuminate\Container\Container;
88
use Illuminate\Contracts\Http\Kernel;
9+
use Laravel\Lumen\Console\Kernel as LumenKernel;
910
use Symfony\Component\HttpFoundation\JsonResponse;
1011
use Symfony\Component\HttpFoundation\RedirectResponse;
1112
use Symfony\Component\HttpFoundation\StreamedResponse;
1213
use Symfony\Component\HttpFoundation\BinaryFileResponse;
14+
use Symfony\Component\Console\Output\ConsoleOutput;
1315
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1416

1517
class Application
@@ -100,7 +102,9 @@ public function __construct($framework, $basePath = null)
100102
protected function bootstrap()
101103
{
102104
$application = $this->getApplication();
103-
$application->bootstrapWith($this->getBootstrappers());
105+
if ($this->isFramework('laravel')) {
106+
$application->bootstrapWith($this->getBootstrappers());
107+
}
104108
$this->enableCoroutine = $application->make('config')->get('swoole_http.enable_coroutine', false);
105109
$this->obOutput = $application->make('config')->get('swoole_http.ob_output', true);
106110

@@ -138,7 +142,12 @@ protected function loadApplication()
138142
public function kernel()
139143
{
140144
if (! $this->kernel instanceof Kernel) {
141-
$this->kernel = $this->getApplication()->make(Kernel::class);
145+
if ($this->isFramework('laravel')) {
146+
$this->kernel = $this->getApplication()->make(Kernel::class);
147+
} else {
148+
149+
$this->kernel = $this->getApplication()->make(LumenKernel::class);
150+
}
142151
}
143152

144153
return $this->kernel;
@@ -167,7 +176,11 @@ public function handle(Request $request)
167176
}
168177
// 检测是否开启ob_output
169178
$this->obOutput && ob_start();
170-
$response = $this->kernel()->handle($request);
179+
if ($this->isFramework('laravel')) {
180+
$response = $this->kernel()->handle($request);
181+
} else {
182+
$response = $this->getApplication()->dispatch($request);
183+
}
171184

172185
// 处理debug信息
173186
$debug = $request->get('debug_print', false);
@@ -250,8 +263,8 @@ protected function setFramework($framework)
250263
{
251264
$framework = strtolower($framework);
252265

253-
if (! in_array($framework, ['laravel'])) {
254-
throw new \Exception(sprintf('Not support framework "%s".', $this->framework));
266+
if (! in_array($framework, ['laravel', 'lumen'])) {
267+
throw new \Exception(sprintf('Not support framework "%s".', $framework));
255268
}
256269

257270
$this->framework = $framework;
@@ -275,7 +288,9 @@ protected function setBasePath($basePath)
275288
*/
276289
public function terminate(Request $request, $response)
277290
{
278-
$this->kernel()->terminate($request, $response);
291+
if ($this->isFramework('laravel')) {
292+
$this->kernel()->terminate($request, $response);
293+
}
279294
}
280295

281296
/**
@@ -291,4 +306,14 @@ protected function preResolveInstances($application)
291306
}
292307
}
293308
}
309+
310+
/**
311+
* 判断是否是框架
312+
* @param string $name
313+
* @return bool
314+
*/
315+
protected function isFramework(string $name)
316+
{
317+
return $this->getFramework() === $name;
318+
}
294319
}

src/Server/ApplicationTask.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Container\Container;
77
use Illuminate\Contracts\Http\Kernel;
8+
use Laravel\Lumen\Console\Kernel as LumenKernel;
89
use Illuminate\Support\Facades\Facade;
910
use FastLaravel\Http\Context\TaskRequest;
1011
use FastLaravel\Http\Task\TaskExecutor;
@@ -81,7 +82,7 @@ protected function bootstrap()
8182
{
8283
$application = $this->getApplication();
8384

84-
if ($this->framework === 'laravel') {
85+
if ($this->isFramework('laravel')) {
8586
$bootstrappers = $this->getBootstrappers();
8687
$application->bootstrapWith($bootstrappers);
8788
} elseif (is_null(Facade::getFacadeApplication())) {
@@ -120,7 +121,11 @@ public function getApplication()
120121
public function getKernel()
121122
{
122123
if (! $this->kernel instanceof Kernel) {
123-
$this->kernel = $this->getApplication()->make(Kernel::class);
124+
if ($this->isFramework('laravel')) {
125+
$this->kernel = $this->getApplication()->make(Kernel::class);
126+
} else {
127+
$this->kernel = $this->getApplication()->make(LumenKernel::class);
128+
}
124129
// clean worker middleware,and bind TaskWorker middleware.
125130
$closure = function () {
126131
$this->middleware = [];
@@ -199,8 +204,8 @@ protected function setFramework($framework)
199204
{
200205
$framework = strtolower($framework);
201206

202-
if (! in_array($framework, ['laravel'])) {
203-
throw new \Exception(sprintf('Not support framework "%s".', $this->framework));
207+
if (! in_array($framework, ['laravel', 'lumen'])) {
208+
throw new \Exception(sprintf('Not support framework "%s".', $framework));
204209
}
205210

206211
$this->framework = $framework;
@@ -254,6 +259,16 @@ protected function rebindKernelContainer($kernel)
254259
$resetKernel();
255260
}
256261

262+
/**
263+
* 判断是否是框架
264+
* @param string $name
265+
* @return bool
266+
*/
267+
protected function isFramework(string $name)
268+
{
269+
return $this->getFramework() === $name;
270+
}
271+
257272
/**
258273
* Clone laravel app and kernel while being cloned.
259274
*/
@@ -263,7 +278,7 @@ public function __clone()
263278

264279
$this->application = $application;
265280

266-
if ($this->framework === 'laravel') {
281+
if ($this->isFramework('laravel')) {
267282
$this->rebindKernelContainer($this->getKernel());
268283
}
269284
}

src/Server/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ protected function setSandbox()
620620
{
621621
// set application to sandbox environment
622622
if ($this->sandboxMode) {
623-
$this->sandbox = Sandbox::make($this->getApplication());
623+
$this->sandbox = Sandbox::make($this->getApplication(), $this->getFramework());
624624
}
625625
}
626626

@@ -665,7 +665,7 @@ protected function bindDb()
665665
$pool = $this->container->config['swoole_http']['pool'] ?? [];
666666
if ($pool) {
667667
$this->app->instance('pool.db', new ConnectionResolver(
668-
$this->app->config['database']['connections'],
668+
$this->app->config['database']['connections'] ?? [],
669669
$pool,
670670
$this->app->config['database']['default']
671671
));

src/Server/Sandbox.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Container\Container;
88
use FastLaravel\Http\Server\Application;
9+
use Laravel\Lumen\Application as LumenApplication;
910
use Illuminate\Support\Facades\Facade;
1011
use Illuminate\Support\ServiceProvider;
1112

@@ -36,29 +37,37 @@ class Sandbox
3637
*/
3738
protected $providers = [];
3839

40+
protected $framework;
41+
3942
/**
4043
* Make a sandbox.
4144
*
4245
* @param \FastLaravel\Http\Server\Application
4346
*
4447
* @return Sandbox
4548
*/
46-
public static function make(Application $application)
49+
public static function make(Application $application, $framework='laravel')
4750
{
48-
return new static($application);
51+
return new static($application, $framework);
4952
}
5053

5154
/**
5255
* Sandbox constructor.
5356
* @param \FastLaravel\Http\Server\Application
5457
*/
55-
public function __construct(Application $application)
58+
public function __construct(Application $application, $framework)
5659
{
60+
$this->framework = $framework;
5761
$this->setApplication($application);
5862
$this->setInitialConfig();
5963
$this->setInitialProviders();
6064
}
6165

66+
public function getFramework()
67+
{
68+
return $this->framework;
69+
}
70+
6271
/**
6372
* Set a base application
6473
*
@@ -338,7 +347,11 @@ public function disable()
338347
protected function setInstance($application)
339348
{
340349
$application->instance('app', $application);
341-
$application->instance(Container::class, $application);
350+
if ($this->isFramework('lumen')) {
351+
$application->instance(LumenApplication::class, $application);
352+
} else {
353+
$application->instance(Container::class, $application);
354+
}
342355

343356
Container::setInstance($application);
344357
Facade::clearResolvedInstances();

0 commit comments

Comments
 (0)