Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Oct 5, 2017
0 parents commit 0d22392
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/test export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
/vendor/
38 changes: 38 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
sudo: false

language: php

cache:
directories:
- $HOME/.composer/cache
- vendor

env:
- HTTP_MIDDLEWARE_VERSION="^0.1.1"
- HTTP_MIDDLEWARE_VERSION="^0.2"
- HTTP_MIDDLEWARE_VERSION="^0.3"
- HTTP_MIDDLEWARE_VERSION="^0.4.1"
- HTTP_MIDDLEWARE_VERSION="^0.5"
- HTTP_MIDDLEWARE_VERSION="dev-master"

php:
- 5.6
- 7.0
- 7.1
- 7.2

before_install:
- phpenv config-rm xdebug.ini || return 0
- travis_retry composer self-update

install:
- travis_retry composer require --no-interaction http-interop/http-middleware:$HTTP_MIDDLEWARE_VERSION
- travis_retry composer install --no-interaction
- if [[ $TRAVIS_PHP_VERSION =~ ^5.6 ]]; then travis_retry composer update --no-interaction --with-dependencies phpunit/phpunit ; fi
- stty cols 120 && composer show

script:
- composer test

notifications:
email: false
43 changes: 43 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Contributor Code of Conduct

This project adheres to [The Code Manifesto](http://codemanifesto.com)
as its guidelines for contributor interactions.

## The Code Manifesto

We want to work in an ecosystem that empowers developers to reach their
potential — one that encourages growth and effective collaboration. A space that
is safe for all.

A space such as this benefits everyone that participates in it. It encourages
new developers to enter our field. It is through discussion and collaboration
that we grow, and through growth that we improve.

In the effort to create such a place, we hold to these values:

1. **Discrimination limits us.** This includes discrimination on the basis of
race, gender, sexual orientation, gender identity, age, nationality, technology
and any other arbitrary exclusion of a group of people.
2. **Boundaries honor us.** Your comfort levels are not everyone’s comfort
levels. Remember that, and if brought to your attention, heed it.
3. **We are our biggest assets.** None of us were born masters of our trade.
Each of us has been helped along the way. Return that favor, when and where
you can.
4. **We are resources for the future.** As an extension of #3, share what you
know. Make yourself a resource to help those that come after you.
5. **Respect defines us.** Treat others as you wish to be treated. Make your
discussions, criticisms and debates from a position of respectfulness. Ask
yourself, is it true? Is it necessary? Is it constructive? Anything less is
unacceptable.
6. **Reactions require grace.** Angry responses are valid, but abusive language
and vindictive actions are toxic. When something happens that offends you,
handle it assertively, but be respectful. Escalate reasonably, and try to
allow the offender an opportunity to explain themselves, and possibly correct
the issue.
7. **Opinions are just that: opinions.** Each and every one of us, due to our
background and upbringing, have varying opinions. The fact of the matter, is
that is perfectly acceptable. Remember this: if you respect your own
opinions, you should respect the opinions of others.
8. **To err is human.** You might not intend it, but mistakes do happen and
contribute to build experience. Tolerate honest mistakes, and don't hesitate
to apologize if you make one yourself.
23 changes: 23 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (c) 2017, Michał Bundyra
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# http-middleware-compatibility

[![Build Status](https://travis-ci.org/webimpress/http-middleware-compatibility.svg?branch=master)](https://travis-ci.org/webimpress/http-middleware-compatibility)

## Purpose

The purpose of the library is to deliver consistent interface for different
versions of [`http-interop/http-middleware`](https://github.com/http-interop/http-middleware)
which implements [Draft of PSR-15 HTTP Middleware](https://github.com/php-fig/fig-standards/tree/master/proposed/http-middleware).

Many projects currently use different version of library
`http-interop/http-middleware` and updating to newest version requires usually
major release. The library lets consumers of your component decide what version
of `http-interop/http-middleware` they want to use and allow them to migrate to
the latest version at any time.

## Usage

Your middleware should implement interface `Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface`,
and for delegator/request handler you should use interface
`Webimpress\HttpMiddlewareCompatibility\HandlerInterface`.

```php
<?php

namespace MyNamespace;

use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;

class MyMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
// ...
}
}
```

Both interfaces are just aliases. It allows your middleware to work with
currently installed version of `http-interop/http-middleware` library.

### Handler method in 0.5.0

In release 0.5.0 of http-middleware handler method has been changed from
`process` to `handle`. We want support both, so we deliver helper method
`Webimpress\HttpMiddlewareCompatibility\Handler::getMethodName()`. Here is an
example how you can use it in your middleware:

```php
<?php

namespace MyNamespace;

use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

class MyMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
return $handler->{HANDLER_METHOD}($request);
}
}
```

That's it! Now consumers of your component can decide what version of
`http-interop/http-middleware` they want to use.
54 changes: 54 additions & 0 deletions autoload/http-middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Webimpress\HttpMiddlewareCompatibility;

// http-interop/http-middleware 0.3.0, 0.2.0, 0.1.1
if (interface_exists(\Interop\Http\Middleware\ServerMiddlewareInterface::class)
&& interface_exists(\Interop\Http\Middleware\DelegateInterface::class)
) {
class_alias(
\Interop\Http\Middleware\ServerMiddlewareInterface::class,
MiddlewareInterface::class
);

class_alias(
\Interop\Http\Middleware\DelegateInterface::class,
HandlerInterface::class
);

define(__NAMESPACE__ . '\HANDLER_METHOD', 'process');
}

// http-interop/http-middleware 0.4.1
if (interface_exists(\Interop\Http\ServerMiddleware\MiddlewareInterface::class)
&& interface_exists(\Interop\Http\ServerMiddleware\DelegateInterface::class)
) {
class_alias(
\Interop\Http\ServerMiddleware\MiddlewareInterface::class,
MiddlewareInterface::class
);

class_alias(
\Interop\Http\ServerMiddleware\DelegateInterface::class,
HandlerInterface::class
);

define(__NAMESPACE__ . '\HANDLER_METHOD', 'process');
}

// http-interop/http-middleware 0.5.0
if (interface_exists(\Interop\Http\Server\MiddlewareInterface::class)
&& interface_exists(\Interop\Http\Server\RequestHandlerInterface::class)
) {
class_alias(
\Interop\Http\Server\MiddlewareInterface::class,
MiddlewareInterface::class
);

class_alias(
\Interop\Http\Server\RequestHandlerInterface::class,
HandlerInterface::class
);

define(__NAMESPACE__ . '\HANDLER_METHOD', 'handle');
}
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "webimpress/http-middleware-compatibility",
"description": "Compatibility library for Draft PSR-15 HTTP Middleware",
"type": "library",
"homepage": "https://github.com/webimpress/http-middleware-compatibility",
"license": "BSD-2-Clause",
"keywords": [
"webimpress",
"psr-15",
"middleware"
],
"support": {
"issues": "https://github.com/webimpress/http-middleware-compatibility/issues",
"source": "https://github.com/webimpress/http-middleware-compatibility"
},
"config": {
"sort-packages": true
},
"require": {
"php": "^5.6 || ^7.0",
"http-interop/http-middleware": "^0.1.1 || ^0.2 || ^0.3 || ^0.4.1 || ^0.5"
},
"require-dev": {
"phpunit/phpunit": "^5.7.22 || ^6.3.1"
},
"autoload": {
"files": [
"autoload/http-middleware.php"
]
},
"autoload-dev": {
"psr-4": {
"WebimpressTest\\HttpMiddlewareCompatibility\\": "test/"
}
},
"scripts": {
"test": "phpunit --colors=always"
}
}
11 changes: 11 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="http-middleware-compatibility">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
48 changes: 48 additions & 0 deletions test/HandlerInterfaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace WebimpressTest\HttpMiddlewareCompatibility;

use PHPUnit\Framework\TestCase;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

class HandlerInterfaceTest extends TestCase
{
public function testHandlerInterfaceIsDefined()
{
self::assertTrue(interface_exists(HandlerInterface::class));
}

public function testHandlerInterfaceIsAliasToBaseLibrary()
{
// 0.3.0
if (interface_exists(\Interop\Http\Middleware\DelegateInterface::class)) {
self::assertTrue(is_a(HandlerInterface::class, \Interop\Http\Middleware\DelegateInterface::class, true));
self::assertTrue(is_a(\Interop\Http\Middleware\DelegateInterface::class, HandlerInterface::class, true));
self::assertSame('process', HANDLER_METHOD);

return;
}

// 0.4.1
if (interface_exists(\Interop\Http\ServerMiddleware\DelegateInterface::class)) {
self::assertTrue(is_a(HandlerInterface::class, \Interop\Http\ServerMiddleware\DelegateInterface::class, true));
self::assertTrue(is_a(\Interop\Http\ServerMiddleware\DelegateInterface::class, HandlerInterface::class, true));
self::assertSame('process', HANDLER_METHOD);

return;
}

// 0.5.0
if (interface_exists(\Interop\Http\Server\RequestHandlerInterface::class)) {
self::assertTrue(is_a(HandlerInterface::class, \Interop\Http\Server\RequestHandlerInterface::class, true));
self::assertTrue(is_a(\Interop\Http\Server\RequestHandlerInterface::class, HandlerInterface::class, true));
self::assertSame('handle', HANDLER_METHOD);

return;
}

self::fail('Unsupported version');
}
}
43 changes: 43 additions & 0 deletions test/MiddlewareInterfaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace WebimpressTest\HttpMiddlewareCompatibility;

use PHPUnit\Framework\TestCase;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;

class MiddlewareInterfaceTest extends TestCase
{
public function testMiddlewareInterfaceIsDefined()
{
self::assertTrue(interface_exists(MiddlewareInterface::class));
}

public function testMiddlewareInterfaceIsAliasToBaseLibrary()
{
// 0.3.0
if (interface_exists(\Interop\Http\Middleware\ServerMiddlewareInterface::class)) {
self::assertTrue(is_a(MiddlewareInterface::class, \Interop\Http\Middleware\ServerMiddlewareInterface::class, true));
self::assertTrue(is_a(\Interop\Http\Middleware\ServerMiddlewareInterface::class, MiddlewareInterface::class, true));

return;
}

// 0.4.1
if (interface_exists(\Interop\Http\ServerMiddleware\MiddlewareInterface::class)) {
self::assertTrue(is_a(MiddlewareInterface::class, \Interop\Http\ServerMiddleware\MiddlewareInterface::class, true));
self::assertTrue(is_a(\Interop\Http\ServerMiddleware\MiddlewareInterface::class, MiddlewareInterface::class, true));

return;
}

// 0.5.0
if (interface_exists(\Interop\Http\Server\MiddlewareInterface::class)) {
self::assertTrue(is_a(MiddlewareInterface::class, \Interop\Http\Server\MiddlewareInterface::class, true));
self::assertTrue(is_a(\Interop\Http\Server\MiddlewareInterface::class, MiddlewareInterface::class, true));

return;
}

self::fail('Unsupported version');
}
}

0 comments on commit 0d22392

Please sign in to comment.