Skip to content

Commit

Permalink
修改路由对象
Browse files Browse the repository at this point in the history
  • Loading branch information
titrxw committed Jan 14, 2021
1 parent ed2ec60 commit 3873ac2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Src/Core/Dispatcher/RequestDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function dispatch(...$params) {
//获取到全部中间件数据,最后附加Http组件的特定的last中间件,用于处理调用Controller
$route = $this->getRoute($psr7Request);
$this->getEventDispatcher()->dispatch(new RouteMatchedEvent($route, $psr7Request));
$psr7Request = $psr7Request->withAttribute('route', $route);
$this->getContext()->setRequest($psr7Request);
$psr7Request->route = $route;

$middleWares = $this->middlewareMapping->getRouteMiddleWares($route);
Expand Down
7 changes: 3 additions & 4 deletions Src/Core/Middleware/ControllerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use W7\Core\Route\Route;
use W7\Contract\Router\RouteInterface;
use W7\Http\Message\Server\Response;

class ControllerMiddleware extends MiddlewareAbstract {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
//此处处理调用控制器操作
/**
* @var Route $route
* @var RouteInterface $route
*/
$route = $request->getAttribute('route');
array_unshift($route->args, $request);

$this->getContext()->setResponse($this->parseResponse($route->run()));
$this->getContext()->setResponse($this->parseResponse($route->run($request)));

return $handler->handle($request);
}
Expand Down
25 changes: 22 additions & 3 deletions Src/Core/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

namespace W7\Core\Route;

use Psr\Http\Message\RequestInterface;
use W7\App;
use W7\Contract\Router\RouteInterface;
use W7\Core\Helper\StringHelper;

class Route {
class Route implements RouteInterface {
public $name;
public $module;
public $handler;
Expand All @@ -32,6 +34,14 @@ public function __construct($name, $module, $handler, array $args = [], array $m
$this->defaults = $defaults;
}

public function getName() {
return $this->name;
}

public function getModule() {
return $this->module;
}

public function getController() {
if ($this->handler instanceof \Closure) {
return $this->handler;
Expand All @@ -48,7 +58,15 @@ public function getAction() {
return $this->handler[1];
}

public function getMiddleware() {
public function getArgs(): array {
return $this->args;
}

public function getDefaults(): array {
return $this->defaults;
}

public function getMiddleware() : array {
$middleware = $this->middleware;
if (!$this->handler instanceof \Closure) {
list($controller, $method) = $this->handler;
Expand All @@ -70,7 +88,7 @@ protected static function methodExcludedByOptions($method, array $options) {
(! empty($options['except']) && in_array($method, (array) $options['except']));
}

public function run() {
public function run(RequestInterface $request) {
//非闭包函数时实列化对象
if ($this->handler instanceof \Closure) {
$controllerHandler = $this->handler;
Expand All @@ -84,6 +102,7 @@ public function run() {
$controllerHandler = [$classObj, $method];
}

array_unshift($this->args, $request);
$funArgs = $this->args;
if (!empty($this->defaults)) {
$funArgs = array_merge($funArgs, $this->defaults);
Expand Down

0 comments on commit 3873ac2

Please sign in to comment.