-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGenerator.php
82 lines (73 loc) · 1.86 KB
/
Generator.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
<?php
/**
* Created by PhpStorm.
* User: execut
* Date: 14.06.16
* Time: 17:12
*/
namespace execut\robotsTxt;
use yii\base\Component;
use yii\helpers\Url;
use yii;
class Generator extends Component
{
/** @var sting */
public $host = '';
/** @var string */
public $sitemap = '';
/** @var array */
public $userAgent = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty($this->host)) {
if (Yii::$app->request->IsSecureConnection) {
$this->host = Yii::$app->request->hostInfo;
} else {
$this->host = Yii::$app->request->serverName;
}
}
}
/**
* render robots.txt
*
* @return string
*/
public function render()
{
$result = "";
$params = [];
$params['Host'] = $this->host;
$siteMap = $this->sitemap;
if (is_array($siteMap)) {
$siteMap = Url::to($siteMap, true);
}
$params['Sitemap'] = $siteMap;
foreach (array_filter($params) as $key => $value) {
$result .= "$key: $value\n";
}
foreach ($this->userAgent as $userAgent => $value) {
$result .= "User-agent: $userAgent\n";
foreach ($value as $param => $urls) {
if (is_string($urls)) {
$urls = [$urls];
}
$isWithScheme = false;
if ($param == 'Sitemap') {
$urls = [$urls];
$isWithScheme = true;
}
foreach ($urls as $url) {
if (is_array($url)) {
$url = Url::to($url, $isWithScheme);
}
$result .= "$param: $url\n";
}
}
}
return $result;
}
}