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