Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ResponseEmitter | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\http; |
| 23 | |
| 24 | use Psr\Http\Message\ResponseInterface; |
| 25 | |
| 26 | /** |
| 27 | * Class ResponseEmitter |
| 28 | * @package oat\oatbox\http |
| 29 | * @author Moyon Camille <camille@taotesting.com> |
| 30 | */ |
| 31 | class ResponseEmitter |
| 32 | { |
| 33 | /** |
| 34 | * Emit a Http Response to client |
| 35 | * |
| 36 | * @see https://github.com/http-interop/response-sender |
| 37 | * @param ResponseInterface $response |
| 38 | */ |
| 39 | public function __invoke(ResponseInterface $response) |
| 40 | { |
| 41 | $http_line = sprintf( |
| 42 | 'HTTP/%s %s %s', |
| 43 | $response->getProtocolVersion(), |
| 44 | $response->getStatusCode(), |
| 45 | $response->getReasonPhrase() |
| 46 | ); |
| 47 | |
| 48 | if (!headers_sent()) { |
| 49 | header($http_line, true, $response->getStatusCode()); |
| 50 | foreach ($response->getHeaders() as $name => $values) { |
| 51 | foreach ($values as $value) { |
| 52 | header("$name: $value"); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | $stream = $response->getBody(); |
| 58 | |
| 59 | if ($stream->isSeekable()) { |
| 60 | $stream->rewind(); |
| 61 | } |
| 62 | while (!$stream->eof()) { |
| 63 | echo $stream->read(1024 * 8); |
| 64 | } |
| 65 | } |
| 66 | } |