diff --git a/README.md b/README.md index 7fc93af..4e49c8a 100755 --- a/README.md +++ b/README.md @@ -66,8 +66,10 @@ $app = new Ypf\Application($services); $app->run(); ``` + swoole performance -``` + +```bash wrk -t 20 -c 200 -d 30s "http://127.0.0.1:8080/" Running 30s test @ http://127.0.0.1:8080/ 20 threads and 200 connections diff --git a/src/Route/Middleware.php b/src/Route/Middleware.php index cf37493..7aae8c6 100644 --- a/src/Route/Middleware.php +++ b/src/Route/Middleware.php @@ -27,6 +27,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface { try { $route = $this->router->dispatch($request); + foreach ($route->getHeaders() as $header => $required) { + if ((bool) $required && !$request->hasHeader($header)) { + throw new MissingHeaderException($header); + } + } + if (!$route->hasMethod($request->getMethod())) { + throw new MethodNotAllowedException($route->getMethod()); + } + foreach ($route->getParameters() as $attr => $value) { $request = $request->withAttribute($attr, $value); } diff --git a/src/Route/Route.php b/src/Route/Route.php index d39a342..b5b5c01 100644 --- a/src/Route/Route.php +++ b/src/Route/Route.php @@ -4,13 +4,9 @@ namespace Ypf\Route; -use GuzzleHttp\Psr7\Response; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\ResponseInterface; use InvalidArgumentException; use Psr\Container\ContainerInterface; -use Ypf\Route\Exception\MissingHeaderException; -use Ypf\Route\Exception\MethodNotAllowedException; class Route { @@ -132,23 +128,4 @@ public function isMatch(ServerRequestInterface $request): bool return false; } - - public function handle(ServerRequestInterface $request): ResponseInterface - { - foreach ($this->getHeaders() as $header => $required) { - if ((bool) $required && !$request->hasHeader($header)) { - throw new MissingHeaderException($header); - } - } - if (!$this->hasMethod($request->getMethod())) { - throw new MethodNotAllowedException($this->getMethod()); - } - $response = call_user_func($this->getCallable(), $request); - - if (is_string($response)) { - $response = new Response(200, [], $response); - } - - return $response; - } } diff --git a/src/Route/Router.php b/src/Route/Router.php index 1e87881..eb47dc5 100644 --- a/src/Route/Router.php +++ b/src/Route/Router.php @@ -66,11 +66,6 @@ public function dispatch(ServerRequestInterface $request): Route foreach ($routes as $route) { if ($route->isMatch($request)) { return $route; - foreach ($route->getParameters() as $attr => $value) { - $request = $request->withAttribute($attr, $value); - } - - return $route->handle($request); } }