-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathRobots.php
101 lines (83 loc) · 1.54 KB
/
Robots.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
<?php
namespace Melbahja\Seo;
use Melbahja\Seo\Interfaces\SeoInterface;
/**
* @package Melbahja\Seo
* @since v2.0
* @see https://git.io/phpseo
* @license MIT
* @copyright 2019-present Mohamed Elabhja
*/
class Robots implements SeoInterface
{
/**
* Robots rules.
* @var array
*/
protected $rules = [];
/**
* Sitemap urls.
* @var array
*/
protected $sitemaps = [];
/**
* Add rules for bot by user agent name.
*
* @param string $userAgent bot user agent name
* @param array $rules
* @return Robots
*/
public function bot(string $userAgent, array $rules): Robots
{
$this->rules[$userAgent] = $rules;
return $this;
}
/**
* Set sitemap url.
*
* @param string $url
* @return Robots
*/
public function sitemap(string $url): Robots
{
$this->sitemaps[] = $url;
return $this;
}
/**
* Build robots rules.
*
* @return string
*/
public function __toString(): string
{
$out = "# Autogenerated by melbahja/seo\r\n";
foreach ($this->sitemaps as $url)
{
$out .= "Sitemap: {$url}\r\n";
}
if ($out !== "") {
$out .= "\r\n";
}
foreach($this->rules as $agent => $rules)
{
$out .= "User-agent: {$agent}\r\n";
if (isset($rules['allow'])) {
foreach ($rules['allow'] as $v)
{
$out .= "Allow: {$v}\r\n";
}
}
if (isset($rules['disallow'])) {
foreach($rules['disallow'] as $v)
{
$out .= "Disallow: {$v}\r\n";
}
}
if (isset($rules['delay'])) {
$out .= "Crawl-delay: {$rules['delay']}\r\n";
}
$out .= "\r\n";
}
return $out;
}
}