Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 10, 2016
1 parent 17e1768 commit 1e3cdb9
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
47 changes: 47 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Contributing Guide

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. 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.

## How to contribute

This is a collaborative effort. We welcome all contributions submitted as pull requests.

(Contributions on wording & style are also welcome.)

### Bugs

A bug is a demonstrable problem that is caused by the code in the repository. Good bug reports are extremely helpful – thank you!

Please try to be as detailed as possible in your report. Include specific information about the environment – version of PHP, etc, and steps required to reproduce the issue.

### Pull Requests

Good pull requests – patches, improvements, new features – are a fantastic help. Before create a pull request, please follow these instructions:

* The code must follow the [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). Run `composer cs-fix` to fix your code before commit.
* Write tests
* Document any change in `README.md` and `CHANGELOG.md`
* One pull request per feature. If you want to do more than one thing, send multiple pull request

### Runing tests

```sh
composer test
```
186 changes: 185 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,186 @@
# psr15-middlewares
Collection of PSR-15 middlewares

Collection of [PSR-15](https://github.com/http-interop/http-middleware) middlewares

This is a migration of most [psr7-middlewares](https://github.com/oscarotero/psr7-middlewares) to follow PSR-15 specification (currently released as `0.2.0` of `php-interop/http-middleware`).

## Requirements

* PHP >= 5.6
* A [PSR-7](https://packagist.org/providers/psr/http-message-implementation) http mesage implementation ([Diactoros](https://github.com/zendframework/zend-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), etc...)
* A [PSR-15](https://github.com/http-interop/http-middleware) middleware dispatcher ([Middleman](https://github.com/mindplay-dk/middleman), etc...)

## Main differences with [psr7-middlewares](https://github.com/oscarotero/psr7-middlewares)

### PSR-15 / PSR-17 compliant

`PSR-15` defines a set of interfaces for interoperability with `PSR-7` middleware adding the following changes:

* The double-pass signature `function ($request, $response, $next)` is replaced by the new lambda-style: `function ($request, $next)`.
* There's two different interfaces: `MiddlewareInterface` and `ServerMiddlewareInterface` in order to differentiate between middlewares requiring a `RequestInterface` or a `ServerRequestInterface`.

### Split the middlewares into separate packages

This is something [requested by many people](https://github.com/oscarotero/psr7-middlewares/issues/23) so with the PSR-15 comming up, this is a good opportunity to port these components in individual packages.

### Removed the vendor namespace from the request attribute

In the old version, some components insert automatically values in the request attributes in a way that is hard to recover. For example, in [ClientIp](https://github.com/oscarotero/psr7-middlewares#clientip) to get the user ip you need to use a static function:

```php
use PsrMiddlewares\Middleware\ClientIp;

$ip = ClientIp::getIp($request);
```

Because the "hard" way is:

```php
$data = $request->getAttribute('Psr7Middlewares\\Middleware');
$ip = isset($data['CLIENT_IPS'][0]) ? $data['CLIENT_IPS'][0] : null;
```

I decided to remove this and use **configurable** attribute names:

```php
$ip = $request->getAttribute('client-ip'); //easy!!
```

### Split middleware components in multiple subcomponents

In the old version, some components uses "transformer resolvers" to do different things deppending of some circunstances (content-type, encoding, etc). [Payload](https://github.com/oscarotero/psr7-middlewares#payload) is an example of this: it can parse json, urlencoded or csv in the same class. Now this components have been splitted into subcomponents, so now there's a `JsonPayload`, `UrlEncodedPayload` and `CsvPayload`. Easier to maintain and to extends.

### More open

The middlewares has been moved to this organization. This is not the oscarotero's middlewares, this is simply "middlewares" and the community is welcome to participate.

## Usage example

```php
use Zend\Diactoros\ServerRequestFactory;
use mindplay\middleman\Dispatcher;
use Middlewares;

$dispatcher = new Dispatcher([

//Handle errors
(new Middlewares\ErrorHandler())
->catchExceptions(true),

//Log the request
new Middlewares\AccessLog($app->get('logger')),

//Calculate the response time
new Middlewares\ResponseTime(),

//Removes the trailing slash
new Middlewares\TrailingSlash(false),

//Insert the UUID
new Middlewares\Uuid(),

//Disable the search engine robots
new Middlewares\Robots(false),

//Minify the html
new Middlewares\HtmlMinifier(),

//Override the method using X-Http-Method-Override header
new Middlewares\MethodOverride(),

//Parse the json payload
new Middlewares\JsonPayload(),

//Parse the urlencoded payload
new Middlewares\UrlEncodedPayload(),

//Saves the client ip in the 'ip' attribute
(new Middlewares\ClientIp())
->attribute('ip'),

//Allow only some ips
new Middlewares\Firewall(['127.0.0.*']),

//Add cache expiration
new Middlewares\Expires(),

//Negotiate the content-type
new Middlewares\ContentType(),

//Negotiate the language
new Middlewares\ContentLanguage(['gl', 'es', 'en']),

//Create and save a session in 'session' attribute
(new Middlewares\AuraSession())
->attribute('session'),

//Adds the php debugbar
new Middlewares\Debugbar(),

//Handle the routes with fast-route
new Middlewares\FastRoute($app->get('dispatcher'))
]);

$response = $dispatcher->dispatch(ServerRequestFactory::fromGlobals());
```

## List of all available components

* [AccessLog](https://github.com/middlewares/access-log)
* [AuraRouter](https://github.com/middlewares/aura-router)
* [AuraSession](https://github.com/middlewares/aura-session)
* [BasePath](https://github.com/middlewares/base-path)
* [Cache](https://github.com/middlewares/cache)
* [Cache](https://github.com/middlewares/cache#cache)
* [CachePrevention](https://github.com/middlewares/cache#cacheprevention)
* [Expires](https://github.com/middlewares/cache#expires)
* [ClientIp](https://github.com/middlewares/client-ip)
* [Cors](https://github.com/middlewares/cors)
* [Csp](https://github.com/middlewares/csp)
* [Debugbar](https://github.com/middlewares/debugbar)
* [ErrorHandler](https://github.com/middlewares/error-handler)
* [FastRoute](https://github.com/middlewares/fast-route)
* [Filesystem](https://github.com/middlewares/filesystem)
* [Reader](https://github.com/middlewares/filesystem#reader)
* [Writer](https://github.com/middlewares/filesystem#writer)
* [Firewall](https://github.com/middlewares/firewall)
* [Geolocation](https://github.com/middlewares/geolocation)
* [Honeypot](https://github.com/middlewares/honeypot)
* [ImageManipulation](https://github.com/middlewares/image-manipulation)
* [HttpAuthentication](https://github.com/middlewares/http-authentication)
* [BasicAuthentication](https://github.com/middlewares/http-authentication#basicauthentication)
* [DigestAuthentication](https://github.com/middlewares/http-authentication#digestauthentication)
* [Https](https://github.com/middlewares/https)
* [MethodOverride](https://github.com/middlewares/method-override)
* [Minifier](https://github.com/middlewares/minifier)
* [CssMinifier](https://github.com/middlewares/minifier#cssminifier)
* [HtmlMinifier](https://github.com/middlewares/minifier#htmlminifier)
* [JsMinifier](https://github.com/middlewares/minifier#jsminifier)
* [Negotiation](https://github.com/middlewares/negotiation)
* [ContentType](https://github.com/middlewares/negotiation#contenttype)
* [ContentLanguage](https://github.com/middlewares/negotiation#contentlanguage)
* [ContentEncoding](https://github.com/middlewares/negotiation#contentencoding)
* [Payload](https://github.com/middlewares/payload)
* [CsvPayload](https://github.com/middlewares/payload#csvpayload)
* [JsonPayload](https://github.com/middlewares/payload#jsonpayload)
* [UrlEncodedPayload](https://github.com/middlewares/payload#urlencodepayload)
* [PhpSession](https://github.com/middlewares/php-session)
* [Recaptcha](https://github.com/middlewares/recaptcha)
* [ResponseTime](https://github.com/middlewares/response-time)
* [Robots](https://github.com/middlewares/robots)
* [Shutdown](https://github.com/middlewares/shutdown)
* [TrailingSlash](https://github.com/middlewares/trailing-slash)
* [Uuid](https://github.com/middlewares/uuid)
* [Whoops](https://github.com/middlewares/whoops)
* [Www](https://github.com/middlewares/www)


## Contributing

Use the package repository of each component to notify any issue or pull request related with it, and use this repositorio for generical questions, new middlewares discussions, etc.

See [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

---

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

0 comments on commit 1e3cdb9

Please sign in to comment.