Skip to content

PSR-15 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
language: php

matrix:
fast_finish: true
include:
- php: 5.5
- php: 5.6
- php: 7
- php: hhvm
allow_failures:
- php: 7
- php: hhvm
php:
- 5.6
- 7.0
- 7.1

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
- composer info -i
env:
- DEPS=lowest
- DEPS=latest

before_script:
- phpenv config-rm xdebug.ini
- if [[ $DEPS == 'lowest' ]]; then composer update --prefer-stable --no-interaction --prefer-lowest ; fi
- if [[ $DEPS == 'latest' ]]; then composer update --prefer-stable --no-interaction ; fi

script:
- ./vendor/bin/phpunit
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
# block-robots middleware [![Build Status](https://travis-ci.org/php-middleware/block-robots.svg)](https://travis-ci.org/php-middleware/block-robots)
Middleware to avoid search engine indexing
# block-robots middleware [![Build Status](https://travis-ci.org/php-middleware/block-robots.svg?branch=master)](https://travis-ci.org/php-middleware/block-robots)
PSR-15 middleware to avoid search engine indexing with PSR-7

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

## How it works?

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

## Installation

```json
{
"require": {
"php-middleware/block-robots": "^1.0.0"
}
}
```bash
composer require php-middleware/block-robots
```

```php
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{
"name": "php-middleware/block-robots",
"description": "Middleware to avoid search engine indexing with PSR-7",
"description": "PSR-15 middleware to avoid search engine indexing with PSR-7",
"type": "library",
"keywords": [
"middleware",
"psr",
"psr-7",
"psr-15",
"seo",
"robots"
],
"require": {
"php": ">=5.5",
"psr/http-message": "^1.0"
"php": ">=5.6",
"psr/http-message": "^1.0",
"php-middleware/double-pass-compatibility": "^1.0",
"http-interop/http-middleware": "^0.4.1"
},
"require-dev": {
"phpunit/phpunit": "^4.8.6",
"phpunit/phpunit": "^5.7 || ^6.1",
"zendframework/zend-diactoros": "^1.1.3"
},
"autoload": {
Expand Down
38 changes: 29 additions & 9 deletions src/BlockRobotsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,45 @@

namespace PhpMiddleware\BlockRobots;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PhpMiddleware\DoublePassCompatibilityTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

class BlockRobotsMiddleware
class BlockRobotsMiddleware implements MiddlewareInterface
{
use DoublePassCompatibilityTrait;

const ROBOTS_HEADER = 'X-Robots-Tag';

public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
if ($request->getUri()->getPath() === '/robots.txt') {
$newReponse = new Response('php://memory', 200, ['Content-Type' => 'text/plain']);
$newReponse->getBody()->write("User-Agent: *\nDisallow: /");

return $newReponse;
if ($this->isRobotsTxt($request)) {
return $this->createRobotsTxtResponse();
}
/* @var $out ResponseInterface */
$response = $out === null ? $response : $out($request, $response);

$response = $delegate->process($request);

return $this->attachRobotsHeaderTo($response);
}

private function createRobotsTxtResponse()
{
$response = new Response('php://memory', 200, ['Content-Type' => 'text/plain']);
$response->getBody()->write("User-Agent: *\nDisallow: /");

return $response;
}

private function isRobotsTxt(ServerRequestInterface $request)
{
return $request->getUri()->getPath() === '/robots.txt';
}

private function attachRobotsHeaderTo(ResponseInterface $response)
{
return $response->withHeader(self::ROBOTS_HEADER, 'noindex, nofollow');
}
}
3 changes: 2 additions & 1 deletion test/BlockRobotsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace PhpMiddlewareTest\BlockRobots;

use PhpMiddleware\BlockRobots\BlockRobotsMiddleware;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;

class BlockRobotsMiddlewareTest extends \PHPUnit_Framework_TestCase
class BlockRobotsMiddlewareTest extends TestCase
{
protected $middleware;

Expand Down