Skip to content

Commit b603eee

Browse files
authored
Merge pull request #1 from php-middleware/psr-15
PSR-15
2 parents e702211 + d4d7d59 commit b603eee

5 files changed

+55
-36
lines changed

.travis.yml

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
language: php
22

3-
matrix:
4-
fast_finish: true
5-
include:
6-
- php: 5.5
7-
- php: 5.6
8-
- php: 7
9-
- php: hhvm
10-
allow_failures:
11-
- php: 7
12-
- php: hhvm
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
137

14-
install:
15-
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
16-
- composer info -i
8+
env:
9+
- DEPS=lowest
10+
- DEPS=latest
11+
12+
before_script:
13+
- phpenv config-rm xdebug.ini
14+
- if [[ $DEPS == 'lowest' ]]; then composer update --prefer-stable --no-interaction --prefer-lowest ; fi
15+
- if [[ $DEPS == 'latest' ]]; then composer update --prefer-stable --no-interaction ; fi
1716

1817
script:
1918
- ./vendor/bin/phpunit

README.md

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
# block-robots middleware [![Build Status](https://travis-ci.org/php-middleware/block-robots.svg)](https://travis-ci.org/php-middleware/block-robots)
2-
Middleware to avoid search engine indexing
1+
# block-robots middleware [![Build Status](https://travis-ci.org/php-middleware/block-robots.svg?branch=master)](https://travis-ci.org/php-middleware/block-robots)
2+
PSR-15 middleware to avoid search engine indexing with PSR-7
33

44
This middleware provide framework-agnostic possibility to preventing your site from being indexed.
55

66
## How it works?
77

88
* Add `X-Robots-Tag` header with `noindex, nofollow` value.
9-
* Add `robots.txt` "file" with `Disallow: /` body
9+
* Add `robots.txt` "file" with `User-Agent: * Disallow: /` body
1010

1111
## Installation
1212

13-
```json
14-
{
15-
"require": {
16-
"php-middleware/block-robots": "^1.0.0"
17-
}
18-
}
13+
```bash
14+
composer require php-middleware/block-robots
1915
```
2016

2117
```php

composer.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
{
22
"name": "php-middleware/block-robots",
3-
"description": "Middleware to avoid search engine indexing with PSR-7",
3+
"description": "PSR-15 middleware to avoid search engine indexing with PSR-7",
44
"type": "library",
55
"keywords": [
66
"middleware",
77
"psr",
88
"psr-7",
9+
"psr-15",
910
"seo",
1011
"robots"
1112
],
1213
"require": {
13-
"php": ">=5.5",
14-
"psr/http-message": "^1.0"
14+
"php": ">=5.6",
15+
"psr/http-message": "^1.0",
16+
"php-middleware/double-pass-compatibility": "^1.0",
17+
"http-interop/http-middleware": "^0.4.1"
1518
},
1619
"require-dev": {
17-
"phpunit/phpunit": "^4.8.6",
20+
"phpunit/phpunit": "^5.7 || ^6.1",
1821
"zendframework/zend-diactoros": "^1.1.3"
1922
},
2023
"autoload": {

src/BlockRobotsMiddleware.php

+29-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,45 @@
22

33
namespace PhpMiddleware\BlockRobots;
44

5+
use Interop\Http\ServerMiddleware\DelegateInterface;
6+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7+
use PhpMiddleware\DoublePassCompatibilityTrait;
58
use Psr\Http\Message\ResponseInterface;
69
use Psr\Http\Message\ServerRequestInterface;
710
use Zend\Diactoros\Response;
811

9-
class BlockRobotsMiddleware
12+
class BlockRobotsMiddleware implements MiddlewareInterface
1013
{
14+
use DoublePassCompatibilityTrait;
15+
1116
const ROBOTS_HEADER = 'X-Robots-Tag';
1217

13-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
18+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
1419
{
15-
if ($request->getUri()->getPath() === '/robots.txt') {
16-
$newReponse = new Response('php://memory', 200, ['Content-Type' => 'text/plain']);
17-
$newReponse->getBody()->write("User-Agent: *\nDisallow: /");
18-
19-
return $newReponse;
20+
if ($this->isRobotsTxt($request)) {
21+
return $this->createRobotsTxtResponse();
2022
}
21-
/* @var $out ResponseInterface */
22-
$response = $out === null ? $response : $out($request, $response);
2323

24+
$response = $delegate->process($request);
25+
26+
return $this->attachRobotsHeaderTo($response);
27+
}
28+
29+
private function createRobotsTxtResponse()
30+
{
31+
$response = new Response('php://memory', 200, ['Content-Type' => 'text/plain']);
32+
$response->getBody()->write("User-Agent: *\nDisallow: /");
33+
34+
return $response;
35+
}
36+
37+
private function isRobotsTxt(ServerRequestInterface $request)
38+
{
39+
return $request->getUri()->getPath() === '/robots.txt';
40+
}
41+
42+
private function attachRobotsHeaderTo(ResponseInterface $response)
43+
{
2444
return $response->withHeader(self::ROBOTS_HEADER, 'noindex, nofollow');
2545
}
2646
}

test/BlockRobotsMiddlewareTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace PhpMiddlewareTest\BlockRobots;
44

55
use PhpMiddleware\BlockRobots\BlockRobotsMiddleware;
6+
use PHPUnit\Framework\TestCase;
67
use Psr\Http\Message\ResponseInterface;
78
use Zend\Diactoros\Response;
89
use Zend\Diactoros\ServerRequest;
910
use Zend\Diactoros\Uri;
1011

11-
class BlockRobotsMiddlewareTest extends \PHPUnit_Framework_TestCase
12+
class BlockRobotsMiddlewareTest extends TestCase
1213
{
1314
protected $middleware;
1415

0 commit comments

Comments
 (0)