This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
/
RobotsTxtMiddleware.php
129 lines (120 loc) · 4.06 KB
/
RobotsTxtMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <joan@codeinc.fr>
// Date: 02/05/2018
// Time: 17:30
// Project: Psr15Middlewares
//
declare(strict_types=1);
namespace CodeInc\RobotsTxtMiddleware;
use Arcanedev\Robots\Robots;
use CodeInc\RobotsTxtMiddleware\Assets\RobotsTxtResponse;
use CodeInc\RobotsTxtMiddleware\Tests\RobotsTxtMiddlewareTest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* Class RobotsTxtMiddleware
*
* @uses Robots <https://github.com/ARCANEDEV/Robots>
* @see RobotsTxtMiddlewareTest
* @package CodeInc\RobotsTxtMiddleware
* @author Joan Fabrégat <joan@codeinc.fr>
* @license MIT <https://github.com/CodeIncHQ/Psr15Middlewares/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/Psr15Middlewares
*/
class RobotsTxtMiddleware extends Robots implements MiddlewareInterface
{
public const DEFAULT_URI_PATH = '/robots.txt';
/**
* @var string
*/
private $uriPath;
/**
* RobotsTxtMiddleware constructor.
*
* @param string $uriPath
*/
public function __construct(string $uriPath = self::DEFAULT_URI_PATH)
{
parent::__construct();
$this->uriPath = $uriPath;
}
/**
* Returns the URI path for which the robots.txt file is returned.
*
* @return string
*/
public function getUriPath():string
{
return $this->uriPath;
}
/**
* @inheritdoc
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler):ResponseInterface
{
if ($this->isRobotsTxtRequest($request)) {
return new RobotsTxtResponse($this);
}
else {
return $handler->handle($request);
}
}
/**
* Verifies if the requests points toward the robots.txt file.
*
* @param ServerRequestInterface $request
* @return bool
*/
public function isRobotsTxtRequest(ServerRequestInterface $request):bool
{
return $request->getUri()->getPath() == $this->uriPath;
}
/**
* Disallow all robots on /.
*
* @param string $uriPath
* @param string $baseUrl
* @return RobotsTxtMiddleware
*/
public static function disallowAll(string $uriPath = self::DEFAULT_URI_PATH, string $baseUrl = '/'):self
{
$robotTxtMiddlewre = new self($uriPath);
$robotTxtMiddlewre->addUserAgent('*');
$robotTxtMiddlewre->addDisallow($baseUrl);
return $robotTxtMiddlewre;
}
/**
* Allow all robots on /.
*
* @param string $uriPath
* @param string $baseUrl
* @return RobotsTxtMiddleware
*/
public static function allowAll(string $uriPath = self::DEFAULT_URI_PATH, string $baseUrl = '/'):self
{
$robotTxtMiddlewre = new self($uriPath);
$robotTxtMiddlewre->addUserAgent('*');
$robotTxtMiddlewre->addAllow($baseUrl);
return $robotTxtMiddlewre;
}
}