Skip to content

Commit 663e77f

Browse files
committed
Initial commit.
0 parents  commit 663e77f

15 files changed

+558
-0
lines changed

.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
2+
3+
/tests export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/.php_cs export-ignore
8+
/.travis.yml export-ignore
9+
/.scrutinizer.yml export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
phpunit.xml
3+
composer.lock
4+
.php_cs.cache

.php_cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__)
5+
->exclude('vendor')
6+
;
7+
8+
return PhpCsFixer\Config::create()
9+
->setRules([
10+
'@PSR2' => true,
11+
])
12+
->setFinder($finder)
13+
;

.scrutinizer.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
filter:
2+
paths: ["src/*"]
3+
tools:
4+
php_sim: true
5+
php_pdepend: true
6+
php_analyzer: true
7+
external_code_coverage: true

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
sudo: false
2+
3+
language: php
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache
8+
- vendor
9+
10+
matrix:
11+
include:
12+
- php: 7
13+
env:
14+
- EXECUTE_CS_CHECK=true
15+
- EXECUTE_TEST_COVERALLS=true
16+
- php: 7.1
17+
- php: hhvm
18+
allow_failures:
19+
- php: hhvm
20+
21+
install:
22+
- travis_retry composer install --no-interaction
23+
24+
script:
25+
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --disallow-test-output --coverage-clover ./clover.xml ; else ./vendor/bin/phpunit --disallow-test-output ; fi
26+
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run ; fi
27+
28+
after_script:
29+
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover ./clover.xml ; fi

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing
2+
3+
Contributions are accepted via Pull Requests on [Github](https://github.com/wecodein/http-middleware).
4+
5+
## Running tests
6+
7+
``` bash
8+
$ composer test
9+
```
10+
11+
## Fixing coding standards issues
12+
13+
``` bash
14+
$ composer cs-fix
15+
```

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2017 Dusan Vejin
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# http-middleware
2+
3+
[![Build Status][ico-build]][link-build]
4+
[![Code Quality][ico-code-quality]][link-code-quality]
5+
[![Code Coverage][ico-code-coverage]][link-code-coverage]
6+
[![Latest Version][ico-version]][link-packagist]
7+
[![PDS Skeleton][ico-pds]][link-pds]
8+
9+
10+
## Installation
11+
12+
The preferred method of installation is via [Composer](http://getcomposer.org/). Run the following command to install the latest version of a package and add it to your project's `composer.json`:
13+
14+
```bash
15+
composer require wecodein/http-middleware
16+
```
17+
18+
## Usage
19+
20+
``` php
21+
use Http\Factory\Guzzle\ResponseFactory;
22+
use Http\Factory\Guzzle\ServerRequestFactory;
23+
use Interop\Http\ServerMiddleware\DelegateInterface;
24+
use Psr\Http\Message\ServerRequestInterface;
25+
use WeCodeIn\Http\ServerMiddleware\Delegate;
26+
use WeCodeIn\Http\ServerMiddleware\Middleware\CallableMiddleware;
27+
28+
require_once './vendor/autoload.php';
29+
30+
$serverRequestFactory = new ServerRequestFactory();
31+
$responseFactory = new ResponseFactory();
32+
$serverRequest = $serverRequestFactory->createServerRequest('GET', 'http://localhost');
33+
34+
$noContentMiddleware = function (ServerRequestInterface $request, DelegateInterface $delegate) {
35+
$response = $delegate->process($request);
36+
37+
if ($response->getBody()->getSize() === 0) {
38+
return $response->withStatus(204, 'No Content');
39+
}
40+
41+
return $response;
42+
};
43+
44+
$delegate = new Delegate($responseFactory);
45+
$delegate->insert(new CallableMiddleware($noContentMiddleware));
46+
$response = $delegate($serverRequest);
47+
48+
assert($response->getStatusCode() === 204, 'Assert that status code is 204');
49+
assert($response->getReasonPhrase() === 'No Content', 'Assert that reason phrase is `No Content`');
50+
51+
```
52+
53+
## Credits
54+
55+
- [Dusan Vejin][link-author]
56+
- [All Contributors][link-contributors]
57+
58+
## License
59+
60+
Released under MIT License - see the [License File](LICENSE) for details.
61+
62+
63+
[ico-version]: https://img.shields.io/packagist/v/wecodein/http-middleware.svg
64+
[ico-build]: https://travis-ci.org/wecodein/http-middleware.svg?branch=master
65+
[ico-code-coverage]: https://img.shields.io/scrutinizer/coverage/g/wecodein/http-middleware.svg
66+
[ico-code-quality]: https://img.shields.io/scrutinizer/g/wecodein/http-middleware.svg
67+
[ico-pds]: https://img.shields.io/badge/pds-skeleton-blue.svg
68+
69+
[link-packagist]: https://packagist.org/packages/wecodein/http-middleware
70+
[link-build]: https://travis-ci.org/wecodein/http-middleware
71+
[link-code-coverage]: https://scrutinizer-ci.com/g/wecodein/http-middleware/code-structure
72+
[link-code-quality]: https://scrutinizer-ci.com/g/wecodein/http-middleware
73+
[link-pds]: https://github.com/php-pds/skeleton
74+
[link-author]: https://github.com/dutekvejin
75+
[link-contributors]: ../../contributors

composer.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "wecodein/http-middleware",
3+
"description": "Middleware dispatching system",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"wecodein",
8+
"http-middleware",
9+
"psr-15"
10+
],
11+
"authors": [
12+
{
13+
"name": "Dusan Vejin",
14+
"email": "[email protected]",
15+
"homepage": "https://github.com/dutekvejin"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.0",
20+
"http-interop/http-middleware": "^0.4.1",
21+
"http-interop/http-factory": "^0.3.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^6.2",
25+
"friendsofphp/php-cs-fixer": "^2.0"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"WeCodeIn\\Http\\ServerMiddleware\\": "src/"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"WeCodeIn\\Http\\ServerMiddleware\\Tests\\": "tests/"
35+
}
36+
},
37+
"scripts": {
38+
"test": "phpunit --colors=always",
39+
"cs-fix": "php-cs-fixer fix --config=.php_cs",
40+
"cs-check": "php-cs-fixer fix --config=.php_cs -v --diff --dry-run"
41+
},
42+
"extra": {
43+
"branch-alias": {
44+
"dev-master": "1.0-dev"
45+
}
46+
}
47+
}

