annotate vendor/zendframework/zend-diactoros/src/Server.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
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@12 4 * @copyright Copyright (c) 2015-2017 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@0 11 use Psr\Http\Message\ServerRequestInterface;
Chris@0 12 use Psr\Http\Message\ResponseInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * "Serve" incoming HTTP requests
Chris@0 16 *
Chris@0 17 * Given a callback, takes an incoming request, dispatches it to the
Chris@0 18 * callback, and then sends a response.
Chris@0 19 */
Chris@0 20 class Server
Chris@0 21 {
Chris@0 22 /**
Chris@0 23 * @var callable
Chris@0 24 */
Chris@0 25 private $callback;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Response emitter to use; by default, uses Response\SapiEmitter.
Chris@0 29 *
Chris@0 30 * @var Response\EmitterInterface
Chris@0 31 */
Chris@0 32 private $emitter;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * @var ServerRequestInterface
Chris@0 36 */
Chris@0 37 private $request;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * @var ResponseInterface
Chris@0 41 */
Chris@0 42 private $response;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Constructor
Chris@0 46 *
Chris@0 47 * Given a callback, a request, and a response, we can create a server.
Chris@0 48 *
Chris@0 49 * @param callable $callback
Chris@0 50 * @param ServerRequestInterface $request
Chris@0 51 * @param ResponseInterface $response
Chris@0 52 */
Chris@0 53 public function __construct(
Chris@0 54 callable $callback,
Chris@0 55 ServerRequestInterface $request,
Chris@0 56 ResponseInterface $response
Chris@0 57 ) {
Chris@0 58 $this->callback = $callback;
Chris@0 59 $this->request = $request;
Chris@0 60 $this->response = $response;
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Allow retrieving the request, response and callback as properties
Chris@0 65 *
Chris@0 66 * @param string $name
Chris@0 67 * @return mixed
Chris@0 68 * @throws OutOfBoundsException for invalid properties
Chris@0 69 */
Chris@0 70 public function __get($name)
Chris@0 71 {
Chris@0 72 if (! property_exists($this, $name)) {
Chris@0 73 throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server');
Chris@0 74 }
Chris@0 75 return $this->{$name};
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Set alternate response emitter to use.
Chris@0 80 *
Chris@0 81 * @param Response\EmitterInterface $emitter
Chris@0 82 */
Chris@0 83 public function setEmitter(Response\EmitterInterface $emitter)
Chris@0 84 {
Chris@0 85 $this->emitter = $emitter;
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Create a Server instance
Chris@0 90 *
Chris@0 91 * Creates a server instance from the callback and the following
Chris@0 92 * PHP environmental values:
Chris@0 93 *
Chris@0 94 * - server; typically this will be the $_SERVER superglobal
Chris@0 95 * - query; typically this will be the $_GET superglobal
Chris@0 96 * - body; typically this will be the $_POST superglobal
Chris@0 97 * - cookies; typically this will be the $_COOKIE superglobal
Chris@0 98 * - files; typically this will be the $_FILES superglobal
Chris@0 99 *
Chris@0 100 * @param callable $callback
Chris@0 101 * @param array $server
Chris@0 102 * @param array $query
Chris@0 103 * @param array $body
Chris@0 104 * @param array $cookies
Chris@0 105 * @param array $files
Chris@0 106 * @return static
Chris@0 107 */
Chris@0 108 public static function createServer(
Chris@0 109 callable $callback,
Chris@0 110 array $server,
Chris@0 111 array $query,
Chris@0 112 array $body,
Chris@0 113 array $cookies,
Chris@0 114 array $files
Chris@0 115 ) {
Chris@0 116 $request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files);
Chris@0 117 $response = new Response();
Chris@0 118 return new static($callback, $request, $response);
Chris@0 119 }
Chris@0 120
Chris@0 121 /**
Chris@0 122 * Create a Server instance from an existing request object
Chris@0 123 *
Chris@0 124 * Provided a callback, an existing request object, and optionally an
Chris@0 125 * existing response object, create and return the Server instance.
Chris@0 126 *
Chris@0 127 * If no Response object is provided, one will be created.
Chris@0 128 *
Chris@0 129 * @param callable $callback
Chris@0 130 * @param ServerRequestInterface $request
Chris@0 131 * @param null|ResponseInterface $response
Chris@0 132 * @return static
Chris@0 133 */
Chris@0 134 public static function createServerFromRequest(
Chris@0 135 callable $callback,
Chris@0 136 ServerRequestInterface $request,
Chris@0 137 ResponseInterface $response = null
Chris@0 138 ) {
Chris@0 139 if (! $response) {
Chris@0 140 $response = new Response();
Chris@0 141 }
Chris@0 142 return new static($callback, $request, $response);
Chris@0 143 }
Chris@0 144
Chris@0 145 /**
Chris@0 146 * "Listen" to an incoming request
Chris@0 147 *
Chris@0 148 * If provided a $finalHandler, that callable will be used for
Chris@0 149 * incomplete requests.
Chris@0 150 *
Chris@0 151 * @param null|callable $finalHandler
Chris@0 152 */
Chris@0 153 public function listen(callable $finalHandler = null)
Chris@0 154 {
Chris@0 155 $callback = $this->callback;
Chris@0 156
Chris@0 157 $response = $callback($this->request, $this->response, $finalHandler);
Chris@0 158 if (! $response instanceof ResponseInterface) {
Chris@0 159 $response = $this->response;
Chris@0 160 }
Chris@12 161
Chris@12 162 $this->getEmitter()->emit($response);
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Retrieve the current response emitter.
Chris@0 167 *
Chris@0 168 * If none has been registered, lazy-loads a Response\SapiEmitter.
Chris@0 169 *
Chris@0 170 * @return Response\EmitterInterface
Chris@0 171 */
Chris@0 172 private function getEmitter()
Chris@0 173 {
Chris@0 174 if (! $this->emitter) {
Chris@0 175 $this->emitter = new Response\SapiEmitter();
Chris@0 176 }
Chris@0 177
Chris@0 178 return $this->emitter;
Chris@0 179 }
Chris@0 180 }