-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
270 lines (224 loc) · 8.66 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
namespace wdmg\turbo;
/**
* Yii2 Yandex.Turbo pages generator
*
* @category Module
* @version 1.1.0
* @author Alexsander Vyshnyvetskyy <alex.vyshnyvetskyy@gmail.com>
* @link https://github.com/wdmg/yii2-turbo
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
use Yii;
use wdmg\base\BaseModule;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/**
* Yandex.Turbo module definition class
*/
class Module extends BaseModule
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'wdmg\turbo\controllers';
/**
* {@inheritdoc}
*/
public $defaultRoute = "list/index";
/**
* @var string, the name of module
*/
public $name = "Yandex.Turbo";
/**
* @var string, the description of module
*/
public $description = "Turbo-pages generator";
/**
* @var array list of supported models for displaying a Turbo-pages feed
*/
public $supportModels = [
'pages' => 'wdmg\pages\models\Pages',
'news' => 'wdmg\news\models\News',
'blog' => 'wdmg\blog\models\Posts'
];
/**
* @var int cache lifetime, `0` - for not use cache
*/
public $cacheExpire = 3600;
/**
* @var array default channel options
*/
public $channelOptions = [];
/**
* @var string default route to render Turbo-pages feed (use "/" - for root)
*/
public $turboRoute = "/turbo";
/**
* @var string the module version
*/
private $version = "1.1.0";
/**
* @var integer, priority of initialization
*/
private $priority = 5;
/**
* {@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 frontend
$this->turboRoute = self::normalizeRoute($this->turboRoute);
}
/**
* {@inheritdoc}
*/
public function dashboardNavItems($options = null)
{
$items = [
'label' => $this->name,
'icon' => 'fa fa-fw fa-rocket',
'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 = \wdmg\helpers\ArrayHelper::merge($items, $options);
}
return $items;
}
/**
* {@inheritdoc}
*/
public function bootstrap($app)
{
parent::bootstrap($app);
if (isset(Yii::$app->params["turbo.supportModels"]))
$this->supportModels = Yii::$app->params["turbo.supportModels"];
if (isset(Yii::$app->params["turbo.cacheExpire"]))
$this->cacheExpire = Yii::$app->params["turbo.cacheExpire"];
if (isset(Yii::$app->params["turbo.channelOptions"]))
$this->channelOptions = Yii::$app->params["turbo.channelOptions"];
if (isset(Yii::$app->params["turbo.turboRoute"]))
$this->turboRoute = Yii::$app->params["turbo.turboRoute"];
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->channelOptions))
throw new InvalidConfigException("Required module property `channelOptions` isn't set.");
if (!isset($this->turboRoute))
throw new InvalidConfigException("Required module property `turboRoute` isn't set.");
if (!is_array($this->supportModels))
throw new InvalidConfigException("Module property `supportModels` must be array.");
if (!is_array($this->channelOptions))
throw new InvalidConfigException("Module property `channelOptions` must be array.");
if (!is_integer($this->cacheExpire))
throw new InvalidConfigException("Module property `cacheExpire` must be integer.");
if (!is_string($this->turboRoute))
throw new InvalidConfigException("Module property `turboRoute` must be a string.");
// Add route to pass turbo-pages in frontend
$turboRoute = $this->turboRoute;
if (empty($turboRoute) || $turboRoute == "/") {
$app->getUrlManager()->addRules([
[
'pattern' => '/turbo',
'route' => 'admin/turbo/default',
'suffix' => '.xml'
],
'/turbo.xml' => 'admin/turbo/default'
], true);
} else if (is_string($turboRoute)) {
$app->getUrlManager()->addRules([
[
'pattern' => $turboRoute . '/feed',
'route' => 'admin/turbo/default',
'suffix' => '.xml'
],
$turboRoute . '/feed.xml' => 'admin/turbo/default'
], true);
}
// Attach to events of create/change/remove of models for the subsequent clearing cache of feeds
if (!($app instanceof \yii\console\Application)) {
if ($cache = $app->getCache()) {
if (is_array($models = $this->supportModels)) {
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('yandex-turbo'));
});
\yii\base\Event::on($class, $model::EVENT_AFTER_UPDATE, function ($event) use ($cache) {
$cache->delete(md5('yandex-turbo'));
});
\yii\base\Event::on($class, $model::EVENT_AFTER_DELETE, function ($event) use ($cache) {
$cache->delete(md5('yandex-turbo'));
});
}
}
}
}
}
}
/**
* Generate current RSS-feed URL
*
* @return null|string
*/
public function getFeedURL() {
$url = null;
$turboRoute = $this->turboRoute;
if (empty($turboRoute) || $turboRoute == "/") {
$url = Url::to('/turbo.xml', true);
} else {
$url = Url::to($turboRoute . '/feed.xml', true);
}
return $url;
}
/**
* Get items for building a Yandex.Turbo pages
*
* @return array
*/
public function getTurboItems() {
$items = [];
if (is_array($models = $this->supportModels)) {
foreach ($models as $name => $class) {
// If class of model exist
if (class_exists($class)) {
$model = new $class();
// If module is loaded
if ($model->getModule()) {
$append = [];
foreach ($model->getAllPublished(['in_turbo' => true]) as $item) {
$append[] = [
'url' => (isset($item->url)) ? $item->url : null,
'name' => (isset($item->name)) ? $item->name : null,
'title' => (isset($item->title)) ? $item->title : null,
'image' => (isset($item->image)) ? $model->getImagePath(true) . '/' . $item->image : null,
'description' => (isset($item->excerpt)) ? $item->excerpt : ((isset($item->description)) ? $item->description : null),
'content' => (isset($item->content)) ? $item->content : null,
'updated_at' => (isset($item->updated_at)) ? $item->updated_at : null,
'status' => (isset($item->status)) ? (($item->status) ? true : false) : false
];
};
$items = ArrayHelper::merge($items, $append);
}
}
}
}
return $items;
}
}