3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use OutOfBoundsException;
13 use Psr\Http\Message\ServerRequestInterface;
14 use Psr\Http\Message\ResponseInterface;
17 * "Serve" incoming HTTP requests
19 * Given a callback, takes an incoming request, dispatches it to the
20 * callback, and then sends a response.
30 * Response emitter to use; by default, uses Response\SapiEmitter.
32 * @var Response\EmitterInterface
37 * @var ServerRequestInterface
42 * @var ResponseInterface
49 * Given a callback, a request, and a response, we can create a server.
51 * @param callable $callback
52 * @param ServerRequestInterface $request
53 * @param ResponseInterface $response
55 public function __construct(
57 ServerRequestInterface $request,
58 ResponseInterface $response
60 $this->callback = $callback;
61 $this->request = $request;
62 $this->response = $response;
66 * Allow retrieving the request, response and callback as properties
70 * @throws OutOfBoundsException for invalid properties
72 public function __get($name)
74 if (! property_exists($this, $name)) {
75 throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server');
77 return $this->{$name};
81 * Set alternate response emitter to use.
83 * @param Response\EmitterInterface $emitter
85 public function setEmitter(Response\EmitterInterface $emitter)
87 $this->emitter = $emitter;
91 * Create a Server instance
93 * Creates a server instance from the callback and the following
94 * PHP environmental values:
96 * - server; typically this will be the $_SERVER superglobal
97 * - query; typically this will be the $_GET superglobal
98 * - body; typically this will be the $_POST superglobal
99 * - cookies; typically this will be the $_COOKIE superglobal
100 * - files; typically this will be the $_FILES superglobal
102 * @param callable $callback
103 * @param array $server
104 * @param array $query
106 * @param array $cookies
107 * @param array $files
110 public static function createServer(
118 $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files);
119 $response = new Response();
120 return new static($callback, $request, $response);
124 * Create a Server instance from an existing request object
126 * Provided a callback, an existing request object, and optionally an
127 * existing response object, create and return the Server instance.
129 * If no Response object is provided, one will be created.
131 * @param callable $callback
132 * @param ServerRequestInterface $request
133 * @param null|ResponseInterface $response
136 public static function createServerFromRequest(
138 ServerRequestInterface $request,
139 ResponseInterface $response = null
142 $response = new Response();
144 return new static($callback, $request, $response);
148 * "Listen" to an incoming request
150 * If provided a $finalHandler, that callable will be used for
151 * incomplete requests.
153 * Output buffering is enabled prior to invoking the attached
154 * callback; any output buffered will be sent prior to any
155 * response body content.
157 * @param null|callable $finalHandler
159 public function listen(callable $finalHandler = null)
161 $callback = $this->callback;
164 $bufferLevel = ob_get_level();
166 $response = $callback($this->request, $this->response, $finalHandler);
167 if (! $response instanceof ResponseInterface) {
168 $response = $this->response;
170 $this->getEmitter()->emit($response, $bufferLevel);
174 * Retrieve the current response emitter.
176 * If none has been registered, lazy-loads a Response\SapiEmitter.
178 * @return Response\EmitterInterface
180 private function getEmitter()
182 if (! $this->emitter) {
183 $this->emitter = new Response\SapiEmitter();
186 return $this->emitter;