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