├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src └── functions.php └── tests └── SendTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | /build/ 3 | /vendor/ 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | dist: trusty 3 | sudo: false 4 | php: 5 | - '5.6' 6 | - '7.0' 7 | - '7.1' 8 | - '7.2' 9 | - '7.3' 10 | - nightly 11 | matrix: 12 | allow_failures: 13 | - php: nightly 14 | install: 15 | - composer install 16 | script: 17 | - ./vendor/bin/phpunit 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Woody Gilk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HTTP Response Sender 2 | 3 | A simple function that will send PSR-7 `ResponseInterface` headers and body. 4 | 5 | ## Installation 6 | 7 | ```php 8 | composer require http-interop/response-sender 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```php 14 | use function Http\Response\send; 15 | 16 | // Any ResponseInterface instance can be used. 17 | $notFound = $responseFactory->createResponse(404); 18 | 19 | // Send headers and body. 20 | send($notFound); 21 | ``` 22 | 23 | ## License 24 | 25 | MIT 26 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "http-interop/response-sender", 3 | "description": "A function to convert PSR-7 Response to HTTP output", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Woody Gilk", 9 | "email": "woody.gilk@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "require": { 14 | "php": ">=5.6", 15 | "psr/http-message": "^1.0" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": "^5.6", 19 | "guzzlehttp/psr7": "^1.4", 20 | "php-mock/php-mock-phpunit": "^1.1" 21 | }, 22 | "autoload": { 23 | "files": [ 24 | "src/functions.php" 25 | ] 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Http\\Response\\": "tests/" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ./tests 5 | 6 | 7 | 8 | 9 | ./src 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/functions.php: -------------------------------------------------------------------------------- 1 | getProtocolVersion(), 16 | $response->getStatusCode(), 17 | $response->getReasonPhrase() 18 | ); 19 | 20 | header($http_line, true, $response->getStatusCode()); 21 | 22 | foreach ($response->getHeaders() as $name => $values) { 23 | foreach ($values as $value) { 24 | header("$name: $value", false); 25 | } 26 | } 27 | 28 | $stream = $response->getBody(); 29 | 30 | if ($stream->isSeekable()) { 31 | $stream->rewind(); 32 | } 33 | 34 | while (!$stream->eof()) { 35 | echo $stream->read(1024 * 8); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /tests/SendTest.php: -------------------------------------------------------------------------------- 1 | 'text/plain', 19 | ], 20 | $body = uniqid(true), 21 | $version = '1.1' 22 | ); 23 | 24 | $sent_headers = []; 25 | $header = $this->getFunctionMock(__NAMESPACE__, 'header'); 26 | $header->expects($this->any())->will($this->returnCallback( 27 | function ($header, $replace) use (&$sent_headers) { 28 | return $sent_headers[] = $header; 29 | } 30 | )); 31 | 32 | $output = $this->captureSend($response); 33 | 34 | $this->assertSame([ 35 | "HTTP/$version $status OK", 36 | "Content-Type: text/plain", 37 | ], $sent_headers); 38 | 39 | $this->assertSame($body, $output); 40 | } 41 | 42 | private function mockHeaderCalls() 43 | { 44 | } 45 | 46 | private function captureSend(Response $response) 47 | { 48 | ob_start(); 49 | send($response); 50 | return ob_get_clean(); 51 | } 52 | } 53 | --------------------------------------------------------------------------------