-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
303 lines (251 loc) · 9.06 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace wdmg\amp;
/**
* Yii2 Google AMP
*
* @category Module
* @version 1.1.0
* @author Alexsander Vyshnyvetskyy <alex.vyshnyvetskyy@gmail.com>
* @link https://github.com/wdmg/yii2-amp
* @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;
/**
* RSS-feed module definition class
*/
class Module extends BaseModule
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'wdmg\amp\controllers';
/**
* {@inheritdoc}
*/
public $defaultRoute = "list/index";
/**
* @var string, the name of module
*/
public $name = "Google AMP";
/**
* @var string, the description of module
*/
public $description = "AMP pages generator";
/**
* @var array list of supported models for displaying a AMP pages
*/
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; // 1 hr.
/**
* @var string default route to render AMP pages (use "/" - for root)
*/
public $ampRoute = "/amp";
/**
* @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->ampRoute = self::normalizeRoute($this->ampRoute);
}
/**
* {@inheritdoc}
*/
public function dashboardNavItems($options = null)
{
$items = [
'label' => $this->name,
'icon' => 'fa fa-fw fa-bolt',
'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["amp.supportModels"]))
$this->supportModels = Yii::$app->params["amp.supportModels"];
if (isset(Yii::$app->params["amp.cacheExpire"]))
$this->cacheExpire = Yii::$app->params["amp.cacheExpire"];
if (isset(Yii::$app->params["amp.ampRoute"]))
$this->ampRoute = Yii::$app->params["amp.ampRoute"];
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->ampRoute))
throw new InvalidConfigException("Required module property `ampRoute` 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->ampRoute))
throw new InvalidConfigException("Module property `ampRoute` must be a string.");
// Add route to pass AMP pages in frontend
$ampRoute = $this->ampRoute;
if (empty($ampRoute) || $ampRoute == "/") {
$app->getUrlManager()->addRules([
[
'pattern' => '/<url:[\w-\/]+>',
'route' => 'admin/amp/default',
'suffix' => ''
],
'/<url:[\w-\/]+>' => 'admin/amp/default'
], true);
} else if (is_string($ampRoute)) {
$app->getUrlManager()->addRules([
[
'pattern' => $ampRoute . '/<url:[\w-\/]+>',
'route' => 'admin/amp/default',
'suffix' => ''
],
$ampRoute . '/<url:[\w-\/]+>' => 'admin/amp/default'
], true);
}
// Attach to events of create/change/remove of models for the subsequent clearing cache
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('google-amp'));
});
\yii\base\Event::on($class, $model::EVENT_AFTER_UPDATE, function ($event) use ($cache) {
$cache->delete(md5('google-amp'));
});
\yii\base\Event::on($class, $model::EVENT_AFTER_DELETE, function ($event) use ($cache) {
$cache->delete(md5('google-amp'));
});
}
}
}
}
}
}
/**
* Generate base AMP URL
*
* @return null|string
*/
public function getBaseURL() {
$url = null;
$ampRoute = $this->ampRoute;
if (empty($ampRoute) || $ampRoute == "/") {
$url = Url::to('/amp', true);
} else {
$url = Url::to($ampRoute . '/', true);
}
return $url;
}
/**
* Build URL to AMP page
*
* @param null $url
* @return bool|mixed
*/
public function buildAmpPageUrl($url = null) {
if ($url) {
$base = Url::base(true);
$amp_base = $this->getBaseURL();
return str_replace($base, rtrim($amp_base, '/'), $url);
}
return false;
}
/**
* Get items for building a Google AMP pages
*
* @return array
*/
public function getAmpItems() {
$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_amp' => 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;
}
/**
* Searches page resource among known URLs
*
* @param $url
* @return null if page not exist in AMP list
*/
public function findPage($url = null) {
if (is_null($url))
return null;
if ($this->cacheExpire !== 0 && ($cache = Yii::$app->getCache())) {
$data = $cache->getOrSet(md5('google-amp'), function () {
return $this->getAmpItems();
}, intval($this->cacheExpire));
} else {
$data = $this->getAmpItems();
}
foreach ($data as $item) {
if ($item['url'] === $url) {
return $item;
}
}
return null;
}
}