-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robin de Graaf
committed
Mar 22, 2019
1 parent
ec7e916
commit 4c22dcb
Showing
25 changed files
with
2,061 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
composer.lock | ||
vendor | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: php | ||
php: | ||
- '7.1' | ||
- '7.2' | ||
- '7.3' | ||
install: composer install | ||
script: vendor/bin/phpunit --verbose tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Parable Http | ||
|
||
## 0.1.0 | ||
|
||
_Changes_ | ||
- First release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
dependencies: | ||
composer install \ | ||
--no-interaction \ | ||
--no-plugins \ | ||
--no-scripts | ||
|
||
tests: dependencies | ||
vendor/bin/phpunit --verbose tests | ||
|
||
coverage: dependencies | ||
rm -rf ./coverage | ||
vendor/bin/phpunit --coverage-html ./coverage tests | ||
|
||
tests-clean: | ||
vendor/bin/phpunit --verbose tests | ||
|
||
coverage-clean: | ||
rm -rf ./coverage | ||
vendor/bin/phpunit --coverage-html ./coverage tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Parable Http | ||
|
||
[![Build Status](https://travis-ci.org/parable-php/http.svg?branch=master)](https://travis-ci.org/parable-php/http) | ||
[![Latest Stable Version](https://poser.pugx.org/parable-php/http/v/stable)](https://packagist.org/packages/parable-php/http) | ||
[![Latest Unstable Version](https://poser.pugx.org/parable-php/http/v/unstable)](https://packagist.org/packages/parable-php/http) | ||
[![License](https://poser.pugx.org/parable-php/http/license)](https://packagist.org/packages/parable-php/http) | ||
|
||
Parable Http is a minimalist Http library used to receive requests and send responses. It is not a full implementation, offering just-enough functionality. | ||
|
||
## Install | ||
|
||
Php 7.1+ and [composer](https://getcomposer.org) are required. | ||
|
||
```bash | ||
$ composer require parable-php/http | ||
``` | ||
|
||
## Usage | ||
|
||
To create a `Request` object automatically from the server variables, use: | ||
|
||
```php | ||
$request = RequestFactory::createFromServer(); | ||
``` | ||
|
||
To create a `Request` from scratch, use: | ||
|
||
```php | ||
$request = new Request( | ||
'GET', | ||
'http://url.here/path?param=value' | ||
); | ||
``` | ||
|
||
To set up a minimal response you want to send to the client: | ||
|
||
```php | ||
$response = new Response(200, 'This is the body'); | ||
``` | ||
|
||
And to send it, use the `Dispatcher`: | ||
|
||
```php | ||
$response = new Response(200, 'This is the body'); | ||
$dispatcher = new Dispatcher(); | ||
|
||
$dispatcher->dispatch($response); | ||
``` | ||
|
||
This will send a response with stat | ||
us code `200`, with the body set as passed to the `Response` upon creation. | ||
|
||
## API | ||
|
||
#### Request | ||
- `getMethod(): string` - returns `GET`, `POST`, etc. | ||
- `getUri(): Uri` - return a `Uri` object representing the uri being requested. | ||
- `getRequestUri(): ?string` - the path of the `Uri` | ||
- `getProtocol(): string` - the protocol used, `HTTP/1.1` by default | ||
- `getProtocolVersion(): string` - just the `1.1` part of the protocol | ||
- `getBody(): ?string` - the body of the request, if any | ||
- `getUser(): ?string` - the username from the uri | ||
- `getPass(): ?string` - the password from the uri | ||
- `isHttps(): bool` - whether the request was made over https. This represents a 'best guess' based on multiple checks. | ||
- `isMethod(string $method): bool` - check whether the method matches `$method` | ||
|
||
#### Response | ||
The `Response` class is immutable, meaning it can't be altered after creation, but new copies with altered properties can be created using the `withSomething()` methods. | ||
|
||
- `getStatusCode(): int` - the status code to be sent (i.e. `200`) | ||
- `getStatusCodeText(): int` - the status code text to be sent (i.e. `OK`) | ||
- `getBody(): ?string` - the body to be sent | ||
- `getContentType(): string` - the content type (i.e. `text/html`, `application/json`) | ||
- `getProtocol(): string` - the protocol used (i.e. `HTTP/1.1`) | ||
- `getProtocolVersion(): string` - the protocol version (i.e. `1.1`) | ||
- `withStatusCode(int $value): self` - | ||
- `withBody(string $value): self` - | ||
- `withPrependedBody(string $value): self` - | ||
- `withAppendedBody(string $value): self` - | ||
- `withContentType(string $value): self` - | ||
- `withHeader(string $header, string $value): self` - | ||
- `withHeaders(array $headers): self` - | ||
- `withAddedHeaders(array $headers): self` - | ||
- `withProtocol(string $value): self` - | ||
|
||
## Contributing | ||
|
||
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at [devvoh.com](https://devvoh.com). | ||
|
||
## License | ||
|
||
All Parable components are open-source software, licensed under the MIT license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "parable-php/http", | ||
"type": "library", | ||
"description": "Parable HTTP is a straight-forward Request/Response library", | ||
"keywords": ["library", "micro", "http", "request", "response", "parable"], | ||
"homepage": "https://github.com/parable-php/http", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Robin de Graaf", | ||
"email": "[email protected]", | ||
"homepage": "https://devvoh.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.1", | ||
"ext-mbstring": "*", | ||
"ralouphie/getallheaders": "^3.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Parable\\Http\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Parable\\Http\\Tests\\": "tests" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
beStrictAboutTestsThatDoNotTestAnything="true" | ||
beStrictAboutOutputDuringTests="true" | ||
colors="true" | ||
cacheResult="false" | ||
> | ||
<php> | ||
<ini name="memory_limit" value="1024M" /> | ||
<ini name="display_errors" value="stdout" /> | ||
<ini name="error_log" value="/dev/null" /> | ||
</php> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Parable\Http; | ||
|
||
use Parable\Http\Traits\SupportsOutputBuffers; | ||
|
||
class Dispatcher | ||
{ | ||
use SupportsOutputBuffers; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $shouldTerminate = true; | ||
|
||
public function dispatch(Response $response): void | ||
{ | ||
if (HeaderSender::alreadySent()) { | ||
throw new Exception('Cannot dispatch response if headers already sent.'); | ||
} | ||
|
||
HeaderSender::send(sprintf( | ||
'%s %s %s', | ||
$response->getProtocol(), | ||
$response->getStatusCode(), | ||
$response->getStatusCodeText() ?? 'Unknown status code' | ||
)); | ||
|
||
foreach ($response->getHeaders() as $header => $value) { | ||
HeaderSender::send(sprintf( | ||
'%s: %s', | ||
$header, | ||
$value | ||
)); | ||
} | ||
|
||
$bufferedContent = $this->getAll(); | ||
|
||
echo $bufferedContent . $response->getBody(); | ||
|
||
if ($this->shouldTerminate) { | ||
$this->terminate(0); | ||
} | ||
} | ||
|
||
public function setShouldTerminate(bool $shouldTerminate): void | ||
{ | ||
$this->shouldTerminate = $shouldTerminate; | ||
} | ||
|
||
public function dispatchAndTerminate(Response $response, int $exitCode = 0): void | ||
{ | ||
$this->dispatch($response); | ||
$this->terminate($exitCode); | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
protected function terminate(int $exitCode): void | ||
{ | ||
exit($exitCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Parable\Http; | ||
|
||
class Exception extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Parable\Http; | ||
|
||
class HeaderSender | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected static $headers = []; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected static $testMode = false; | ||
|
||
public static function setTestMode(bool $testMode): void | ||
{ | ||
self::$testMode = $testMode; | ||
} | ||
|
||
public static function send(string $header): void | ||
{ | ||
if (self::$testMode) { | ||
self::$headers[] = $header; | ||
return; | ||
} | ||
|
||
header($header); | ||
} | ||
|
||
public static function alreadySent(): bool | ||
{ | ||
if (self::$testMode) { | ||
return false; | ||
} | ||
|
||
return headers_sent(); | ||
} | ||
|
||
public static function list(): array | ||
{ | ||
if (self::$testMode) { | ||
return self::$headers; | ||
} | ||
|
||
return headers_list(); | ||
} | ||
} |
Oops, something went wrong.