-
Notifications
You must be signed in to change notification settings - Fork 6
/
AllowParser.php
168 lines (153 loc) · 3.86 KB
/
AllowParser.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* vipnytt/RobotsTxtParser
*
* @link https://github.com/VIPnytt/RobotsTxtParser
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
*/
namespace vipnytt\RobotsTxtParser\Parser\Directives;
use vipnytt\RobotsTxtParser\Client\Directives\AllowClient;
use vipnytt\RobotsTxtParser\Exceptions;
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
/**
* Class AllowParser
*
* @package vipnytt\RobotsTxtParser\Parser\Directives
*/
class AllowParser implements ParserInterface, RobotsTxtInterface
{
use DirectiveParserTrait;
/**
* Sub directives white list
*/
const SUB_DIRECTIVES = [
self::DIRECTIVE_CLEAN_PARAM,
self::DIRECTIVE_HOST,
];
/**
* Directive
* @var string
*/
private $directive;
/**
* Path
* @var array
*/
private $path = [];
/**
* Sub-directive Clean-param
* @var InlineCleanParamParser
*/
private $cleanParam;
/**
* Sub-directive Host
* @var InlineHostParser
*/
private $host;
/**
* Optimized for performance
* @var bool
*/
private $optimized = false;
/**
* AllowParser constructor
*
* @param string $base
* @param string $effective
* @param string $directive
*/
public function __construct($base, $effective, $directive)
{
$this->directive = $directive;
$this->cleanParam = new InlineCleanParamParser();
$this->host = new InlineHostParser($base, $effective);
}
/**
* Add
*
* @param string $line
* @return bool
*/
public function add($line)
{
$pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
switch ($pair['directive']) {
case self::DIRECTIVE_CLEAN_PARAM:
return $this->cleanParam->add($pair['value']);
case self::DIRECTIVE_HOST:
return $this->host->add($pair['value']);
}
return $this->addPath($line);
}
/**
* Add plain path to allow/disallow
*
* @param string $path
* @return bool
*/
private function addPath($path)
{
$path = rtrim($path, '*');
if (!in_array('/', $this->path) &&
in_array(mb_substr($path, 0, 1), [
'/',
'*',
])
) {
$this->path[] = $path;
$this->optimized = false;
}
return in_array($path, $this->path);
}
/**
* Render
*
* @param RenderHandler $handler
* @return bool
*/
public function render(RenderHandler $handler)
{
$this->removeOverlapping();
sort($this->path);
$inline = new RenderHandler($handler->getLevel());
$this->host->render($inline);
$this->cleanParam->render($inline);
$handler->addInline($this->directive, $inline);
foreach ($this->path as $path) {
$handler->add($this->directive, $path);
}
return true;
}
/**
* Remove overlapping paths
*
* @return bool
*/
private function removeOverlapping()
{
if (!$this->optimized) {
foreach ($this->path as $key1 => &$path1) {
foreach ($this->path as $key2 => &$path2) {
if ($key1 !== $key2 &&
mb_strpos($path1, $path2) === 0
) {
unset($this->path[$key1]);
}
}
}
$this->optimized = true;
}
return $this->optimized;
}
/**
* Client
*
* @return AllowClient
*/
public function client()
{
$this->removeOverlapping();
return new AllowClient($this->path, $this->host->client(), $this->cleanParam->client());
}
}