-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mpscholten/psr7
Added Psr7 integration
- Loading branch information
Showing
12 changed files
with
421 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
// This example shows how to use this library with the Psr7 `ServerRequestInterface` | ||
// To try out this example do the following: | ||
// - Install dependencies: `composer install` | ||
// - Start webserver: `cd examples && php -S localhost:8080` | ||
// - Open in browser: | ||
// | http://localhost:8080/psr7.php?action=hello | ||
// | http://localhost:8080/psr7.php?action=hello&name=yourname | ||
// | http://localhost:8080/psr7.php?action=helloWithDefault | ||
// | http://localhost:8080/psr7.php?action=json&payload={%22a%22:1} | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use MPScholten\RequestParser\Psr7\ControllerHelperTrait; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
$symfonyRequest = Request::createFromGlobals(); | ||
$psr7Factory = new DiactorosFactory(); | ||
$request = $psr7Factory->createRequest($symfonyRequest); | ||
|
||
class MyController | ||
{ | ||
use ControllerHelperTrait; | ||
|
||
public function __construct(ServerRequestInterface $request) | ||
{ | ||
$this->initRequestParser($request); | ||
} | ||
|
||
public function hello() | ||
{ | ||
$name = $this->queryParameter('name')->string()->required(); | ||
|
||
return "Hello $name"; | ||
} | ||
|
||
public function helloWithDefault() | ||
{ | ||
$name = $this->queryParameter('name')->string()->defaultsTo('unknown'); | ||
|
||
return "Hello $name"; | ||
} | ||
|
||
public function json() | ||
{ | ||
$payload = $this->queryParameter('payload')->json()->required(); | ||
|
||
return print_r($payload, true); | ||
} | ||
} | ||
|
||
$controller = new MyController($request); | ||
$action = $request->getQueryParams()['action']; | ||
|
||
try { | ||
echo $controller->$action(); | ||
} catch (\MPScholten\RequestParser\NotFoundException $e) { | ||
echo $e->getMessage(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace MPScholten\RequestParser; | ||
|
||
trait BaseControllerHelperTrait | ||
{ | ||
/** | ||
* @var RequestParser | ||
*/ | ||
private $queryParser; | ||
|
||
/** | ||
* @var RequestParser | ||
*/ | ||
private $bodyParser; | ||
|
||
protected final function initRequestParser($request, callable $exceptionFactory = null) | ||
{ | ||
/** @var $requestParserFactory RequestParserFactory */ | ||
$requestParserFactory = $this->createRequestParserFactory($request, $exceptionFactory); | ||
$this->queryParser = $requestParserFactory->createQueryParser(); | ||
$this->bodyParser = $requestParserFactory->createBodyParser(); | ||
} | ||
|
||
/** | ||
* Use this method to access the query parameters of the request. | ||
* | ||
* $page = $this->queryParameter('page')->int()->defaultsTo(0) | ||
* | ||
* @param string $name | ||
* @return TypeParser | ||
*/ | ||
protected function queryParameter($name) | ||
{ | ||
return $this->queryParser->get($name); | ||
} | ||
|
||
/** | ||
* Use this method to access the body parameters of the request (e.g. $_POST). | ||
* | ||
* $password = $this->bodyParameter('password')->string()->required() | ||
* | ||
* @param string $name | ||
* @return TypeParser | ||
*/ | ||
protected function bodyParameter($name) | ||
{ | ||
return $this->bodyParser->get($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace MPScholten\RequestParser\Psr7; | ||
|
||
use MPScholten\RequestParser\BaseControllerHelperTrait; | ||
|
||
trait ControllerHelperTrait | ||
{ | ||
use BaseControllerHelperTrait; | ||
|
||
protected final function createRequestParserFactory($request, $exceptionFactory) | ||
{ | ||
return new Psr7RequestParserFactory($request, $exceptionFactory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace MPScholten\RequestParser\Psr7; | ||
|
||
use MPScholten\RequestParser\RequestParser; | ||
use MPScholten\RequestParser\RequestParserFactory; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class Psr7RequestParserFactory implements RequestParserFactory | ||
{ | ||
private $request; | ||
private $exceptionFactory; | ||
|
||
public function __construct(ServerRequestInterface $request, $exceptionFactory = null) | ||
{ | ||
$this->request = $request; | ||
$this->exceptionFactory = $exceptionFactory; | ||
} | ||
|
||
public function createQueryParser() | ||
{ | ||
$query = $this->request->getQueryParams(); | ||
|
||
$readParameter = function ($name) use ($query) { | ||
if (!isset($query[$name])) { | ||
return null; | ||
} | ||
|
||
return $query[$name]; | ||
}; | ||
|
||
return new RequestParser($readParameter, $this->exceptionFactory); | ||
} | ||
|
||
public function createBodyParser() | ||
{ | ||
$body = $this->request->getParsedBody(); | ||
|
||
$readParameter = function ($name) use ($body) { | ||
if (!isset($body[$name])) { | ||
return null; | ||
} | ||
|
||
return $body[$name]; | ||
}; | ||
|
||
return new RequestParser($readParameter, $this->exceptionFactory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace MPScholten\RequestParser; | ||
|
||
interface RequestParserFactory | ||
{ | ||
/** | ||
* @return RequestParser | ||
*/ | ||
public function createQueryParser(); | ||
|
||
/** | ||
* @return RequestParser | ||
*/ | ||
public function createBodyParser(); | ||
} |
Oops, something went wrong.