annotate vendor/zendframework/zend-diactoros/src/Server.php @ 19:fa3358dc1485 tip

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