1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Parser; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\CleanParamParser; |
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\DirectiveParserCommons; |
6
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\HostParser; |
7
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\SitemapParser; |
8
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\UserAgentParser; |
9
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Core |
13
|
|
|
* |
14
|
|
|
* @package vipnytt\RobotsTxtParser\Parser |
15
|
|
|
*/ |
16
|
|
|
class RobotsTxtParser implements RobotsTxtInterface |
17
|
|
|
{ |
18
|
|
|
use DirectiveParserCommons; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Directive white list |
22
|
|
|
*/ |
23
|
|
|
const TOP_LEVEL_DIRECTIVES = [ |
24
|
|
|
self::DIRECTIVE_CLEAN_PARAM, |
25
|
|
|
self::DIRECTIVE_HOST, |
26
|
|
|
self::DIRECTIVE_SITEMAP, |
27
|
|
|
self::DIRECTIVE_USER_AGENT, |
28
|
|
|
]; |
29
|
|
|
/** |
30
|
|
|
* Current user-agent(s) |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $userAgentValues; |
34
|
|
|
/** |
35
|
|
|
* Clean-param class |
36
|
|
|
* @var CleanParamParser |
37
|
|
|
*/ |
38
|
|
|
protected $cleanParam; |
39
|
|
|
/** |
40
|
|
|
* Host class |
41
|
|
|
* @var HostParser |
42
|
|
|
*/ |
43
|
|
|
protected $host; |
44
|
|
|
/** |
45
|
|
|
* Sitemap class |
46
|
|
|
* @var SitemapParser |
47
|
|
|
*/ |
48
|
|
|
protected $sitemap; |
49
|
|
|
/** |
50
|
|
|
* User-agent class |
51
|
|
|
* @var UserAgentParser |
52
|
|
|
*/ |
53
|
|
|
protected $userAgent; |
54
|
|
|
/** |
55
|
|
|
* Previous directive |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $previousDirective; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Core constructor. |
62
|
|
|
* |
63
|
|
|
* @param string $content - file content |
64
|
|
|
*/ |
65
|
|
|
public function __construct($content) |
66
|
|
|
{ |
67
|
|
|
mb_internal_encoding(self::ENCODING); |
68
|
|
|
$this->cleanParam = new CleanParamParser(); |
69
|
|
|
$this->host = new HostParser(); |
70
|
|
|
$this->sitemap = new SitemapParser(); |
71
|
|
|
$this->userAgent = new UserAgentParser(); |
72
|
|
|
$this->parseTxt($content); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Client robots.txt |
77
|
|
|
* |
78
|
|
|
* @param string $txt |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
private function parseTxt($txt) |
82
|
|
|
{ |
83
|
|
|
$lines = array_filter(array_map('trim', mb_split('\r\n|\n|\r', $txt))); |
84
|
|
|
// Client each line individually |
85
|
|
|
foreach ($lines as $line) { |
86
|
|
|
// Limit rule length |
87
|
|
|
$line = mb_substr($line, 0, self::MAX_LENGTH_RULE); |
88
|
|
|
// Remove comments |
89
|
|
|
$line = mb_split('#', $line, 2)[0]; |
90
|
|
|
// Client line |
91
|
|
|
$this->add($line); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add line |
97
|
|
|
* |
98
|
|
|
* @param string $line |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function add($line) |
102
|
|
|
{ |
103
|
|
|
$previousDirective = $this->previousDirective; |
104
|
|
|
$pair = $this->generateRulePair($line, self::TOP_LEVEL_DIRECTIVES); |
105
|
|
|
if ($pair['directive'] === self::DIRECTIVE_USER_AGENT) { |
106
|
|
|
if ($previousDirective !== self::DIRECTIVE_USER_AGENT) { |
107
|
|
|
$this->userAgentValues = []; |
108
|
|
|
} |
109
|
|
|
$this->userAgentValues[] = $pair['value']; |
110
|
|
|
} |
111
|
|
|
$this->previousDirective = $pair['directive']; |
112
|
|
|
switch ($pair['directive']) { |
113
|
|
|
case self::DIRECTIVE_CLEAN_PARAM: |
114
|
|
|
return $this->cleanParam->add($pair['value']); |
115
|
|
|
case self::DIRECTIVE_HOST: |
116
|
|
|
return $this->host->add($pair['value']); |
117
|
|
|
case self::DIRECTIVE_SITEMAP: |
118
|
|
|
return $this->sitemap->add($pair['value']); |
119
|
|
|
case self::DIRECTIVE_USER_AGENT: |
120
|
|
|
return $this->userAgent->set($this->userAgentValues); |
121
|
|
|
} |
122
|
|
|
return $this->userAgent->add($line); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Render |
127
|
|
|
* |
128
|
|
|
* @param string $lineSeparator |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function render($lineSeparator = "\n") |
132
|
|
|
{ |
133
|
|
|
return implode($lineSeparator, array_merge( |
134
|
|
|
$this->cleanParam->render(), |
135
|
|
|
$this->host->render(), |
136
|
|
|
$this->sitemap->render(), |
137
|
|
|
$this->userAgent->render() |
138
|
|
|
)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Export rules |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function export() |
147
|
|
|
{ |
148
|
|
|
return array_merge( |
149
|
|
|
$this->cleanParam->export(), |
150
|
|
|
$this->host->export(), |
151
|
|
|
$this->sitemap->export(), |
152
|
|
|
$this->userAgent->export() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|