Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(Middleware): Fix Middleware behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 10, 2019
1 parent 169440c commit eb9c905
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/commands/ServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function actionReload()
println('rid-httpd is not running.');
return ExitCode::UNSPECIFIED_ERROR;
}
println('rid-httpd worker process restart completed.');
println('rid-httpd worker process reload completed.');
return ExitCode::OK; // 返回退出码
}

Expand Down
16 changes: 10 additions & 6 deletions apps/config/http_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// 控制器命名空间
'controllerNamespace' => 'apps\controllers',

// 中间件命名空间
'middlewareNamespace' => 'apps\middleware',

// 全局中间件
'middleware' => ["IpBan"],
'middleware' => [
apps\middleware\IpBanMiddleware::class,
apps\middleware\DebugMiddleware::class
],

// 组件配置
'components' => [
Expand All @@ -36,8 +36,12 @@
'rules' => [
'GET tracker/{tracker_action}' => ['tracker','index'],
'GET captcha' => ['captcha', 'index'],
'api/v1/{controller}/{action}' => ['api/{controller}', '{action}', 'middleware' => ['Api']],
'{controller}/{action}' => ['{controller}', '{action}', 'middleware' => ['Before']],
'api/v1/{controller}/{action}' => ['api/{controller}', '{action}', 'middleware' => [
apps\middleware\ApiMiddleware::class
]],
'{controller}/{action}' => ['{controller}', '{action}', 'middleware' => [
apps\middleware\AuthMiddleware::class
]],
],
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace apps\middleware;


class BeforeMiddleware
class AuthMiddleware
{

public function handle($callable, \Closure $next)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace apps\middleware;

/**
* 后置中间件
*/
class AfterMiddleware
class DebugMiddleware
{

public function handle($callable, \Closure $next)
{
// 添加中间件执行代码
$response = $next();
list($controller, $action) = $callable;

if (env('APP_DEBUG')) {
println('NOW() :' . date('Y-m-d H:i:s')) ;
Expand All @@ -24,8 +19,6 @@ public function handle($callable, \Closure $next)
var_dump(memory_get_usage());
}

// ...
// 返回响应内容
return $response;
}

Expand Down
6 changes: 1 addition & 5 deletions framework/Http/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class Application extends \Rid\Base\Application
// 控制器命名空间
public $controllerNamespace = '';

// 中间件命名空间
public $middlewareNamespace = '';

// 全局中间件
public $middleware = [];

Expand Down Expand Up @@ -103,8 +100,7 @@ protected function runMiddleware($callable, $middleware)
protected function newMiddlewareInstance($routeMiddleware)
{
$middleware = [];
foreach (array_merge($this->middleware, $routeMiddleware) as $key => $name) {
$class = "{$this->middlewareNamespace}\\{$name}Middleware";
foreach (array_merge($this->middleware, $routeMiddleware) as $key => $class) {
$middleware[$key] = new $class();
}
return $middleware;
Expand Down

0 comments on commit eb9c905

Please sign in to comment.