-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
247 lines (202 loc) · 7.95 KB
/
Module.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace wdmg\sitemap;
/**
* Yii2 Sitemap manager
*
* @category Module
* @version 1.2.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-sitemap
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
use wdmg\helpers\ArrayHelper;
use Yii;
use wdmg\base\BaseModule;
use yii\base\InvalidConfigException;
use yii\helpers\Url;
/**
* Sitemap module definition class
*/
class Module extends BaseModule
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'wdmg\sitemap\controllers';
/**
* {@inheritdoc}
*/
public $defaultRoute = "list/index";
/**
* @var string, the name of module
*/
public $name = "Sitemap";
/**
* @var string, the description of module
*/
public $description = "Sitemap manager";
/**
* @var array list of supported models for displaying a sitemap
*/
public $supportModels = [
'pages' => 'wdmg\pages\models\Pages',
'news' => 'wdmg\news\models\News',
'blog' => 'wdmg\blog\models\Posts'
];
/**
* @var int sitemap cache lifetime, `0` - for not use cache
*/
public $cacheExpire = 43200;
/**
* @var string default update frequency for sitemap.xml items
* See https://www.sitemaps.org/protocol.html#xmlTagDefinitions
*/
public $defaultFrequency = 'weekly';
/**
* @var float default update priority for sitemap.xml items
* See https://www.sitemaps.org/protocol.html#xmlTagDefinitions
*/
public $defaultPriority = 0.5;
/**
* @var string default route to rendered sitemap.xml (use "/" - for root)
*/
public $sitemapRoute = "/";
/**
* @var string the module version
*/
private $version = "1.2.0";
/**
* @var integer, priority of initialization
*/
private $priority = 4;
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// Set version of current module
$this->setVersion($this->version);
// Set priority of current module
$this->setPriority($this->priority);
// Process and normalize route for sitemap in frontend
$this->sitemapRoute = self::normalizeRoute($this->sitemapRoute);
}
/**
* {@inheritdoc}
*/
public function dashboardNavItems($options = null)
{
$items = [
'label' => $this->name,
'icon' => 'fa fa-fw fa-sitemap',
'url' => [$this->routePrefix . '/'. $this->id],
'active' => (in_array(\Yii::$app->controller->module->id, [$this->id]) && Yii::$app->controller->id == 'list'),
];
if (!is_null($options)) {
if (isset($options['count'])) {
$items['label'] .= '<span class="badge badge-default float-right">' . $options['count'] . '</span>';
unset($options['count']);
}
if (is_array($options))
$items = ArrayHelper::merge($items, $options);
}
return $items;
}
/**
* {@inheritdoc}
*/
public function bootstrap($app)
{
parent::bootstrap($app);
if (isset(Yii::$app->params["sitemap.supportModels"]))
$this->supportModels = Yii::$app->params["sitemap.supportModels"];
if (isset(Yii::$app->params["sitemap.cacheExpire"]))
$this->cacheExpire = Yii::$app->params["sitemap.cacheExpire"];
if (isset(Yii::$app->params["sitemap.defaultFrequency"]))
$this->defaultFrequency = Yii::$app->params["sitemap.defaultFrequency"];
if (isset(Yii::$app->params["sitemap.defaultPriority"]))
$this->defaultPriority = Yii::$app->params["sitemap.defaultPriority"];
if (isset(Yii::$app->params["sitemap.sitemapRoute"]))
$this->sitemapRoute = Yii::$app->params["sitemap.sitemapRoute"];
if (!isset($this->supportModels))
throw new InvalidConfigException("Required module property `supportModels` isn't set.");
if (!isset($this->cacheExpire))
throw new InvalidConfigException("Required module property `cacheExpire` isn't set.");
if (!isset($this->defaultFrequency))
throw new InvalidConfigException("Required module property `defaultFrequency` isn't set.");
if (!isset($this->defaultPriority))
throw new InvalidConfigException("Required module property `defaultPriority` isn't set.");
if (!isset($this->sitemapRoute))
throw new InvalidConfigException("Required module property `sitemapRoute` isn't set.");
if (!is_array($this->supportModels))
throw new InvalidConfigException("Module property `supportModels` must be array.");
if (!is_integer($this->cacheExpire))
throw new InvalidConfigException("Module property `cacheExpire` must be integer.");
if (!is_string($this->defaultFrequency))
throw new InvalidConfigException("Module property `defaultFrequency` must be a string.");
if (!in_array($this->defaultFrequency, ['always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never']))
throw new InvalidConfigException("Module property `defaultFrequency` must be one of the values: always, hourly, daily, weekly, monthly, yearly, never.");
if (!is_float($this->defaultPriority))
throw new InvalidConfigException("Module property `defaultPriority` must be float integer.");
if (!is_string($this->sitemapRoute))
throw new InvalidConfigException("Module property `sitemapRoute` must be a string.");
// Add route to pass sitemap.xml in frontend
$sitemapRoute = $this->sitemapRoute;
if (empty($sitemapRoute) || $sitemapRoute == "/") {
$app->getUrlManager()->addRules([
[
'pattern' => '/sitemap',
'route' => 'admin/sitemap/default',
'suffix' => '.xml'
],
'/sitemap.xml' => 'admin/sitemap/default'
], true);
} else if (is_string($sitemapRoute)) {
$app->getUrlManager()->addRules([
[
'pattern' => $sitemapRoute . '/sitemap',
'route' => 'admin/sitemap/default',
'suffix' => '.xml'
],
$sitemapRoute . '/sitemap.xml' => 'admin/sitemap/default'
], true);
}
// Attach to events of create/change/remove of models for the subsequent clearing cache of sitemap
if (!($app instanceof \yii\console\Application)) {
if (is_array($models = $this->supportModels) && $cache = $app->getCache()) {
foreach ($models as $name => $class) {
if (class_exists($class)) {
$model = new $class();
\yii\base\Event::on($class, $model::EVENT_AFTER_INSERT, function ($event) use ($cache) {
$cache->delete(md5('sitemap'));
});
\yii\base\Event::on($class, $model::EVENT_AFTER_UPDATE, function ($event) use ($cache) {
$cache->delete(md5('sitemap'));
});
\yii\base\Event::on($class, $model::EVENT_AFTER_DELETE, function ($event) use ($cache) {
$cache->delete(md5('sitemap'));
});
}
}
}
}
}
/**
* Generate current sitemap.xml URL
*
* @return null|string
*/
public function getSitemapURL() {
$url = null;
$sitemapRoute = $this->sitemapRoute;
if (empty($sitemapRoute) || $sitemapRoute == "/") {
$url = Url::to('/sitemap.xml', true);
} else {
$url = Url::to($sitemapRoute . '/sitemap.xml', true);
}
return $url;
}
}