From d5364d5a1a576796456983760102711491c21a8d Mon Sep 17 00:00:00 2001 From: Niels Theen Date: Mon, 11 Feb 2019 16:27:55 +0100 Subject: [PATCH 1/4] Use arguments variable in callback instead using query paramters --- README.md | 4 ++-- examples/01-define-api.php | 4 ++-- src/Server.php | 8 +++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 46db6fa..f814415 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ the request to following endpoint. To add dynamic values in the REST API definition the operator `:` can be used ```php -$server->post('/say/:word', function (\Psr\Http\Message\ServerRequestInterface $request, callable $next) { - $word = $request->getQueryParams()['word']; +$server->post('/say/:word', function (\Psr\Http\Message\ServerRequestInterface $request, callable $next, array $arguments) { + $word = $arguments['word']; return new \React\Http\Response(200, array(), 'You said: ' . $word); }); diff --git a/examples/01-define-api.php b/examples/01-define-api.php index 176fbc7..29886c0 100644 --- a/examples/01-define-api.php +++ b/examples/01-define-api.php @@ -10,8 +10,8 @@ return new \React\Http\Response(200, array(), 'hello'); }); -$server->post('/say/:word', function (\Psr\Http\Message\ServerRequestInterface $request, callable $next) { - $word = $request->getQueryParams()['word']; +$server->post('/say/:word', function (\Psr\Http\Message\ServerRequestInterface $request, callable $next, array $arguments) { + $word = $arguments['word']; return new \React\Http\Response(200, array(), 'You said: ' . $word); }); diff --git a/src/Server.php b/src/Server.php index 96bd788..2589769 100644 --- a/src/Server.php +++ b/src/Server.php @@ -103,19 +103,17 @@ private function createRestfulFunction($httpMethod, $path, $function) return $next($request); } - $queryParams = $request->getQueryParams(); + $argument = array(); foreach ($pathArray as $id => $valueName) { $position = strpos($valueName, ':'); if (0 === $position) { $valueName = substr($valueName, 1); - $queryParams[$valueName] = $requestPathArray[$id]; + $argument[$valueName] = $requestPathArray[$id]; } } - $request = $request->withQueryParams($queryParams); - - return $function($request, $next); + return $function($request, $next, $argument); } return $next($request); From bc76af5d90ab5104e54ed67542ad76f4192acb69 Mon Sep 17 00:00:00 2001 From: Niels Theen Date: Tue, 12 Feb 2019 08:28:42 +0100 Subject: [PATCH 2/4] Use relative path to execute script from anywhere --- examples/01-define-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/01-define-api.php b/examples/01-define-api.php index 29886c0..bcf3d4f 100644 --- a/examples/01-define-api.php +++ b/examples/01-define-api.php @@ -1,6 +1,6 @@ Date: Tue, 12 Feb 2019 08:43:49 +0100 Subject: [PATCH 3/4] Adapt Unittests to new behavior --- tests/ServerTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 415f26f..f7be1e3 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -59,16 +59,16 @@ public function testCreateGetEndpointOnTestServerAndSendRequest() $this->assertSame('example.com', $requestAssertion->getHeaderLine('Host')); } - public function testCreatePostEndpointOnTestServerAndSendRequestWithQueryParameter() + public function testCreatePostEndpointOnTestServerAndSendRequestWithAdditionalParameter() { $requestAssertion = null; $idAssertion = null; $server = new Server(); - $server->post('/user/add/:id', function (ServerRequestInterface $request, callable $next) use (&$requestAssertion, &$idAssertion) { + $server->post('/user/add/:id', function (ServerRequestInterface $request, callable $next, array $paramters) use (&$requestAssertion, &$idAssertion) { $requestAssertion = $request; - $idAssertion = $request->getQueryParams()['id']; + $idAssertion = $paramters['id']; }); $server->listen($this->socket); @@ -86,7 +86,7 @@ public function testCreatePostEndpointOnTestServerAndSendRequestWithQueryParamet $this->assertEquals('10', $idAssertion); } - public function testCreatePutEndpointOnTestServerAndSendRequestWithQueryParameters() + public function testCreatePutEndpointOnTestServerAndSendRequestWithParameters() { $requestAssertion = null; $idAssertion = null; @@ -94,10 +94,10 @@ public function testCreatePutEndpointOnTestServerAndSendRequestWithQueryParamete $server = new Server(); - $server->put('/user/add/:id/group/:name', function (ServerRequestInterface $request, callable $next) use (&$requestAssertion, &$idAssertion, &$nameAssertion) { + $server->put('/user/add/:id/group/:name', function (ServerRequestInterface $request, callable $next, array $parameters) use (&$requestAssertion, &$idAssertion, &$nameAssertion) { $requestAssertion = $request; - $idAssertion = $request->getQueryParams()['id']; - $nameAssertion = $request->getQueryParams()['name']; + $idAssertion = $parameters['id']; + $nameAssertion = $parameters['name']; }); $server->listen($this->socket); From 640922b6c0beb8719a788d5815b9de6916e35ccf Mon Sep 17 00:00:00 2001 From: Niels Theen Date: Wed, 13 Feb 2019 10:31:30 +0100 Subject: [PATCH 4/4] Adapt README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f814415..af597c4 100644 --- a/README.md +++ b/README.md @@ -59,16 +59,16 @@ the request to following endpoint. To add dynamic values in the REST API definition the operator `:` can be used ```php -$server->post('/say/:word', function (\Psr\Http\Message\ServerRequestInterface $request, callable $next, array $arguments) { - $word = $arguments['word']; +$server->post('/say/:word', function (\Psr\Http\Message\ServerRequestInterface $request, callable $next, array $parameters) { + $word = $parameters['word']; return new \React\Http\Response(200, array(), 'You said: ' . $word); }); ``` Now a HTTP client can call the address e.g. `http://localhost:8080/say/hello`. -The key `word` and value `hello` will be stored in query parameters of the -PSR-7 request. +The key `word` and value `hello` will be stored in the third +parameter of the callback function. There is no type check her that can validate which API should be used. `/say/:word` and`/say/:number` would be the same. In this case the order of your API