Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加rest路由 #42

Merged
merged 3 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/aop/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"require": {
"php": "^8.0",
"ext-pcntl": "*",
"max/utils": "^1.0",
"max/di": "^1.0",
"max/utils": "dev-master",
"max/di": "dev-master",
"nikic/php-parser": "^4.13",
"symfony/finder": "*"
}
Expand Down
4 changes: 2 additions & 2 deletions src/cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
},
"require": {
"php": "^8.0",
"max/utils": "^1.0",
"max/utils": "dev-master",
"psr/simple-cache": "^1.0",
"max/redis": "^1.0"
"max/redis": "dev-master"
},
"extra": {
"max": {
Expand Down
2 changes: 1 addition & 1 deletion src/config/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require": {
"php": "^8.0",
"max/utils": "^1.0"
"max/utils": "dev-master"
},
"extra": {
"max": {
Expand Down
6 changes: 3 additions & 3 deletions src/database/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"php": "^8.0",
"ext-pdo": "*",
"ext-json": "*",
"max/utils": "^1.0",
"max/config": "^1.0",
"max/context": "^1.0"
"max/utils": "dev-master",
"max/config": "dev-master",
"max/context": "dev-master"
},
"extra": {
"max": {
Expand Down
2 changes: 1 addition & 1 deletion src/framework/src/Console/Command/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function execute(InputInterface $input, OutputInterface $output)
/** @var Route $route */
$action = $route->getAction();
if (is_array($action)) {
$action = implode('::', $action);
$action = implode('@', $action);
} elseif ($action instanceof Closure) {
$action = 'Closure';
}
Expand Down
2 changes: 1 addition & 1 deletion src/http-message/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"require": {
"php": "^8.0",
"psr/http-message": "^1.0",
"max/utils": "^1.0"
"max/utils": "dev-master"
}
}
8 changes: 4 additions & 4 deletions src/http-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
],
"require": {
"php": "^8.0",
"max/http-message": "^1.0",
"max/utils": "^1.0",
"max/routing": "^1.0",
"max/di": "^1.0",
"max/http-message": "dev-master",
"max/utils": "dev-master",
"max/routing": "dev-master",
"max/di": "dev-master",
"psr/http-server-middleware": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/event-dispatcher": "^1.0"
Expand Down
4 changes: 2 additions & 2 deletions src/http-server/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function through(ServerRequestInterface $request): ResponseInterface
/**
* 添加中间件.
*/
public function use(string|array $middleware): static
public function use(string ...$middleware): static
{
array_push($this->middlewares, ...(array) $middleware);
array_push($this->middlewares, ...$middleware);
return $this;
}

Expand Down
6 changes: 4 additions & 2 deletions src/http-server/src/Middleware/RoutingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public function __construct(
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$route = $this->routeCollector->resolveRequest($request);
return $handler->use($route->getMiddlewares())->handle($request->withAttribute(Route::class, $route));
$route = $this->routeCollector->resolveRequest($request);
$request = $request->withAttribute(Route::class, $route);

return $handler->use(...$route->getMiddlewares())->handle($request);
}
}
18 changes: 5 additions & 13 deletions src/http-server/src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionException;
use RuntimeException;

class RequestHandler implements RequestHandlerInterface
{
/**
* 容器是否有make方法.
*/
private bool $hasMakeMethod;

public function __construct(
protected ContainerInterface $container,
protected array $middlewares = []
) {
$this->hasMakeMethod = method_exists($this->container, 'make');
}

/**
Expand All @@ -42,20 +37,17 @@ public function __construct(
public function handle(ServerRequestInterface $request): ResponseInterface
{
if ($middleware = array_shift($this->middlewares)) {
return $this->handleMiddleware(
$this->hasMakeMethod ? $this->container->make($middleware) : new $middleware(),
$request
);
return $this->handleMiddleware($this->container->make($middleware), $request);
}
return $this->handleRequest($request);
}

/**
* 添加中间件.
*/
public function use(string|array $middleware): static
public function use(string ...$middleware): static
{
array_push($this->middlewares, ...(array) $middleware);
array_push($this->middlewares, ...$middleware);
return $this;
}

Expand All @@ -70,7 +62,7 @@ protected function handleRequest(ServerRequestInterface $request): ResponseInter
$parameters['request'] = $request;
return $this->container->call($route->getAction(), $parameters);
}
throw new RouteNotFoundException('No route in request attributes', 404);
throw new RuntimeException('No route found in the request context', 404);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/routing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": "^8.0",
"max/http-message": "^1.0"
"max/http-message": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
99 changes: 99 additions & 0 deletions src/routing/src/RestRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Max\Routing;

class RestRouter
{
/**
* Rest路由规则
*
* @var array|array[]
*/
protected static array $maps = [
'index' => [['GET', 'HEAD'], '/%s'],
'show' => [['GET', 'HEAD'], '/%s/{id}'],
'store' => [['POST'], '/%s'],
'update' => [['PUT', 'PATCH'], '/%s/{id}'],
'delete' => [['DELETE'], '/%s/{id}'],
];

/**
* @var Route
*/
protected Route $index;
/**
* @var Route
*/
protected Route $show;
/**
* @var Route
*/
protected Route $store;
/**
* @var Route
*/
protected Route $update;
/**
* @var Route
*/
protected Route $delete;

public function __construct(
protected RouteCollector $routeCollector,
protected string $uri,
protected string $controller,
protected array $middlewares = [],
protected array $patterns = [],
) {
foreach (static::$maps as $action => $map) {
[$methods, $path] = $map;
$this->routeCollector->addRoute($this->{$action} = new Route(
$methods,
sprintf($path, $uri),
[$this->controller, $action],
$this->patterns,
$this->middlewares,
));
}
}

/**
* @return Route
*/
public function getIndex(): Route
{
return $this->index;
}

/**
* @return Route
*/
public function getShow(): Route
{
return $this->show;
}

/**
* @return Route
*/
public function getStore(): Route
{
return $this->store;
}

/**
* @return Route
*/
public function getUpdate(): Route
{
return $this->update;
}

/**
* @return Route
*/
public function getDelete(): Route
{
return $this->delete;
}
}
33 changes: 16 additions & 17 deletions src/routing/src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use InvalidArgumentException;
use Max\Http\Message\Contract\RequestMethodInterface;

use function array_merge;
use function array_unique;
use function sprintf;

Expand Down Expand Up @@ -46,10 +45,7 @@ public function __construct(
*/
public function any(string $path, array|Closure|string $action): Route
{
return $this->request(
$path,
$action,
[
return $this->request($path, $action, [
RequestMethodInterface::METHOD_GET,
RequestMethodInterface::METHOD_HEAD,
RequestMethodInterface::METHOD_POST,
Expand Down Expand Up @@ -101,6 +97,20 @@ public function get(string $uri, string|array|Closure $action): Route
return $this->request($uri, $action, [RequestMethodInterface::METHOD_GET, RequestMethodInterface::METHOD_HEAD]);
}

/**
* Restful路由
*/
public function rest(string $uri, string $controller): RestRouter
{
return new RestRouter(
$this->routeCollector,
$this->prefix . ltrim($uri, '/'),
$this->formatController($controller),
$this->middlewares,
$this->patterns,
);
}

/**
* Allow multi request methods.
*/
Expand All @@ -114,7 +124,7 @@ public function request(string $path, array|Closure|string $action, array $metho
if ($action instanceof Closure || count($action) === 2) {
if (is_array($action)) {
[$controller, $action] = $action;
$action = [$this->formatController($controller), $action];
$action = [$this->formatController($controller), $action];
}
return $this->routeCollector->addRoute(new Route($methods, $this->prefix . $path, $action, $this->patterns, $this->middlewares));
}
Expand All @@ -140,17 +150,6 @@ public function middleware(string ...$middlewares): Router
return $new;
}

/**
* 变量规则.
*/
public function patterns(array $patterns): Router
{
$new = clone $this;
$new->patterns = array_merge($this->patterns, $patterns);

return $new;
}

/**
* 单个变量规则.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/session/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": "^8.0",
"max/utils": "^1.0",
"max/redis": "^1.0"
"max/utils": "dev-master",
"max/redis": "dev-master"
}
}
2 changes: 1 addition & 1 deletion src/utils/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"require": {
"php": "^8.0",
"max/macro": "^1.0",
"max/macro": "dev-master",
"ramsey/uuid": "^4.2",
"symfony/mime": "*",
"voku/portable-ascii": "^1.5",
Expand Down
4 changes: 2 additions & 2 deletions src/view/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
],
"require": {
"php": "^8.0",
"max/utils": "^1.0",
"max/config": "^1.0"
"max/utils": "dev-master",
"max/config": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down