|
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\UrlHelper; |
|
15
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
16
|
|
|
use craft\web\UrlManager; |
|
17
|
|
|
use flipbox\ember\modules\LoggerTrait; |
|
18
|
|
|
use flipbox\patron\models\Settings as SettingsModel; |
|
19
|
|
|
use yii\base\Event; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Flipbox Factory <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @method SettingsModel getSettings() |
|
26
|
|
|
* |
|
27
|
|
|
* @property services\Providers $providers |
|
28
|
|
|
* @property services\ProviderSettings $providerSettings |
|
29
|
|
|
* @property services\ProviderLocks $providerLocks |
|
30
|
|
|
* @property services\Tokens $tokens |
|
31
|
|
|
* @property services\ManageProviders $manageProviders |
|
32
|
|
|
* @property services\ManageTokens $manageTokens |
|
33
|
|
|
* @property services\Session $session |
|
34
|
|
|
*/ |
|
35
|
|
|
class Patron extends Plugin |
|
36
|
|
|
{ |
|
37
|
|
|
use LoggerTrait; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected static function getLogFileName(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return 'patron'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
*/ |
|
50
|
18 |
|
public function init() |
|
51
|
|
|
{ |
|
52
|
18 |
|
parent::init(); |
|
53
|
|
|
|
|
54
|
|
|
// Components |
|
55
|
18 |
|
$this->setComponents([ |
|
56
|
18 |
|
'providers' => services\Providers::class, |
|
57
|
|
|
'providerSettings' => services\ProviderSettings::class, |
|
58
|
|
|
'providerLocks' => services\ProviderLocks::class, |
|
59
|
|
|
'tokens' => services\Tokens::class, |
|
60
|
|
|
'manageProviders' => services\ManageProviders::class, |
|
61
|
|
|
'manageTokens' => services\ManageTokens::class, |
|
62
|
|
|
'session' => services\Session::class |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
// Modules |
|
66
|
18 |
|
$this->setModules([ |
|
67
|
18 |
|
'cp' => cp\Cp::class |
|
68
|
|
|
|
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
// Template variables |
|
72
|
18 |
|
Event::on( |
|
73
|
18 |
|
CraftVariable::class, |
|
74
|
18 |
|
CraftVariable::EVENT_INIT, |
|
75
|
6 |
|
function (Event $event) { |
|
76
|
|
|
/** @var CraftVariable $variable */ |
|
77
|
|
|
$variable = $event->sender; |
|
78
|
|
|
$variable->set('patron', self::getInstance()); |
|
79
|
18 |
|
} |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
// CP routes |
|
83
|
18 |
|
Event::on( |
|
84
|
18 |
|
UrlManager::class, |
|
85
|
18 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
|
86
|
18 |
|
[self::class, 'onRegisterCpUrlRules'] |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
// Register our site routes |
|
90
|
18 |
|
Event::on( |
|
91
|
18 |
|
UrlManager::class, |
|
92
|
18 |
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
93
|
6 |
|
function (RegisterUrlRulesEvent $event) { |
|
94
|
|
|
if ($callbackUrlRule = $this->getSettings()->getCallbackUrlRule()) { |
|
95
|
|
|
$event->rules = array_merge( |
|
96
|
|
|
$event->rules, |
|
97
|
|
|
$callbackUrlRule |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
18 |
|
} |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
// Add default environments upon creation |
|
104
|
18 |
|
$defaultEnvironments = $this->getSettings()->getDefaultEnvironments(); |
|
105
|
18 |
|
if (!empty($defaultEnvironments)) { |
|
106
|
18 |
|
Event::on( |
|
107
|
18 |
|
records\ProviderInstance::class, |
|
108
|
18 |
|
records\ProviderInstance::EVENT_BEFORE_INSERT, |
|
109
|
|
|
[ |
|
110
|
18 |
|
events\handlers\BeforeInsertProviderInstance::class, |
|
111
|
|
|
'handle' |
|
112
|
|
|
] |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Replicate environments to token |
|
117
|
18 |
|
if ($this->getSettings()->getAutoPopulateTokenEnvironments() === true) { |
|
118
|
18 |
|
Event::on( |
|
119
|
18 |
|
records\Token::class, |
|
120
|
18 |
|
records\Token::EVENT_BEFORE_INSERT, |
|
121
|
|
|
[ |
|
122
|
18 |
|
events\handlers\BeforeInsertToken::class, |
|
123
|
|
|
'handle' |
|
124
|
|
|
] |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
18 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritdoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getCpNavItem() |
|
133
|
|
|
{ |
|
134
|
|
|
return array_merge( |
|
135
|
|
|
parent::getCpNavItem(), |
|
136
|
|
|
[ |
|
137
|
|
|
'subnav' => [ |
|
138
|
|
|
'patron.providers' => [ |
|
139
|
|
|
'label' => Craft::t('patron', 'Providers'), |
|
140
|
|
|
'url' => 'patron/providers', |
|
141
|
|
|
], |
|
142
|
|
|
'patron.settings' => [ |
|
143
|
|
|
'label' => Craft::t('patron', 'Settings'), |
|
144
|
|
|
'url' => 'patron/settings', |
|
145
|
|
|
] |
|
146
|
|
|
] |
|
147
|
|
|
] |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @inheritdoc |
|
153
|
|
|
* @return SettingsModel |
|
154
|
|
|
*/ |
|
155
|
18 |
|
public function createSettingsModel() |
|
156
|
|
|
{ |
|
157
|
18 |
|
return new SettingsModel(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @inheritdoc |
|
162
|
|
|
* @throws \yii\base\ExitException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getSettingsResponse() |
|
165
|
|
|
{ |
|
166
|
|
|
Craft::$app->getResponse()->redirect( |
|
167
|
|
|
UrlHelper::cpUrl('patron/settings') |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
Craft::$app->end(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/******************************************* |
|
174
|
|
|
* SERVICES |
|
175
|
|
|
*******************************************/ |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
179
|
|
|
* @return services\Providers |
|
180
|
|
|
*/ |
|
181
|
3 |
|
public function getProviders(): services\Providers |
|
182
|
|
|
{ |
|
183
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
184
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
185
|
3 |
|
return $this->get('providers'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
190
|
|
|
* @return services\ProviderSettings |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getProviderSettings(): services\ProviderSettings |
|
193
|
|
|
{ |
|
194
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
195
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
196
|
|
|
return $this->get('providerSettings'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
201
|
|
|
* @return services\ProviderLocks |
|
202
|
|
|
*/ |
|
203
|
3 |
|
public function getProviderLocks(): services\ProviderLocks |
|
204
|
|
|
{ |
|
205
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
206
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
207
|
3 |
|
return $this->get('providerLocks'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
212
|
|
|
* @return services\Tokens |
|
213
|
|
|
*/ |
|
214
|
3 |
|
public function getTokens(): services\Tokens |
|
215
|
|
|
{ |
|
216
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
217
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
218
|
3 |
|
return $this->get('tokens'); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
223
|
|
|
* @return services\ManageProviders |
|
224
|
|
|
*/ |
|
225
|
3 |
|
public function manageProviders(): services\ManageProviders |
|
226
|
|
|
{ |
|
227
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
228
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
229
|
3 |
|
return $this->get('manageProviders'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
234
|
|
|
* @return services\ManageTokens |
|
235
|
|
|
*/ |
|
236
|
3 |
|
public function manageTokens(): services\ManageTokens |
|
237
|
|
|
{ |
|
238
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
239
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
240
|
3 |
|
return $this->get('manageTokens'); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
245
|
|
|
* @return services\Session |
|
246
|
|
|
*/ |
|
247
|
3 |
|
public function getSession(): services\Session |
|
248
|
|
|
{ |
|
249
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
250
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
251
|
3 |
|
return $this->get('session'); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
/******************************************* |
|
256
|
|
|
* MODULES |
|
257
|
|
|
*******************************************/ |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
|
261
|
|
|
* @return cp\Cp |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getCp(): cp\Cp |
|
264
|
|
|
{ |
|
265
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
266
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
267
|
|
|
return $this->getModule('cp'); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/******************************************* |
|
271
|
|
|
* EVENTS |
|
272
|
|
|
*******************************************/ |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @param RegisterUrlRulesEvent $event |
|
276
|
|
|
*/ |
|
277
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
|
278
|
|
|
{ |
|
279
|
|
|
$event->rules = array_merge( |
|
280
|
|
|
$event->rules, |
|
281
|
|
|
[ |
|
282
|
|
|
// SETTINGS |
|
283
|
|
|
'patron/settings' => 'patron/cp/view/settings/index', |
|
284
|
|
|
|
|
285
|
|
|
// BASE |
|
286
|
|
|
'patron' => |
|
287
|
|
|
'patron/cp/view/general/index', |
|
288
|
|
|
|
|
289
|
|
|
// PROVIDERS |
|
290
|
|
|
'patron/providers' => |
|
291
|
|
|
'patron/cp/view/providers/default/index', |
|
292
|
|
|
|
|
293
|
|
|
'patron/providers/new' => |
|
294
|
|
|
'patron/cp/view/providers/default/upsert', |
|
295
|
|
|
|
|
296
|
|
|
// INSTANCES |
|
297
|
|
|
'patron/providers/<identifier:\d+>' => |
|
298
|
|
|
'patron/cp/view/providers/default/upsert', |
|
299
|
|
|
|
|
300
|
|
|
'patron/providers/<provider:\d+>/instances/<identifier:\d+>' => |
|
301
|
|
|
'patron/cp/view/providers/instances/upsert', |
|
302
|
|
|
|
|
303
|
|
|
'patron/providers/<provider:\d+>/instances/new' => |
|
304
|
|
|
'patron/cp/view/providers/instances/upsert', |
|
305
|
|
|
|
|
306
|
|
|
// TOKENS |
|
307
|
|
|
'patron/providers/<provider:\d+>/tokens' => |
|
308
|
|
|
'patron/cp/view/providers/tokens/index', |
|
309
|
|
|
|
|
310
|
|
|
'patron/providers/<provider:\d+>/tokens/<identifier:\d+>' => |
|
311
|
|
|
'patron/cp/view/providers/tokens/upsert', |
|
312
|
|
|
] |
|
313
|
|
|
); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|