Skip to content

Commit

Permalink
Lots of updates (#1)
Browse files Browse the repository at this point in the history
This adds a bunch of updates. A couple of the key additions are:
- request object
- response object
- data decoders
- status object
- exceptions
  • Loading branch information
sndsgd committed Jul 12, 2016
1 parent 3e1e7ce commit f14ea4f
Show file tree
Hide file tree
Showing 112 changed files with 6,035 additions and 582 deletions.
5 changes: 2 additions & 3 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
src_dir: src
coverage_clover: tests/coverage-clover.xml
json_path: tests/coverage-coveralls.json
coverage_clover: tests/coverage/clover.xml
json_path: tests/coverage/coveralls.json
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/.coveralls export-ignore
/.coveralls.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/apigen.neon export-ignore
/phpunit.xml export-ignore
/tests export-ignore
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
docs
vendor
composer.lock
bin/dev.php
tests/coverage-clover.xml
tests/coverage-coveralls.json
/build
/vendor
/composer.lock
15 changes: 2 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- hhvm-nightly

matrix:
allow_failures:
- php: 7.0
- php: hhvm
- php: hhvm-nightly

install:
- composer install --no-interaction --prefer-source --dev

script:
- phpunit --coverage-clover tests/coverage-clover.xml
- phpunit --coverage-clover tests/coverage/clover.xml

after_script:
- '[ "5.5" = "$(phpenv version-name)" ] && composer require satooshi/php-coveralls:dev-master && vendor/bin/coveralls -v'
- vendor/bin/coveralls -v
118 changes: 104 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,127 @@
# sndsgd-http
# sndsgd/http

[![Latest Version](https://img.shields.io/github/release/sndsgd/sndsgd-http.svg?style=flat-square)](https://github.com/sndsgd/sndsgd-http/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/sndsgd/sndsgd-http/LICENSE)
[![Build Status](https://img.shields.io/travis/sndsgd/sndsgd-http/master.svg?style=flat-square)](https://travis-ci.org/sndsgd/sndsgd-http)
[![Coverage Status](https://img.shields.io/coveralls/sndsgd/sndsgd-http.svg?style=flat-square)](https://coveralls.io/r/sndsgd/sndsgd-http?branch=master)
[![Latest Version](https://img.shields.io/github/release/sndsgd/http.svg?style=flat-square)](https://github.com/sndsgd/http/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/sndsgd/http/LICENSE)
[![Build Status](https://img.shields.io/travis/sndsgd/http/master.svg?style=flat-square)](https://travis-ci.org/sndsgd/http)
[![Coverage Status](https://img.shields.io/coveralls/sndsgd/http.svg?style=flat-square)](https://coveralls.io/r/sndsgd/http?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/sndsgd/http.svg?style=flat-square)](https://packagist.org/packages/sndsgd/http)

Improved HTTP for PHP.


## Requirements

You need **PHP >= 5.4.0** to use this library, however, the latest stable version of PHP is recommended.
This project is unstable and subject to changes from release to release.

You need **PHP >= 7.0** to use this library, however, the latest stable version of PHP is recommended.


## Install

Install `sndsgd/http` using [Composer](https://getcomposer.org/).
Install `sndsgd/rate` using [Composer](https://getcomposer.org/).


## Usage

This library aims to improve how PHP handles HTTP requests, mainly by streamlining the the process of working with request parameters.

### Improved Request Parameter Decoders

To experiment with the decoders, you can use PHP's built in webserver with the script located in `bin/request-demo.php`.

```sh
php -S localhost:8000 bin/request-demo.php
```
composer require sndsgd/http

Now you will be able to make requests to [http://localhost:8000](http://localhost:8000/) using any HTTP client. In the examples below, we'll be using [httpie](https://github.com/jkbrzt/httpie). To dump information about the request that is being made, simply append `-v` to any of the commands below.


#### Query String Decoder

In many query parameter implementations, you do not need to use brackets to indicate multiple values, but with PHP you do. For compatibility with the built in PHP decoder, you can continue to use brackets.

```sh
http http://localhost:8000/query a==1 a==2 a[]==💩
```

Result:

```json
"$_GET": {
"a": [
"💩"
]
},
"sndsgd": {
"a": [
"1",
"2",
"💩"
]
}
}
```

## Testing
#### Request Body Decoder

The built in body decoder for PHP only handles `multipart/form-data` and `application/x-www-form-urlencoded` content types for `POST` requests. This library acts as a polyfill to add `application/json` to that list, and to allow for decoding the request body for _any_ request method. By default, `POST` requests will be processed by the built in PHP decoder, however, you can disable that functionality by setting `enable_post_data_reading = Off` in your `php.ini`.


Use [PHPUnit](https://phpunit.de/) to run unit tests.
> _The built in decoder will not decode the body of this urlencoded `PATCH` request_
```
vendor/bin/phpunit
http --form PATCH http://localhost:8000/body a=1 b=2 c[d]=3a
```

Result:

## Documentation
```json
{
"$_POST": [],
"$_FILES": [],
"sndsgd": {
"a": "1",
"b": "2",
"c": {
"d": "3"
}
}
}
```

#### Uploaded Files

Use [ApiGen](http://apigen.org/) to create docs.
Instead of using `$_FILES`, with `sndsgd/http` uploaded files are included with the other parameters of the request body as objects.

```
apigen generate
http --form POST http://localhost:8000/body a=1 file@README.md
```

Result:

```json
{
"$_POST": {
"a": "1"
},
"$_FILES": {
"file": {
"name": "README.md",
"type": "",
"tmp_name": "/private/var/folders/2b/mtmy5wk56jx13vjgqpydc3nr0000gn/T/php9DTXiq",
"error": 0,
"size": 2766
}
},
"sndsgd": {
"a": "1",
"file": {
"filename": "README.md",
"contentType": "text/x-markdown",
"realContentType": "text/plain",
"size": 2766,
"tempfile": "/private/var/folders/2b/mtmy5wk56jx13vjgqpydc3nr0000gn/T/php9DTXiq"
}
}
}
```
5 changes: 0 additions & 5 deletions apigen.neon

This file was deleted.

30 changes: 30 additions & 0 deletions bin/request-demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

require __DIR__."/../vendor/autoload.php";

$request = new \sndsgd\http\Request($_SERVER);
switch ($request->getPath()) {
case "/query":
$data = [
'$_GET' => $_GET,
'sndsgd' => $request->getQueryParameters(),
];
break;
case "/body":
$data = [
'$_POST' => $_POST,
'$_FILES' => $_FILES,
'sndsgd' => $request->getBodyParameters(),
];
break;
default:
$data = [
"path" => $request->getPath(),
"method" => $request->getMethod(),
"headers" => $request->getHeaders(),
"query" => $request->getQueryParameters(),
"body" => $request->getBodyParameters(),
];
}

echo json_encode($data, \sndsgd\Json::HUMAN);
36 changes: 36 additions & 0 deletions bin/request-form-demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use \sndsgd\form\field;
use \sndsgd\form\rule;

require __DIR__."/../vendor/autoload.php";

$form = (new \sndsgd\Form())
->addFields(
(new field\StringField("caption"))
->addRules(
new rule\RequiredRule(),
new rule\MaxLengthRule(255)
),
(new field\UploadedFileField("image"))
->addRules(
new rule\RequiredRule(),
new rule\UploadedFileTypeRule("image/jpeg", "image/png")
)
);

$request = new \sndsgd\http\Request($_SERVER);
$validator = new \sndsgd\form\Validator($form);
try {
$parameters = $validator->validate($request->getBodyParameters());
$message = "Success";
} catch (\sndsgd\form\ValidationException $ex) {
$message = "Validation Error";
$errors = $ex->getErrors();
}

echo json_encode([
"message" => $message,
"parameters" => $parameters ?? null,
"errors" => $errors ?? [],
], \sndsgd\Json::HUMAN);
61 changes: 33 additions & 28 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
{
"name": "sndsgd/http",
"description": "A collection of tools for working with HTTP",
"type": "library",
"homepage": "https://github.com/sndsgd/sndsgd-http",
"license": "MIT",
"authors": [
{
"name": "Russell B",
"homepage": "http://snds.gd/"
}
],
"support": {
"issues": "https://github.com/sndsgd/sndsgd-http/issues",
"source": "https://github.com/sndsgd/sndsgd-http/tree/master"
},
"require": {
"php": ">=5.4.0",
"sndsgd/util": "~0.5"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"sndsgd\\http\\": "src/"
}
}
}
"name": "sndsgd/http",
"description": "Improved HTTP for PHP",
"type": "library",
"homepage": "https://github.com/sndsgd/http",
"license": "MIT",
"authors": [
{
"name": "Russell B",
"homepage": "http://snds.gd/"
}
],
"support": {
"issues": "https://github.com/sndsgd/http/issues",
"source": "https://github.com/sndsgd/http/tree/master"
},
"minimum-stability": "dev",
"require": {
"php": ">=7.0.0",
"sndsgd/util": "~1.0.5",
"sndsgd/error": "~0.0.3",
"sndsgd/form": "~0.1.0",
"sndsgd/fs": "~0.2.4"
},
"require-dev": {
"mikey179/vfsStream": "1.6.2",
"satooshi/php-coveralls": "~1.0"
},
"autoload": {
"psr-4": {
"sndsgd\\": "src"
}
}
}
50 changes: 27 additions & 23 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
>
<testsuites>
<testsuite name="sndsgd/http tests">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="sndsgd/http tests">
<directory suffix=".php">./tests/unit</directory>
</testsuite>
</testsuites>

<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
</logging>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
<log type="coverage-html" target="build/coverage" showUncoveredFiles="true"/>
</logging>

<filter>
<blacklist>
<directory>./vendor</directory>
</blacklist>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
<blacklist>
<directory>./vendor</directory>
</blacklist>
</filter>
</phpunit>
</phpunit>

0 comments on commit f14ea4f

Please sign in to comment.