|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\patron; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\base\Plugin; |
|
13
|
|
|
use craft\events\RegisterUrlRulesEvent; |
|
14
|
|
|
use craft\helpers\ArrayHelper; |
|
15
|
|
|
use craft\helpers\Json; |
|
16
|
|
|
use craft\helpers\UrlHelper; |
|
17
|
|
|
use craft\services\ProjectConfig; |
|
18
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
19
|
|
|
use craft\web\UrlManager; |
|
20
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
|
21
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
|
22
|
|
|
use flipbox\craft\ember\modules\LoggerTrait; |
|
23
|
|
|
use flipbox\patron\events\handlers\ProjectConfigHandler; |
|
24
|
|
|
use flipbox\patron\events\RegisterProviderSettings; |
|
25
|
|
|
use flipbox\patron\models\Settings as SettingsModel; |
|
26
|
|
|
use flipbox\patron\queries\ProviderQuery; |
|
27
|
|
|
use flipbox\patron\queries\TokenQuery; |
|
28
|
|
|
use flipbox\patron\records\Provider; |
|
29
|
|
|
use flipbox\patron\records\Token; |
|
30
|
|
|
use flipbox\patron\settings\BaseSettings; |
|
31
|
|
|
use flipbox\patron\settings\SettingsInterface; |
|
32
|
|
|
use yii\base\Event; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @author Flipbox Factory <[email protected]> |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
* |
|
38
|
|
|
* @method SettingsModel getSettings() |
|
39
|
|
|
* |
|
40
|
|
|
* @property services\Session $session |
|
41
|
|
|
*/ |
|
42
|
|
|
class Patron extends Plugin |
|
43
|
|
|
{ |
|
44
|
|
|
use LoggerTrait; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
public static $category = 'patron'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The before persist token event name |
|
53
|
|
|
*/ |
|
54
|
|
|
const EVENT_BEFORE_PERSIST_TOKEN = 'beforePersistToken'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
3 |
|
* The after persist token event name |
|
58
|
|
|
*/ |
|
59
|
3 |
|
const EVENT_AFTER_PERSIST_TOKEN = 'afterPersistToken'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
3 |
|
* @return string |
|
63
|
3 |
|
*/ |
|
64
|
|
|
protected static function getLogFileName(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return 'patron'; |
|
67
|
3 |
|
} |
|
68
|
3 |
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function init() |
|
73
|
3 |
|
{ |
|
74
|
3 |
|
parent::init(); |
|
75
|
2 |
|
|
|
76
|
|
|
// Components |
|
77
|
|
|
$this->setComponents([ |
|
78
|
|
|
'session' => services\Session::class |
|
79
|
3 |
|
]); |
|
80
|
|
|
|
|
81
|
|
|
// Modules |
|
82
|
|
|
$this->setModules([ |
|
83
|
3 |
|
'cp' => cp\Cp::class |
|
84
|
3 |
|
]); |
|
85
|
3 |
|
|
|
86
|
3 |
|
|
|
87
|
|
|
// Project config |
|
88
|
|
|
$this->registerProjectConfigEvents(); |
|
89
|
|
|
|
|
90
|
3 |
|
|
|
91
|
3 |
|
// Template variables |
|
92
|
3 |
|
Event::on( |
|
93
|
2 |
|
CraftVariable::class, |
|
94
|
|
|
CraftVariable::EVENT_INIT, |
|
95
|
|
|
function (Event $event) { |
|
96
|
|
|
/** @var CraftVariable $variable */ |
|
97
|
|
|
$variable = $event->sender; |
|
98
|
|
|
$variable->set('patron', self::getInstance()); |
|
99
|
|
|
} |
|
100
|
3 |
|
); |
|
101
|
|
|
|
|
102
|
|
|
// CP routes |
|
103
|
|
|
Event::on( |
|
104
|
3 |
|
UrlManager::class, |
|
105
|
3 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
|
106
|
3 |
|
[self::class, 'onRegisterCpUrlRules'] |
|
107
|
3 |
|
); |
|
108
|
3 |
|
|
|
109
|
|
|
// Register our site routes |
|
110
|
3 |
|
Event::on( |
|
111
|
|
|
UrlManager::class, |
|
112
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
113
|
|
|
function (RegisterUrlRulesEvent $event) { |
|
114
|
|
|
if ($callbackUrlRule = $this->getSettings()->getCallbackUrlRule()) { |
|
115
|
|
|
$event->rules = array_merge( |
|
116
|
|
|
$event->rules, |
|
117
|
3 |
|
$callbackUrlRule |
|
118
|
3 |
|
); |
|
119
|
3 |
|
} |
|
120
|
3 |
|
} |
|
121
|
|
|
); |
|
122
|
3 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Register project config events, if we're able to |
|
126
|
|
|
*/ |
|
127
|
3 |
|
protected function registerProjectConfigEvents() |
|
128
|
|
|
{ |
|
129
|
|
|
if (!version_compare(Craft::$app->getVersion(), '3.1', '>=')) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Project Config |
|
134
|
|
|
Craft::$app->projectConfig |
|
135
|
|
|
->onAdd( |
|
136
|
|
|
'patronProviders.{uid}', |
|
137
|
|
|
[ |
|
138
|
|
|
ProjectConfigHandler::class, |
|
139
|
|
|
'handleChangedProvider' |
|
140
|
|
|
] |
|
141
|
|
|
) |
|
142
|
|
|
->onUpdate( |
|
143
|
|
|
'patronProviders.{uid}', |
|
144
|
|
|
[ProjectConfigHandler::class, |
|
145
|
|
|
'handleChangedProvider' |
|
146
|
|
|
] |
|
147
|
|
|
) |
|
148
|
|
|
->onRemove( |
|
149
|
|
|
'patronProviders.{uid}', |
|
150
|
|
|
[ProjectConfigHandler::class, |
|
151
|
|
|
'handleDeletedProvider' |
|
152
|
|
|
] |
|
153
|
|
|
) |
|
154
|
|
|
->onAdd( |
|
155
|
|
|
'patronTokens.{uid}', |
|
156
|
|
|
[ProjectConfigHandler::class, |
|
157
|
|
|
'handleChangedToken' |
|
158
|
|
|
] |
|
159
|
|
|
) |
|
160
|
|
|
->onUpdate( |
|
161
|
|
|
'patronTokens.{uid}', |
|
162
|
|
|
[ProjectConfigHandler::class, |
|
163
|
|
|
'handleChangedToken' |
|
164
|
|
|
] |
|
165
|
3 |
|
) |
|
166
|
|
|
->onRemove( |
|
167
|
3 |
|
'patronTokens.{uid}', |
|
168
|
|
|
[ProjectConfigHandler::class, |
|
169
|
|
|
'handleDeletedToken' |
|
170
|
|
|
] |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
|
|
Event::on( |
|
174
|
|
|
ProjectConfig::class, |
|
175
|
|
|
ProjectConfig::EVENT_REBUILD, |
|
176
|
|
|
[ |
|
177
|
|
|
events\handlers\ProjectConfigHandler::class, |
|
178
|
|
|
'rebuild' |
|
179
|
|
|
] |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
Event::on( |
|
183
|
|
|
Provider::class, |
|
184
|
|
|
Provider::EVENT_AFTER_INSERT, |
|
185
|
|
|
[ |
|
186
|
|
|
events\ManageProviderProjectConfig::class, |
|
187
|
|
|
'save' |
|
188
|
|
|
] |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
|
|
Event::on( |
|
192
|
|
|
Provider::class, |
|
193
|
|
|
Provider::EVENT_AFTER_UPDATE, |
|
194
|
|
|
[ |
|
195
|
|
|
events\ManageProviderProjectConfig::class, |
|
196
|
|
|
'save' |
|
197
|
|
|
] |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
Event::on( |
|
201
|
|
|
Provider::class, |
|
202
|
|
|
Provider::EVENT_AFTER_DELETE, |
|
203
|
|
|
[ |
|
204
|
|
|
events\ManageProviderProjectConfig::class, |
|
205
|
|
|
'delete' |
|
206
|
|
|
] |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
Event::on( |
|
210
|
|
|
Token::class, |
|
211
|
|
|
Token::EVENT_AFTER_INSERT, |
|
212
|
|
|
[ |
|
213
|
|
|
events\ManageTokenProjectConfig::class, |
|
214
|
|
|
'save' |
|
215
|
|
|
] |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
Event::on( |
|
219
|
|
|
Token::class, |
|
220
|
|
|
Token::EVENT_AFTER_UPDATE, |
|
221
|
|
|
[ |
|
222
|
|
|
events\ManageTokenProjectConfig::class, |
|
223
|
|
|
'save' |
|
224
|
|
|
] |
|
225
|
|
|
); |
|
226
|
|
|
|
|
227
|
|
|
Event::on( |
|
228
|
3 |
|
Token::class, |
|
229
|
|
|
Token::EVENT_AFTER_DELETE, |
|
230
|
|
|
[ |
|
231
|
|
|
events\ManageTokenProjectConfig::class, |
|
232
|
3 |
|
'delete' |
|
233
|
|
|
] |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/******************************************* |
|
239
|
|
|
* NAV |
|
240
|
|
|
*******************************************/ |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @inheritdoc |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getCpNavItem() |
|
246
|
|
|
{ |
|
247
|
|
|
return array_merge( |
|
248
|
|
|
parent::getCpNavItem(), |
|
249
|
|
|
[ |
|
250
|
|
|
'subnav' => [ |
|
251
|
|
|
'patron.providers' => [ |
|
252
|
|
|
'label' => Craft::t('patron', 'Providers'), |
|
253
|
|
|
'url' => 'patron/providers', |
|
254
|
|
|
], |
|
255
|
|
|
'patron.settings' => [ |
|
256
|
|
|
'label' => Craft::t('patron', 'Settings'), |
|
257
|
|
|
'url' => 'patron/settings', |
|
258
|
|
|
] |
|
259
|
|
|
] |
|
260
|
|
|
] |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
/******************************************* |
|
266
|
|
|
* SETTINGS |
|
267
|
|
|
*******************************************/ |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @inheritdoc |
|
271
|
|
|
* @return SettingsModel |
|
272
|
|
|
*/ |
|
273
|
|
|
public function createSettingsModel() |
|
274
|
|
|
{ |
|
275
|
|
|
return new SettingsModel(); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @inheritdoc |
|
280
|
|
|
* @throws \yii\base\ExitException |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getSettingsResponse() |
|
283
|
|
|
{ |
|
284
|
|
|
Craft::$app->getResponse()->redirect( |
|
285
|
|
|
UrlHelper::cpUrl('patron/settings') |
|
286
|
|
|
); |
|
287
|
|
|
|
|
288
|
|
|
Craft::$app->end(); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/******************************************* |
|
292
|
|
|
* QUERIES |
|
293
|
|
|
*******************************************/ |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param array $config |
|
297
|
|
|
* @return ProviderQuery |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getProviders(array $config = []): ProviderQuery |
|
300
|
|
|
{ |
|
301
|
|
|
$query = new ProviderQuery(); |
|
302
|
|
|
|
|
303
|
|
|
QueryHelper::configure( |
|
304
|
|
|
$query, |
|
305
|
|
|
$config |
|
306
|
|
|
); |
|
307
|
|
|
|
|
308
|
|
|
return $query; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @param array $config |
|
313
|
|
|
* @return TokenQuery |
|
314
|
|
|
*/ |
|
315
|
|
|
public function getTokens(array $config = []): TokenQuery |
|
316
|
|
|
{ |
|
317
|
|
|
$query = new TokenQuery(); |
|
318
|
|
|
|
|
319
|
|
|
QueryHelper::configure( |
|
320
|
|
|
$query, |
|
321
|
|
|
$config |
|
322
|
|
|
); |
|
323
|
|
|
|
|
324
|
|
|
return $query; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
/******************************************* |
|
329
|
|
|
* SERVICES |
|
330
|
|
|
*******************************************/ |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
334
|
|
|
* @return services\Session |
|
335
|
|
|
*/ |
|
336
|
|
|
public function getSession(): services\Session |
|
337
|
|
|
{ |
|
338
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
339
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
340
|
|
|
return $this->get('session'); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
/******************************************* |
|
345
|
|
|
* MODULES |
|
346
|
|
|
*******************************************/ |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
350
|
|
|
* @return cp\Cp |
|
351
|
|
|
*/ |
|
352
|
|
|
public function getCp(): cp\Cp |
|
353
|
|
|
{ |
|
354
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
355
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
356
|
|
|
return $this->getModule('cp'); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
|
|
360
|
|
|
/******************************************* |
|
361
|
|
|
* PROVIDER SETTINGS |
|
362
|
|
|
*******************************************/ |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* @param string|null $class |
|
366
|
|
|
* @param array $settings |
|
367
|
|
|
* @return SettingsInterface |
|
368
|
|
|
* @throws \yii\base\InvalidConfigException |
|
369
|
|
|
*/ |
|
370
|
|
|
public function providerSettings(string $class = null, $settings = []): SettingsInterface |
|
371
|
|
|
{ |
|
372
|
|
|
if (null === $class) { |
|
373
|
|
|
return new BaseSettings(); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
$event = new RegisterProviderSettings(); |
|
377
|
|
|
|
|
378
|
|
|
RegisterProviderSettings::trigger( |
|
379
|
|
|
$class, |
|
380
|
|
|
RegisterProviderSettings::REGISTER_SETTINGS, |
|
381
|
|
|
$event |
|
382
|
|
|
); |
|
383
|
|
|
|
|
384
|
|
|
if (is_string($settings)) { |
|
385
|
|
|
$settings = Json::decodeIfJson($settings); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
if (!is_array($settings)) { |
|
389
|
|
|
$settings = ArrayHelper::toArray($settings, []); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$settings['class'] = $event->class; |
|
393
|
|
|
|
|
394
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
395
|
|
|
return ObjectHelper::create($settings, SettingsInterface::class); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
|
|
399
|
|
|
/******************************************* |
|
400
|
|
|
* EVENTS |
|
401
|
|
|
*******************************************/ |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @param RegisterUrlRulesEvent $event |
|
405
|
|
|
*/ |
|
406
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
|
407
|
|
|
{ |
|
408
|
|
|
$event->rules = array_merge( |
|
409
|
|
|
$event->rules, |
|
410
|
|
|
[ |
|
411
|
|
|
// SETTINGS |
|
412
|
|
|
'patron/settings' => 'patron/cp/view/settings/index', |
|
413
|
|
|
|
|
414
|
|
|
// BASE |
|
415
|
|
|
'patron' => |
|
416
|
|
|
'patron/cp/view/general/index', |
|
417
|
|
|
|
|
418
|
|
|
// PROVIDERS |
|
419
|
|
|
'patron/providers' => |
|
420
|
|
|
'patron/cp/view/providers/default/index', |
|
421
|
|
|
|
|
422
|
|
|
'patron/providers/new' => |
|
423
|
|
|
'patron/cp/view/providers/default/upsert', |
|
424
|
|
|
|
|
425
|
|
|
'patron/providers/<identifier:\d+>' => |
|
426
|
|
|
'patron/cp/view/providers/default/upsert', |
|
427
|
|
|
|
|
428
|
|
|
// TOKENS |
|
429
|
|
|
'patron/providers/<provider:\d+>/tokens' => |
|
430
|
|
|
'patron/cp/view/providers/tokens/index', |
|
431
|
|
|
|
|
432
|
|
|
'patron/providers/<provider:\d+>/tokens/<identifier:\d+>' => |
|
433
|
|
|
'patron/cp/view/providers/tokens/upsert', |
|
434
|
|
|
] |
|
435
|
|
|
); |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/******************************************* |
|
439
|
|
|
* TRANSLATE |
|
440
|
|
|
*******************************************/ |
|
441
|
|
|
|
|
442
|
|
|
/** |
|
443
|
|
|
* Translates a message to the specified language. |
|
444
|
|
|
* |
|
445
|
|
|
* This is a shortcut method of [[\Craft::t()]]. |
|
446
|
|
|
* * |
|
447
|
|
|
* @param string $message the message to be translated. |
|
448
|
|
|
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message. |
|
449
|
|
|
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current |
|
450
|
|
|
* [[\yii\base\Application::language|application language]] will be used. |
|
451
|
|
|
* @return string the translated message. |
|
452
|
|
|
*/ |
|
453
|
|
|
public static function t($message, $params = [], $language = null) |
|
454
|
|
|
{ |
|
455
|
|
|
return Craft::t(self::$category, $message, $params, $language); |
|
456
|
|
|
} |
|
457
|
|
|
} |
|
458
|
|
|
|