3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros;
10 use OutOfBoundsException;
11 use Psr\Http\Message\ServerRequestInterface;
12 use Psr\Http\Message\ResponseInterface;
15 * "Serve" incoming HTTP requests
17 * Given a callback, takes an incoming request, dispatches it to the
18 * callback, and then sends a response.
28 * Response emitter to use; by default, uses Response\SapiEmitter.
30 * @var Response\EmitterInterface
35 * @var ServerRequestInterface
40 * @var ResponseInterface
47 * Given a callback, a request, and a response, we can create a server.
49 * @param callable $callback
50 * @param ServerRequestInterface $request
51 * @param ResponseInterface $response
53 public function __construct(
55 ServerRequestInterface $request,
56 ResponseInterface $response
58 $this->callback = $callback;
59 $this->request = $request;
60 $this->response = $response;
64 * Allow retrieving the request, response and callback as properties
68 * @throws OutOfBoundsException for invalid properties
70 public function __get($name)
72 if (! property_exists($this, $name)) {
73 throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server');
75 return $this->{$name};
79 * Set alternate response emitter to use.
81 * @param Response\EmitterInterface $emitter
83 public function setEmitter(Response\EmitterInterface $emitter)
85 $this->emitter = $emitter;
89 * Create a Server instance
91 * Creates a server instance from the callback and the following
92 * PHP environmental values:
94 * - server; typically this will be the $_SERVER superglobal
95 * - query; typically this will be the $_GET superglobal
96 * - body; typically this will be the $_POST superglobal
97 * - cookies; typically this will be the $_COOKIE superglobal
98 * - files; typically this will be the $_FILES superglobal
100 * @param callable $callback
101 * @param array $server
102 * @param array $query
104 * @param array $cookies
105 * @param array $files
108 public static function createServer(
116 $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files);
117 $response = new Response();
118 return new static($callback, $request, $response);
122 * Create a Server instance from an existing request object
124 * Provided a callback, an existing request object, and optionally an
125 * existing response object, create and return the Server instance.
127 * If no Response object is provided, one will be created.
129 * @param callable $callback
130 * @param ServerRequestInterface $request
131 * @param null|ResponseInterface $response
134 public static function createServerFromRequest(
136 ServerRequestInterface $request,
137 ResponseInterface $response = null
140 $response = new Response();
142 return new static($callback, $request, $response);
146 * "Listen" to an incoming request
148 * If provided a $finalHandler, that callable will be used for
149 * incomplete requests.
151 * @param null|callable $finalHandler
153 public function listen(callable $finalHandler = null)
155 $callback = $this->callback;
157 $response = $callback($this->request, $this->response, $finalHandler);
158 if (! $response instanceof ResponseInterface) {
159 $response = $this->response;
162 $this->getEmitter()->emit($response);
166 * Retrieve the current response emitter.
168 * If none has been registered, lazy-loads a Response\SapiEmitter.
170 * @return Response\EmitterInterface
172 private function getEmitter()
174 if (! $this->emitter) {
175 $this->emitter = new Response\SapiEmitter();
178 return $this->emitter;