3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2018 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\ResponseInterface;
12 use Psr\Http\Message\ServerRequestInterface;
14 use function property_exists;
17 * "Serve" incoming HTTP requests
19 * Given a callback, takes an incoming request, dispatches it to the
20 * callback, and then sends a response.
22 * @deprecated since 1.8.0. We recommend using the `RequestHandlerRunner` class
23 * from the zendframework/zend-httphandlerrunner package instead.
33 * Response emitter to use; by default, uses Response\SapiEmitter.
35 * @var Response\EmitterInterface
40 * @var ServerRequestInterface
45 * @var ResponseInterface
52 * Given a callback, a request, and a response, we can create a server.
54 * @param callable $callback
55 * @param ServerRequestInterface $request
56 * @param ResponseInterface $response
58 public function __construct(
60 ServerRequestInterface $request,
61 ResponseInterface $response
63 $this->callback = $callback;
64 $this->request = $request;
65 $this->response = $response;
69 * Allow retrieving the request, response and callback as properties
73 * @throws OutOfBoundsException for invalid properties
75 public function __get($name)
77 if (! property_exists($this, $name)) {
78 throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server');
80 return $this->{$name};
84 * Set alternate response emitter to use.
86 * @param Response\EmitterInterface $emitter
88 public function setEmitter(Response\EmitterInterface $emitter)
90 $this->emitter = $emitter;
94 * Create a Server instance
96 * Creates a server instance from the callback and the following
97 * PHP environmental values:
99 * - server; typically this will be the $_SERVER superglobal
100 * - query; typically this will be the $_GET superglobal
101 * - body; typically this will be the $_POST superglobal
102 * - cookies; typically this will be the $_COOKIE superglobal
103 * - files; typically this will be the $_FILES superglobal
105 * @param callable $callback
106 * @param array $server
107 * @param array $query
109 * @param array $cookies
110 * @param array $files
113 public static function createServer(
121 $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files);
122 $response = new Response();
123 return new static($callback, $request, $response);
127 * Create a Server instance from an existing request object
129 * Provided a callback, an existing request object, and optionally an
130 * existing response object, create and return the Server instance.
132 * If no Response object is provided, one will be created.
134 * @param callable $callback
135 * @param ServerRequestInterface $request
136 * @param null|ResponseInterface $response
139 public static function createServerFromRequest(
141 ServerRequestInterface $request,
142 ResponseInterface $response = null
145 $response = new Response();
147 return new static($callback, $request, $response);
151 * "Listen" to an incoming request
153 * If provided a $finalHandler, that callable will be used for
154 * incomplete requests.
156 * @param null|callable $finalHandler
158 public function listen(callable $finalHandler = null)
160 $callback = $this->callback;
162 $response = $callback($this->request, $this->response, $finalHandler);
163 if (! $response instanceof ResponseInterface) {
164 $response = $this->response;
167 $this->getEmitter()->emit($response);
171 * Retrieve the current response emitter.
173 * If none has been registered, lazy-loads a Response\SapiEmitter.
175 * @return Response\EmitterInterface
177 private function getEmitter()
179 if (! $this->emitter) {
180 $this->emitter = new Response\SapiEmitter();
183 return $this->emitter;