mad-web /
laravel-robots
| 1 | <?php |
||
| 2 | |||
| 3 | namespace MadWeb\Robots; |
||
| 4 | |||
| 5 | class Robots |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * The lines of for the robots.txt. |
||
| 9 | * |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $lines = []; |
||
| 13 | |||
| 14 | /** @var callable|bool */ |
||
| 15 | protected static $shouldIndex = true; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Generate the robots.txt data. |
||
| 19 | * |
||
| 20 | * @return string |
||
| 21 | */ |
||
| 22 | 27 | public function generate() |
|
| 23 | { |
||
| 24 | 27 | return implode(PHP_EOL, $this->lines); |
|
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Add a Sitemap to the robots.txt. |
||
| 29 | * |
||
| 30 | * @param string $sitemap |
||
| 31 | */ |
||
| 32 | 6 | public function addSitemap($sitemap) |
|
| 33 | { |
||
| 34 | 6 | $this->addLine("Sitemap: $sitemap"); |
|
| 35 | 6 | } |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Add a User-agent to the robots.txt. |
||
| 39 | * |
||
| 40 | * @param string $userAgent |
||
| 41 | */ |
||
| 42 | 3 | public function addUserAgent($userAgent) |
|
| 43 | { |
||
| 44 | 3 | $this->addLine("User-agent: $userAgent"); |
|
| 45 | 3 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Add a Host to the robots.txt. |
||
| 49 | * |
||
| 50 | * @param string $host |
||
| 51 | */ |
||
| 52 | 6 | public function addHost($host) |
|
| 53 | { |
||
| 54 | 6 | $this->addLine("Host: $host"); |
|
| 55 | 6 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Add a disallow rule to the robots.txt. |
||
| 59 | * |
||
| 60 | * @param string|array $directories |
||
| 61 | */ |
||
| 62 | 3 | public function addDisallow($directories) |
|
| 63 | { |
||
| 64 | 3 | $this->addRuleLine($directories, 'Disallow'); |
|
| 65 | 3 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Add a allow rule to the robots.txt. |
||
| 69 | * |
||
| 70 | * @param string|array $directories |
||
| 71 | */ |
||
| 72 | 3 | public function addAllow($directories) |
|
| 73 | { |
||
| 74 | 3 | $this->addRuleLine($directories, 'Allow'); |
|
| 75 | 3 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Add a rule to the robots.txt. |
||
| 79 | * |
||
| 80 | * @param string|array $directories |
||
| 81 | * @param string $rule |
||
| 82 | */ |
||
| 83 | 6 | public function addRuleLine($directories, string $rule) |
|
| 84 | { |
||
| 85 | 6 | foreach ((array) $directories as $directory) { |
|
| 86 | 6 | $this->addLine("$rule: $directory"); |
|
| 87 | } |
||
| 88 | 6 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Add a comment to the robots.txt. |
||
| 92 | * |
||
| 93 | * @param string $comment |
||
| 94 | */ |
||
| 95 | 6 | public function addComment($comment) |
|
| 96 | { |
||
| 97 | 6 | $this->addLine("# $comment"); |
|
| 98 | 6 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Add a spacer to the robots.txt. |
||
| 102 | */ |
||
| 103 | 3 | public function addSpacer() |
|
| 104 | { |
||
| 105 | 3 | $this->addLine(''); |
|
| 106 | 3 | } |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Add a line to the robots.txt. |
||
| 110 | * |
||
| 111 | * @param string $line |
||
| 112 | */ |
||
| 113 | 24 | public function addLine(string $line) |
|
| 114 | { |
||
| 115 | 24 | $this->lines[] = $line; |
|
| 116 | 24 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Add multiple lines to the robots.txt. |
||
| 120 | * |
||
| 121 | * @param string|array $lines |
||
| 122 | */ |
||
| 123 | protected function addLines($lines) |
||
| 124 | { |
||
| 125 | foreach ((array) $lines as $line) { |
||
| 126 | $this->addLine($line); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Reset the lines. |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | 3 | public function reset() |
|
| 136 | { |
||
| 137 | 3 | $this->lines = []; |
|
| 138 | 3 | } |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Set callback with should index condition. |
||
| 142 | */ |
||
| 143 | 9 | public function setShouldIndexCallback(callable $callback) |
|
| 144 | { |
||
| 145 | 9 | self::$shouldIndex = $callback; |
|
| 146 | 9 | } |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Check is application should be indexed. |
||
| 150 | */ |
||
| 151 | 9 | public function shouldIndex(): bool |
|
| 152 | { |
||
| 153 | 9 | if (is_callable(self::$shouldIndex)) { |
|
| 154 | 9 | return (bool) call_user_func(self::$shouldIndex); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 155 | } |
||
| 156 | |||
| 157 | return self::$shouldIndex; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Render robots meta tag. |
||
| 162 | */ |
||
| 163 | 3 | public function metaTag(): string |
|
| 164 | { |
||
| 165 | 3 | return '<meta name="robots" content="'.($this->shouldIndex() ? 'index, follow' : 'noindex, nofollow').'">'; |
|
| 166 | } |
||
| 167 | } |
||
| 168 |