-
Notifications
You must be signed in to change notification settings - Fork 2
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
0 parents
commit cf266b5
Showing
13 changed files
with
444 additions
and
0 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,15 @@ | ||
; This file is for unifying the coding style for different editors and IDEs. | ||
; More information at http://editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_size = 4 | ||
indent_style = space | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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,9 @@ | ||
* text=auto eol=lf | ||
|
||
/tests export-ignore | ||
.editorconfig export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.travis.yml export-ignore | ||
phpunit.xml export-ignore | ||
phpcs.xml export-ignore |
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,2 @@ | ||
vendor | ||
composer.lock |
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,14 @@ | ||
language: php | ||
sudo: false | ||
|
||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
before_script: | ||
- composer self-update | ||
- composer install -n | ||
|
||
script: | ||
- composer test |
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,9 @@ | ||
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## 0.1.0 - 2016-09-30 | ||
|
||
First version |
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,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 | ||
``` |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 @@ | ||
# middlewares/trailing-slash | ||
|
||
[![Latest Version on Packagist][ico-version]][link-packagist] | ||
[![Software License][ico-license]](LICENSE.md) | ||
[![Build Status][ico-travis]][link-travis] | ||
[![Quality Score][ico-scrutinizer]][link-scrutinizer] | ||
[![Total Downloads][ico-downloads]][link-downloads] | ||
[![SensioLabs Insight][ico-sensiolabs]][link-sensiolabs] | ||
|
||
Middleware to normalize the trailing slash of the uri path. By default removes the slash so, for example, `/post/23/` is converted to `/post/23`. Useful if you have problems with the router. | ||
|
||
## 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...) | ||
|
||
## Installation | ||
|
||
This package is installable and autoloadable via Composer as [middlewares/trailing-slash](https://packagist.org/packages/middlewares/trailing-slash). | ||
|
||
```sh | ||
composer require middlewares/trailing-slash | ||
``` | ||
|
||
## Example | ||
|
||
```php | ||
$dispatcher = new Dispatcher([ | ||
(new Middlewares\TrailingSlash(true)) | ||
->redirect() | ||
]); | ||
|
||
$response = $dispatcher->dispatch(new Request()); | ||
``` | ||
|
||
## Options | ||
|
||
#### `__construct(false)` | ||
|
||
Set `true` to add the slash instead remove so, for example, `post/23` is converted to `/post/23/`. Note that if the path contains an extension, the slash is **NOT** added. For example, `images/image.png` remains the same, instead be converted to `images/image.png/`. | ||
|
||
#### `redirect(true)` | ||
|
||
Set this option to return a `301` response redirecting to the new path | ||
|
||
--- | ||
|
||
Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details. | ||
|
||
The MIT License (MIT). Please see [LICENSE](LICENSE) for more information. | ||
|
||
[ico-version]: https://img.shields.io/packagist/v/middlewares/trailing-slash.svg?style=flat-square | ||
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square | ||
[ico-travis]: https://img.shields.io/travis/middlewares/trailing-slash/master.svg?style=flat-square | ||
[ico-scrutinizer]: https://img.shields.io/scrutinizer/g/middlewares/trailing-slash.svg?style=flat-square | ||
[ico-downloads]: https://img.shields.io/packagist/dt/middlewares/trailing-slash.svg?style=flat-square | ||
[ico-sensiolabs]: https://img.shields.io/sensiolabs/i/36786f5a-2a15-4399-8817-8f24fcd8c0b4.svg?style=flat-square | ||
|
||
[link-packagist]: https://packagist.org/packages/middlewares/trailing-slash | ||
[link-travis]: https://travis-ci.org/middlewares/trailing-slash | ||
[link-scrutinizer]: https://scrutinizer-ci.com/g/middlewares/trailing-slash | ||
[link-downloads]: https://packagist.org/packages/middlewares/trailing-slash | ||
[link-sensiolabs]: https://insight.sensiolabs.com/projects/36786f5a-2a15-4399-8817-8f24fcd8c0b4 |
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 @@ | ||
{ | ||
"name": "middlewares/trailing-slash", | ||
"type": "library", | ||
"description": "Middleware to normalize the trailing slash of the uri path", | ||
"license": "MIT", | ||
"keywords": [ | ||
"psr-7", | ||
"psr-15", | ||
"middleware", | ||
"path", | ||
"slash", | ||
"normalize", | ||
"http" | ||
], | ||
"homepage": "https://github.com/middlewares/trailing-slash", | ||
"support": { | ||
"issues": "https://github.com/middlewares/trailing-slash/issues" | ||
}, | ||
"require": { | ||
"php": "^5.6 || ^7.0", | ||
"psr/http-message": "~1.0", | ||
"http-interop/http-middleware": "^0.2", | ||
"middlewares/utils": "^0.1.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^5.5", | ||
"zendframework/zend-diactoros": "^1.3", | ||
"friendsofphp/php-cs-fixer": "^1.12", | ||
"squizlabs/php_codesniffer": "^2.7", | ||
"mindplay/middleman": "^2.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Middlewares\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Middlewares\\Tests\\": "tests/" | ||
} | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"phpunit", | ||
"phpcs" | ||
], | ||
"cs-fix": "php-cs-fixer fix . --fixers=-psr0" | ||
} | ||
} |
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,15 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="Middlewares coding standard"> | ||
<description>Middlewares coding standard</description> | ||
|
||
<!-- display progress --> | ||
<arg value="p"/> | ||
<arg name="colors"/> | ||
|
||
<!-- coding standard --> | ||
<rule ref="PSR2"/> | ||
|
||
<!-- Paths to check --> | ||
<file>src</file> | ||
<file>tests</file> | ||
</ruleset> |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="All tests"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</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,92 @@ | ||
<?php | ||
|
||
namespace Middlewares; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Interop\Http\Middleware\MiddlewareInterface; | ||
use Interop\Http\Middleware\DelegateInterface; | ||
|
||
class TrailingSlash implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var bool Add or remove the slash | ||
*/ | ||
private $trailingSlash; | ||
|
||
/** | ||
* @var bool Returns a redirect response or not | ||
*/ | ||
private $redirect; | ||
|
||
/** | ||
* Configure whether add or remove the slash. | ||
* | ||
* @param bool $trailingSlash | ||
*/ | ||
public function __construct($trailingSlash = false) | ||
{ | ||
$this->trailingSlash = (bool) $trailingSlash; | ||
} | ||
|
||
/** | ||
* Whether returns a 301 response to the new path. | ||
* | ||
* @param bool $redirect | ||
* | ||
* @return self | ||
*/ | ||
public function redirect($redirect = true) | ||
{ | ||
$this->redirect = (bool) $redirect; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Process a client request and return a response. | ||
* | ||
* @param RequestInterface $request | ||
* @param DelegateInterface $delegate | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function process(RequestInterface $request, DelegateInterface $delegate) | ||
{ | ||
$uri = $request->getUri(); | ||
$path = $this->normalize($uri->getPath()); | ||
|
||
if ($this->redirect && ($uri->getPath() !== $path)) { | ||
return Utils\Factory::createResponse(301) | ||
->withHeader('Location', (string) $uri->withPath($path)); | ||
} | ||
|
||
return $delegate->process($request->withUri($uri->withPath($path))); | ||
} | ||
|
||
/** | ||
* Normalize the trailing slash. | ||
* | ||
* @param string $path | ||
* | ||
* @return string | ||
*/ | ||
private function normalize($path) | ||
{ | ||
if ($path === '') { | ||
return '/'; | ||
} | ||
|
||
if (strlen($path) > 1) { | ||
if ($this->trailingSlash) { | ||
if (substr($path, -1) !== '/' && !pathinfo($path, PATHINFO_EXTENSION)) { | ||
return $path.'/'; | ||
} | ||
} else { | ||
return rtrim($path, '/'); | ||
} | ||
} | ||
|
||
return $path; | ||
} | ||
} |
Oops, something went wrong.