Skip to content

Instantly share code, notes, and snippets.

@n1215
Created May 11, 2016 16:44
Show Gist options
  • Save n1215/a3ef3f644bb795dd204c70e730dc3fab to your computer and use it in GitHub Desktop.
Save n1215/a3ef3f644bb795dd204c70e730dc3fab to your computer and use it in GitHub Desktop.
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