Created
May 11, 2016 16:44
-
-
Save n1215/a3ef3f644bb795dd204c70e730dc3fab to your computer and use it in GitHub Desktop.
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
trait RequiresServerRequestTrait | |
{ | |
/** | |
* @param RequestInterface $request | |
* @return ServerRequestInterface | |
* @throws ServerRequestRequiredException | |
*/ | |
private function ensureServerRequest(RequestInterface $request) | |
{ | |
if (($request instanceof ServerRequestInterface) === false) { | |
throw ServerRequestRequiredException::requiredBy($this); | |
} | |
return $request; // add this line | |
} | |
} | |
class ExampleMiddleware | |
{ | |
use RequiresServerRequestTrait; | |
public function __invoke( | |
RequestInterface $request, | |
ResponseInterface $response, | |
callable $next | |
) { | |
$serverRequest = $this->ensureServerRequest($request); | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment