diff --git a/src/aop/composer.json b/src/aop/composer.json index 8198a5d1..57eab748 100644 --- a/src/aop/composer.json +++ b/src/aop/composer.json @@ -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": "*" } diff --git a/src/cache/composer.json b/src/cache/composer.json index 1fd6e266..adbc0c5e 100644 --- a/src/cache/composer.json +++ b/src/cache/composer.json @@ -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": { diff --git a/src/config/composer.json b/src/config/composer.json index fcdb7957..0b51b790 100644 --- a/src/config/composer.json +++ b/src/config/composer.json @@ -19,7 +19,7 @@ }, "require": { "php": "^8.0", - "max/utils": "^1.0" + "max/utils": "dev-master" }, "extra": { "max": { diff --git a/src/database/composer.json b/src/database/composer.json index 3d1fde9e..a1033c9f 100644 --- a/src/database/composer.json +++ b/src/database/composer.json @@ -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": { diff --git a/src/framework/src/Console/Command/RouteListCommand.php b/src/framework/src/Console/Command/RouteListCommand.php index f72a5b17..3e9b7491 100644 --- a/src/framework/src/Console/Command/RouteListCommand.php +++ b/src/framework/src/Console/Command/RouteListCommand.php @@ -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'; } diff --git a/src/http-message/composer.json b/src/http-message/composer.json index aaa30f8c..22d0cc44 100644 --- a/src/http-message/composer.json +++ b/src/http-message/composer.json @@ -20,6 +20,6 @@ "require": { "php": "^8.0", "psr/http-message": "^1.0", - "max/utils": "^1.0" + "max/utils": "dev-master" } } diff --git a/src/http-server/composer.json b/src/http-server/composer.json index caa3b1ef..18da7f2b 100644 --- a/src/http-server/composer.json +++ b/src/http-server/composer.json @@ -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" diff --git a/src/http-server/src/Kernel.php b/src/http-server/src/Kernel.php index e018032f..a0a95e51 100644 --- a/src/http-server/src/Kernel.php +++ b/src/http-server/src/Kernel.php @@ -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; } diff --git a/src/http-server/src/Middleware/RoutingMiddleware.php b/src/http-server/src/Middleware/RoutingMiddleware.php index 00c5f299..0dea8488 100644 --- a/src/http-server/src/Middleware/RoutingMiddleware.php +++ b/src/http-server/src/Middleware/RoutingMiddleware.php @@ -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); } } diff --git a/src/http-server/src/RequestHandler.php b/src/http-server/src/RequestHandler.php index e8d83d39..c40887ee 100644 --- a/src/http-server/src/RequestHandler.php +++ b/src/http-server/src/RequestHandler.php @@ -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'); } /** @@ -42,10 +37,7 @@ 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); } @@ -53,9 +45,9 @@ public function handle(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; } @@ -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); } /** diff --git a/src/routing/composer.json b/src/routing/composer.json index f4ec01e0..339f8b2e 100644 --- a/src/routing/composer.json +++ b/src/routing/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": "^8.0", - "max/http-message": "^1.0" + "max/http-message": "dev-master" }, "autoload": { "psr-4": { diff --git a/src/routing/src/RestRouter.php b/src/routing/src/RestRouter.php new file mode 100644 index 00000000..57866e83 --- /dev/null +++ b/src/routing/src/RestRouter.php @@ -0,0 +1,99 @@ + [['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; + } +} diff --git a/src/routing/src/Router.php b/src/routing/src/Router.php index ed169036..5f05ba71 100644 --- a/src/routing/src/Router.php +++ b/src/routing/src/Router.php @@ -15,7 +15,6 @@ use InvalidArgumentException; use Max\Http\Message\Contract\RequestMethodInterface; -use function array_merge; use function array_unique; use function sprintf; @@ -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, @@ -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. */ @@ -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)); } @@ -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; - } - /** * 单个变量规则. */ diff --git a/src/session/composer.json b/src/session/composer.json index e7f010c8..1d1685fa 100644 --- a/src/session/composer.json +++ b/src/session/composer.json @@ -16,7 +16,7 @@ }, "require": { "php": "^8.0", - "max/utils": "^1.0", - "max/redis": "^1.0" + "max/utils": "dev-master", + "max/redis": "dev-master" } } diff --git a/src/utils/composer.json b/src/utils/composer.json index 1d429b30..6aeeb75d 100644 --- a/src/utils/composer.json +++ b/src/utils/composer.json @@ -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", diff --git a/src/view/composer.json b/src/view/composer.json index 4c571833..5e2634fb 100644 --- a/src/view/composer.json +++ b/src/view/composer.json @@ -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": {