Phly\Http\Server::createServerFromRequest PHP Method

createServerFromRequest() public static method

Provided a callback, an existing request object, and optionally an existing response object, create and return the Server instance. If no Response object is provided, one will be created.
public static createServerFromRequest ( callable $callback, Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response = null ) : static
$callback callable
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface
return static
    public static function createServerFromRequest(callable $callback, ServerRequestInterface $request, ResponseInterface $response = null)
    {
        if (!$response) {
            $response = new Response();
        }
        return new static($callback, $request, $response);
    }

Usage Example

Example #1
0
 public function testListenPassesCallableArgumentToCallback()
 {
     $phpunit = $this;
     $invoked = false;
     $request = $this->request;
     $response = $this->response;
     $this->response->expects($this->once())->method('getHeaders')->will($this->returnValue([]));
     $final = function ($req, $res, $err = null) use($phpunit, $request, $response, &$invoked) {
         $phpunit->assertSame($request, $req);
         $phpunit->assertSame($response, $res);
         $invoked = true;
     };
     $callback = function ($req, $res, callable $final = null) use($phpunit) {
         if (!$final) {
             $phpunit->fail('No final callable passed!');
         }
         $final($req, $res);
     };
     $server = Server::createServerFromRequest($callback, $this->request, $this->response);
     $server->listen($final);
     $this->assertTrue($invoked);
 }