1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DJStarCOM\RobotsManager; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class RobotsManager |
7
|
|
|
* @package DJStarCOM\RobotsManager |
8
|
|
|
*/ |
9
|
|
|
class RobotsManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The lines for the robots.txt. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $lines = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Generate the robots.txt data. |
20
|
|
|
* |
21
|
|
|
* @return string|null |
22
|
|
|
*/ |
23
|
|
|
public function generate(): ?string |
24
|
|
|
{ |
25
|
|
|
return implode(PHP_EOL, $this->lines); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add a Sitemap to the robots.txt. |
30
|
|
|
* |
31
|
|
|
* @param string $sitemap |
32
|
|
|
*/ |
33
|
|
|
public function addSitemap(string $sitemap): void |
34
|
|
|
{ |
35
|
|
|
$this->addLine('Sitemap: ' . $sitemap); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a line to the robots.txt. |
40
|
|
|
* |
41
|
|
|
* @param string $line |
42
|
|
|
*/ |
43
|
|
|
protected function addLine(string $line): void |
44
|
|
|
{ |
45
|
|
|
$this->lines[] = $line; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add a User-agent to the robots.txt. |
50
|
|
|
* |
51
|
|
|
* @param string $userAgent |
52
|
|
|
*/ |
53
|
|
|
public function addUserAgent(string $userAgent): void |
54
|
|
|
{ |
55
|
|
|
$this->addLine('User-agent: ' . $userAgent); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add a Host to the robots.txt. |
60
|
|
|
* |
61
|
|
|
* @param string $host |
62
|
|
|
*/ |
63
|
|
|
public function addHost(string $host): void |
64
|
|
|
{ |
65
|
|
|
$this->addLine('Host: ' . $host); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Add a disallow rule to the robots.txt. |
70
|
|
|
* |
71
|
|
|
* @param string|array $directories |
72
|
|
|
*/ |
73
|
|
|
public function addDisallow($directories): void |
74
|
|
|
{ |
75
|
|
|
$this->addRuleLine($directories, 'Disallow'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add a rule to the robots.txt. |
80
|
|
|
* |
81
|
|
|
* @param string|array $directories |
82
|
|
|
* @param string $rule |
83
|
|
|
*/ |
84
|
|
|
protected function addRuleLine($directories, string $rule): void |
85
|
|
|
{ |
86
|
|
|
foreach ((array)$directories as $directory) { |
87
|
|
|
$this->addLine($rule . ': ' . $directory); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add a allow rule to the robots.txt. |
93
|
|
|
* |
94
|
|
|
* @param string|array $directories |
95
|
|
|
*/ |
96
|
|
|
public function addAllow($directories): void |
97
|
|
|
{ |
98
|
|
|
$this->addRuleLine($directories, 'Allow'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add a comment to the robots.txt. |
103
|
|
|
* |
104
|
|
|
* @param string $comment |
105
|
|
|
*/ |
106
|
|
|
public function addComment(string $comment): void |
107
|
|
|
{ |
108
|
|
|
$this->addLine('# ' . $comment); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add a spacer to the robots.txt. |
113
|
|
|
*/ |
114
|
|
|
public function addSpacer(): void |
115
|
|
|
{ |
116
|
|
|
$this->addLine(''); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Reset the lines. |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function reset(): void |
125
|
|
|
{ |
126
|
|
|
$this->lines = []; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|