Chris@0: callback = $callback; Chris@0: $this->request = $request; Chris@0: $this->response = $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Allow retrieving the request, response and callback as properties Chris@0: * Chris@0: * @param string $name Chris@0: * @return mixed Chris@0: * @throws OutOfBoundsException for invalid properties Chris@0: */ Chris@0: public function __get($name) Chris@0: { Chris@0: if (! property_exists($this, $name)) { Chris@0: throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server'); Chris@0: } Chris@0: return $this->{$name}; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set alternate response emitter to use. Chris@0: * Chris@0: * @param Response\EmitterInterface $emitter Chris@0: */ Chris@0: public function setEmitter(Response\EmitterInterface $emitter) Chris@0: { Chris@0: $this->emitter = $emitter; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create a Server instance Chris@0: * Chris@0: * Creates a server instance from the callback and the following Chris@0: * PHP environmental values: Chris@0: * Chris@0: * - server; typically this will be the $_SERVER superglobal Chris@0: * - query; typically this will be the $_GET superglobal Chris@0: * - body; typically this will be the $_POST superglobal Chris@0: * - cookies; typically this will be the $_COOKIE superglobal Chris@0: * - files; typically this will be the $_FILES superglobal Chris@0: * Chris@0: * @param callable $callback Chris@0: * @param array $server Chris@0: * @param array $query Chris@0: * @param array $body Chris@0: * @param array $cookies Chris@0: * @param array $files Chris@0: * @return static Chris@0: */ Chris@0: public static function createServer( Chris@0: callable $callback, Chris@0: array $server, Chris@0: array $query, Chris@0: array $body, Chris@0: array $cookies, Chris@0: array $files Chris@0: ) { Chris@0: $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files); Chris@0: $response = new Response(); Chris@0: return new static($callback, $request, $response); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create a Server instance from an existing request object Chris@0: * Chris@0: * Provided a callback, an existing request object, and optionally an Chris@0: * existing response object, create and return the Server instance. Chris@0: * Chris@0: * If no Response object is provided, one will be created. Chris@0: * Chris@0: * @param callable $callback Chris@0: * @param ServerRequestInterface $request Chris@0: * @param null|ResponseInterface $response Chris@0: * @return static Chris@0: */ Chris@0: public static function createServerFromRequest( Chris@0: callable $callback, Chris@0: ServerRequestInterface $request, Chris@0: ResponseInterface $response = null Chris@0: ) { Chris@0: if (! $response) { Chris@0: $response = new Response(); Chris@0: } Chris@0: return new static($callback, $request, $response); Chris@0: } Chris@0: Chris@0: /** Chris@0: * "Listen" to an incoming request Chris@0: * Chris@0: * If provided a $finalHandler, that callable will be used for Chris@0: * incomplete requests. Chris@0: * Chris@0: * @param null|callable $finalHandler Chris@0: */ Chris@0: public function listen(callable $finalHandler = null) Chris@0: { Chris@0: $callback = $this->callback; Chris@0: Chris@0: $response = $callback($this->request, $this->response, $finalHandler); Chris@0: if (! $response instanceof ResponseInterface) { Chris@0: $response = $this->response; Chris@0: } Chris@12: Chris@12: $this->getEmitter()->emit($response); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieve the current response emitter. Chris@0: * Chris@0: * If none has been registered, lazy-loads a Response\SapiEmitter. Chris@0: * Chris@0: * @return Response\EmitterInterface Chris@0: */ Chris@0: private function getEmitter() Chris@0: { Chris@0: if (! $this->emitter) { Chris@0: $this->emitter = new Response\SapiEmitter(); Chris@0: } Chris@0: Chris@0: return $this->emitter; Chris@0: } Chris@0: }