|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Robots; |
|
4
|
|
|
|
|
5
|
|
|
use Robots\Contracts\Robots as RobotsContract; |
|
6
|
|
|
|
|
7
|
|
|
class Robots implements RobotsContract |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The rows of for the robots. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $rows = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* Array with configuration set in constructor |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $parameters = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Robots constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $parameters |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $parameters = []) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->parameters = $parameters; |
|
32
|
|
|
$this->composeFromParameters(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* Create Robots object from array |
|
38
|
|
|
* |
|
39
|
|
|
*/ |
|
40
|
|
|
private function composeFromParameters(): void |
|
41
|
|
|
{ |
|
42
|
|
|
foreach($this->parameters as $key => $values) { |
|
43
|
|
|
if($key === 'allows') { |
|
44
|
|
|
foreach($values as $value) { |
|
45
|
|
|
$this->addAllow($value); |
|
46
|
|
|
} |
|
47
|
|
|
} elseif($key === 'disallows') { |
|
48
|
|
|
foreach($values as $value) { |
|
49
|
|
|
$this->addDisallow($value); |
|
50
|
|
|
} |
|
51
|
|
|
} elseif($key === 'hosts') { |
|
52
|
|
|
foreach($values as $value) { |
|
53
|
|
|
$this->addHost($value); |
|
54
|
|
|
} |
|
55
|
|
|
} elseif($key === 'sitemaps') { |
|
56
|
|
|
foreach($values as $value) { |
|
57
|
|
|
$this->addSitemap($value); |
|
58
|
|
|
} |
|
59
|
|
|
} elseif($key === 'userAgents') { |
|
60
|
|
|
foreach($values as $value) { |
|
61
|
|
|
$this->addUserAgent($value); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add a allow rule to the robots. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string|array $directories |
|
71
|
|
|
* @return \Robots\Contracts\Robot; |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addAllow($directories): RobotsContract |
|
74
|
|
|
{ |
|
75
|
|
|
$this->addRuleLine($directories, 'Allow'); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Add a comment to the robots. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $comment |
|
84
|
|
|
* @return \Robots\Contracts\Robot; |
|
85
|
|
|
*/ |
|
86
|
|
|
public function addComment(string $comment): RobotsContract |
|
87
|
|
|
{ |
|
88
|
|
|
$this->addLine("# $comment"); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Add a disallow rule to the robots. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|array $directories |
|
97
|
|
|
* @return \Robots\Contracts\Robot; |
|
98
|
|
|
*/ |
|
99
|
|
|
public function addDisallow($directories): RobotsContract |
|
100
|
|
|
{ |
|
101
|
|
|
$this->addRuleLine($directories, 'Disallow'); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add a Host to the robots. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $host |
|
110
|
|
|
* @return \Robots\Contracts\Robot; |
|
111
|
|
|
*/ |
|
112
|
|
|
public function addHost(string $host): RobotsContract |
|
113
|
|
|
{ |
|
114
|
|
|
$this->addLine("Host: $host"); |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add a row to the robots. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $row |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function addLine($row) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->rows[] = (string) $row; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Add multiple rows to the robots. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string|array $rows |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function addRows($rows) |
|
135
|
|
|
{ |
|
136
|
|
|
foreach ((array) $rows as $row) { |
|
137
|
|
|
$this->addLine($row); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Add a rule to the robots. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string|array $directories |
|
145
|
|
|
* @param string $rule |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function addRuleLine($directories, $rule) |
|
148
|
|
|
{ |
|
149
|
|
|
foreach ((array) $directories as $directory) { |
|
150
|
|
|
$this->addLine("$rule: $directory"); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Add a Sitemap to the robots. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $sitemap |
|
158
|
|
|
* @return \Robots\Contracts\Robot; |
|
159
|
|
|
*/ |
|
160
|
|
|
public function addSitemap(string $sitemap): RobotsContract |
|
161
|
|
|
{ |
|
162
|
|
|
$this->addLine("Sitemap: $sitemap"); |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Add a spacer to the robots. |
|
169
|
|
|
* @return \Robots\Contracts\Robot; |
|
170
|
|
|
*/ |
|
171
|
|
|
public function addSpacer(): RobotsContract |
|
172
|
|
|
{ |
|
173
|
|
|
$this->addLine(''); |
|
174
|
|
|
|
|
175
|
|
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Add a User-agent to the robots. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $userAgent |
|
182
|
|
|
* @return \Robots\Contracts\Robot; |
|
183
|
|
|
*/ |
|
184
|
|
|
public function addUserAgent(string $userAgent): RobotsContract |
|
185
|
|
|
{ |
|
186
|
|
|
$this->addLine("User-agent: $userAgent"); |
|
187
|
|
|
|
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Generate the robots data. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function generate(): string |
|
197
|
|
|
{ |
|
198
|
|
|
return implode(PHP_EOL, $this->rows); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Reset the rows. |
|
203
|
|
|
* |
|
204
|
|
|
* @return \Robots\Contracts\Robot; |
|
205
|
|
|
*/ |
|
206
|
|
|
public function reset(): RobotsContract |
|
207
|
|
|
{ |
|
208
|
|
|
$this->rows = []; |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|