phpunit.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<phpunit
2+
bootstrap="./vendor/autoload.php"
3+
colors="true"
4+
>
5+
<testsuites>
6+
<testsuite name="http-middleware tests">
7+
<directory>./tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
<filter>
11+
<whitelist addUncoveredFilesFromWhitelist="true">
12+
<directory suffix=".php">./src</directory>
13+
</whitelist>
14+
</filter>
15+
</phpunit>

src/Delegate.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* This file is part of the http-middleware package.
4+
*
5+
* Copyright (c) wecodein
6+
*
7+
* For full copyright and license information, please refer to the LICENSE file,
8+
* located at the package root folder.
9+
*/
10+
11+
namespace WeCodeIn\Http\ServerMiddleware;
12+
13+
use Interop\Http\Factory\ResponseFactoryInterface;
14+
use Interop\Http\ServerMiddleware\DelegateInterface;
15+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
16+
use Psr\Http\Message\ResponseInterface;
17+
use Psr\Http\Message\ServerRequestInterface;
18+
19+
/**
20+
* Class Delegate
21+
* @package WeCodeIn\Http\ServerMiddleware
22+
*/
23+
class Delegate implements DelegateInterface
24+
{
25+
26+
/**
27+
* @var \SplPriorityQueue
28+
*/
29+
protected $queue;
30+
31+
/**
32+
* @var int
33+
*/
34+
protected $serial = PHP_INT_MAX;
35+
36+
/**
37+
* @var ResponseFactoryInterface
38+
*/
39+
protected $responseFactory;
40+
41+
/**
42+
* @param ResponseFactoryInterface $responseFactory
43+
*/
44+
public function __construct(ResponseFactoryInterface $responseFactory)
45+
{
46+
$this->queue = new \SplPriorityQueue();
47+
$this->responseFactory = $responseFactory;
48+
}
49+
50+
/**
51+
* @param MiddlewareInterface $middleware
52+
* @param int $priority
53+
* @return Delegate
54+
*/
55+
public function insert(MiddlewareInterface $middleware, int $priority = 0) : Delegate
56+
{
57+
$this->queue->insert($middleware, [$priority, $this->serial--]);
58+
return $this;
59+
}
60+
61+
/**
62+
* @return MiddlewareInterface
63+
*/
64+
public function extract() : MiddlewareInterface
65+
{
66+
return $this->queue->extract();
67+
}
68+
69+
/**
70+
* @return bool
71+
*/
72+
public function isEmpty() : bool
73+
{
74+
return $this->queue->isEmpty();
75+
}
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
public function process(ServerRequestInterface $request) : ResponseInterface
81+
{
82+
if ($this->isEmpty()) {
83+
return $this->responseFactory->createResponse();
84+
}
85+
86+
$middleware = $this->extract();
87+
$response = $middleware->process($request, $this);
88+
89+
if (!$response instanceof ResponseInterface) {
90+
throw new \OutOfBoundsException(sprintf(
91+
'Middleware %s did not return a %s', get_class($middleware), ResponseInterface::class
92+
));
93+
}
94+
95+
return $response;
96+
}
97+
98+
/**
99+
* @param ServerRequestInterface $request
100+
* @return ResponseInterface
101+
*/
102+
public function __invoke(ServerRequestInterface $request) : ResponseInterface
103+
{
104+
$delegate = clone $this;
105+
return $delegate->process($request);
106+
}
107+
108+
/**
109+
* Clone queue to allow multiple processing of same queue.
110+
*/
111+
public function __clone()
112+
{
113+
$this->queue = clone $this->queue;
114+
}
115+
}

0 commit comments

Comments
 (0)