annotate vendor/zendframework/zend-diactoros/src/Server.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Zend Framework (http://framework.zend.com/)
Chris@0 4 *
Chris@0 5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
Chris@0 6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
Chris@0 8 */
Chris@0 9
Chris@0 10 namespace Zend\Diactoros;
Chris@0 11
Chris@0 12 use OutOfBoundsException;
Chris@0 13 use Psr\Http\Message\ServerRequestInterface;
Chris@0 14 use Psr\Http\Message\ResponseInterface;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * "Serve" incoming HTTP requests
Chris@0 18 *
Chris@0 19 * Given a callback, takes an incoming request, dispatches it to the
Chris@0 20 * callback, and then sends a response.
Chris@0 21 */
Chris@0 22 class Server
Chris@0 23 {
Chris@0 24 /**
Chris@0 25 * @var callable
Chris@0 26 */
Chris@0 27 private $callback;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Response emitter to use; by default, uses Response\SapiEmitter.
Chris@0 31 *
Chris@0 32 * @var Response\EmitterInterface
Chris@0 33 */
Chris@0 34 private $emitter;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * @var ServerRequestInterface
Chris@0 38 */
Chris@0 39 private $request;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * @var ResponseInterface
Chris@0 43 */
Chris@0 44 private $response;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Constructor
Chris@0 48 *
Chris@0 49 * Given a callback, a request, and a response, we can create a server.
Chris@0 50 *
Chris@0 51 * @param callable $callback
Chris@0 52 * @param ServerRequestInterface $request
Chris@0 53 * @param ResponseInterface $response
Chris@0 54 */
Chris@0 55 public function __construct(
Chris@0 56 callable $callback,
Chris@0 57 ServerRequestInterface $request,
Chris@0 58 ResponseInterface $response
Chris@0 59 ) {
Chris@0 60 $this->callback = $callback;
Chris@0 61 $this->request = $request;
Chris@0 62 $this->response = $response;
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Allow retrieving the request, response and callback as properties
Chris@0 67 *
Chris@0 68 * @param string $name
Chris@0 69 * @return mixed
Chris@0 70 * @throws OutOfBoundsException for invalid properties
Chris@0 71 */
Chris@0 72 public function __get($name)
Chris@0 73 {
Chris@0 74 if (! property_exists($this, $name)) {
Chris@0 75 throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server');
Chris@0 76 }
Chris@0 77 return $this->{$name};
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Set alternate response emitter to use.
Chris@0 82 *
Chris@0 83 * @param Response\EmitterInterface $emitter
Chris@0 84 */
Chris@0 85 public function setEmitter(Response\EmitterInterface $emitter)
Chris@0 86 {
Chris@0 87 $this->emitter = $emitter;
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Create a Server instance
Chris@0 92 *
Chris@0 93 * Creates a server instance from the callback and the following
Chris@0 94 * PHP environmental values:
Chris@0 95 *
Chris@0 96 * - server; typically this will be the $_SERVER superglobal
Chris@0 97 * - query; typically this will be the $_GET superglobal
Chris@0 98 * - body; typically this will be the $_POST superglobal
Chris@0 99 * - cookies; typically this will be the $_COOKIE superglobal
Chris@0 100 * - files; typically this will be the $_FILES superglobal
Chris@0 101 *
Chris@0 102 * @param callable $callback
Chris@0 103 * @param array $server
Chris@0 104 * @param array $query
Chris@0 105 * @param array $body
Chris@0 106 * @param array $cookies
Chris@0 107 * @param array $files
Chris@0 108 * @return static
Chris@0 109 */
Chris@0 110 public static function createServer(
Chris@0 111 callable $callback,
Chris@0 112 array $server,
Chris@0 113 array $query,
Chris@0 114 array $body,
Chris@0 115 array $cookies,
Chris@0 116 array $files
Chris@0 117 ) {
Chris@0 118 $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files);
Chris@0 119 $response = new Response();
Chris@0 120 return new static($callback, $request, $response);
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Create a Server instance from an existing request object
Chris@0 125 *
Chris@0 126 * Provided a callback, an existing request object, and optionally an
Chris@0 127 * existing response object, create and return the Server instance.
Chris@0 128 *
Chris@0 129 * If no Response object is provided, one will be created.
Chris@0 130 *
Chris@0 131 * @param callable $callback
Chris@0 132 * @param ServerRequestInterface $request
Chris@0 133 * @param null|ResponseInterface $response
Chris@0 134 * @return static
Chris@0 135 */
Chris@0 136 public static function createServerFromRequest(
Chris@0 137 callable $callback,
Chris@0 138 ServerRequestInterface $request,
Chris@0 139 ResponseInterface $response = null
Chris@0 140 ) {
Chris@0 141 if (! $response) {
Chris@0 142 $response = new Response();
Chris@0 143 }
Chris@0 144 return new static($callback, $request, $response);
Chris@0 145 }
Chris@0 146
Chris@0 147 /**
Chris@0 148 * "Listen" to an incoming request
Chris@0 149 *
Chris@0 150 * If provided a $finalHandler, that callable will be used for
Chris@0 151 * incomplete requests.
Chris@0 152 *
Chris@0 153 * Output buffering is enabled prior to invoking the attached
Chris@0 154 * callback; any output buffered will be sent prior to any
Chris@0 155 * response body content.
Chris@0 156 *
Chris@0 157 * @param null|callable $finalHandler
Chris@0 158 */
Chris@0 159 public function listen(callable $finalHandler = null)
Chris@0 160 {
Chris@0 161 $callback = $this->callback;
Chris@0 162
Chris@0 163 ob_start();
Chris@0 164 $bufferLevel = ob_get_level();
Chris@0 165
Chris@0 166 $response = $callback($this->request, $this->response, $finalHandler);
Chris@0 167 if (! $response instanceof ResponseInterface) {
Chris@0 168 $response = $this->response;
Chris@0 169 }
Chris@0 170 $this->getEmitter()->emit($response, $bufferLevel);
Chris@0 171 }
Chris@0 172
Chris@0 173 /**
Chris@0 174 * Retrieve the current response emitter.
Chris@0 175 *
Chris@0 176 * If none has been registered, lazy-loads a Response\SapiEmitter.
Chris@0 177 *
Chris@0 178 * @return Response\EmitterInterface
Chris@0 179 */
Chris@0 180 private function getEmitter()
Chris@0 181 {
Chris@0 182 if (! $this->emitter) {
Chris@0 183 $this->emitter = new Response\SapiEmitter();
Chris@0 184 }
Chris@0 185
Chris@0 186 return $this->emitter;
Chris@0 187 }
Chris@0 188 }