Skip to content

Commit

Permalink
update router&readme
Browse files Browse the repository at this point in the history
  • Loading branch information
millken committed Sep 20, 2018
1 parent 8540ec3 commit ff9a528
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 29 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/Route/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 0 additions & 23 deletions src/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
5 changes: 0 additions & 5 deletions src/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit ff9a528

Please sign in to comment.