1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Behavior for XML Sitemap Yii2 module. |
4
|
|
|
* |
5
|
|
|
* @author Serge Larin <[email protected]> |
6
|
|
|
* @link https://github.com/assayer-pro/yii2-sitemap-module |
7
|
|
|
* @copyright 2015 Assayer Pro Company |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
9
|
|
|
* |
10
|
|
|
* based on https://github.com/himiklab/yii2-sitemap-module |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace assayerpro\sitemap\behaviors; |
14
|
|
|
|
15
|
|
|
use yii\base\Behavior; |
16
|
|
|
use yii\base\InvalidConfigException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Behavior for XML Sitemap Yii2 module. |
20
|
|
|
* |
21
|
|
|
* For example: |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* public function behaviors() |
25
|
|
|
* { |
26
|
|
|
* return [ |
27
|
|
|
* 'sitemap' => [ |
28
|
|
|
* 'class' => SitemapBehavior::className(), |
29
|
|
|
* 'scope' => function ($model) { |
30
|
|
|
* $model->select(['url', 'lastmod']); |
31
|
|
|
* $model->andWhere(['is_deleted' => 0]); |
32
|
|
|
* }, |
33
|
|
|
* 'dataClosure' => function ($model) { |
34
|
|
|
* return [ |
35
|
|
|
* 'loc' => yii\helpers\Url::to($model->url, true), |
36
|
|
|
* 'lastmod' => Sitemap::dateToW3C($model->lastmod), |
37
|
|
|
* 'changefreq' => Sitemap::DAILY, |
38
|
|
|
* 'priority' => 0.8 |
39
|
|
|
* ]; |
40
|
|
|
* } |
41
|
|
|
* ], |
42
|
|
|
* ]; |
43
|
|
|
* } |
44
|
|
|
* ``` |
45
|
|
|
* |
46
|
|
|
* @see http://www.sitemaps.org/protocol.html |
47
|
|
|
* @author Serge Larin <[email protected]> |
48
|
|
|
* @author HimikLab |
49
|
|
|
* @package assayerpro\sitemap |
50
|
|
|
*/ |
51
|
|
|
class SitemapBehavior extends Behavior |
52
|
|
|
{ |
53
|
|
|
const BATCH_MAX_SIZE = 100; |
54
|
|
|
|
55
|
|
|
/** @var callable function generate url array for model */ |
56
|
|
|
public $dataClosure; |
57
|
|
|
|
58
|
|
|
/** @var string|bool default time for model change frequency */ |
59
|
|
|
public $defaultChangefreq = false; |
60
|
|
|
|
61
|
|
|
/** @var float|bool default priority for model */ |
62
|
|
|
public $defaultPriority = false; |
63
|
|
|
|
64
|
|
|
/** @var callable function for model filter */ |
65
|
|
|
public $scope; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function init() |
71
|
|
|
{ |
72
|
|
|
if (!is_callable($this->dataClosure)) { |
73
|
|
|
throw new InvalidConfigException('SitemapBehavior::$dataClosure isn\'t callable.'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* generate sitemap structure for model |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function generateSiteMap() |
84
|
|
|
{ |
85
|
|
|
$result = []; |
86
|
|
|
|
87
|
|
|
/** @var \yii\db\ActiveRecord $owner */ |
88
|
|
|
$owner = $this->owner; |
89
|
|
|
$query = $owner::find(); |
90
|
|
|
if (is_callable($this->scope)) { |
91
|
|
|
call_user_func($this->scope, $query); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($query->each(self::BATCH_MAX_SIZE) as $model) { |
95
|
|
|
|
96
|
|
|
$urlData = call_user_func($this->dataClosure, $model); |
97
|
|
|
|
98
|
|
|
if (empty($urlData)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (!isset($urlData['changefreq']) && ($this->defaultChangefreq !== false)) { |
103
|
|
|
$urlData['changefreq'] = $this->defaultChangefreq; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!isset($urlData['priority']) && ($this->defaultPriority !== false)) { |
107
|
|
|
$urlData['priority'] = $this->defaultPriority; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$result[] = $urlData; |
111
|
|
|
} |
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|