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