Skip to content

Commit

Permalink
Merge pull request #8 from legionth/use-arguments-variable-in-callback
Browse files Browse the repository at this point in the history
Use arguments variable in callback instead of query parameters
  • Loading branch information
legionth authored Feb 14, 2019
2 parents 6142a3a + 640922b commit 297ba46
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
$word = $request->getQueryParams()['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
Expand Down
6 changes: 3 additions & 3 deletions examples/01-define-api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once '../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

Expand All @@ -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);
});
Expand Down
8 changes: 3 additions & 5 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -86,18 +86,18 @@ public function testCreatePostEndpointOnTestServerAndSendRequestWithQueryParamet
$this->assertEquals('10', $idAssertion);
}

public function testCreatePutEndpointOnTestServerAndSendRequestWithQueryParameters()
public function testCreatePutEndpointOnTestServerAndSendRequestWithParameters()
{
$requestAssertion = null;
$idAssertion = null;
$nameAssertion = null;

$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);
Expand Down

0 comments on commit 297ba46

Please sign in to comment.