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\patron\models\Settings as SettingsModel; |
18
|
|
|
use yii\base\Event; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
* |
24
|
|
|
* @method SettingsModel getSettings() |
25
|
|
|
* |
26
|
|
|
* @property services\Providers $providers |
27
|
|
|
* @property services\Tokens $tokens |
28
|
|
|
* @property services\ManageProviders $manageProviders |
29
|
|
|
* @property services\ManageTokens $manageTokens |
30
|
|
|
* @property services\Session $session |
31
|
|
|
*/ |
32
|
|
|
class Patron extends Plugin |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
15 |
|
public function init() |
38
|
|
|
{ |
39
|
15 |
|
parent::init(); |
40
|
|
|
|
41
|
|
|
// Components |
42
|
15 |
|
$this->setComponents([ |
43
|
15 |
|
'providers' => services\Providers::class, |
44
|
|
|
'tokens' => services\Tokens::class, |
45
|
|
|
'manageProviders' => services\ManageProviders::class, |
46
|
|
|
'manageTokens' => services\ManageTokens::class, |
47
|
|
|
'session' => services\Session::class |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
// Modules |
51
|
15 |
|
$this->setModules([ |
52
|
15 |
|
'cp' => cp\Cp::class |
53
|
|
|
|
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
// Template variables |
57
|
15 |
|
Event::on( |
58
|
15 |
|
CraftVariable::class, |
59
|
15 |
|
CraftVariable::EVENT_INIT, |
60
|
|
|
function (Event $event) { |
61
|
|
|
/** @var CraftVariable $variable */ |
62
|
|
|
$variable = $event->sender; |
63
|
|
|
$variable->set('patron', self::getInstance()); |
64
|
15 |
|
} |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// CP routes |
68
|
15 |
|
Event::on( |
69
|
15 |
|
UrlManager::class, |
70
|
15 |
|
UrlManager::EVENT_REGISTER_CP_URL_RULES, |
71
|
15 |
|
[self::class, 'onRegisterCpUrlRules'] |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
// Register our site routes |
75
|
15 |
|
Event::on( |
76
|
15 |
|
UrlManager::class, |
77
|
15 |
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
78
|
|
|
function (RegisterUrlRulesEvent $event) { |
79
|
|
|
if ($callbackUrlRule = $this->getSettings()->getCallbackUrlRule()) { |
80
|
|
|
$event->rules = array_merge( |
81
|
|
|
$event->rules, |
82
|
|
|
$callbackUrlRule |
83
|
|
|
); |
84
|
|
|
} |
85
|
15 |
|
} |
86
|
|
|
); |
87
|
15 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function getCpNavItem() |
93
|
|
|
{ |
94
|
|
|
return array_merge( |
95
|
|
|
parent::getCpNavItem(), |
96
|
|
|
[ |
97
|
|
|
'subnav' => [ |
98
|
|
|
'patron.providers' => [ |
99
|
|
|
'label' => Craft::t('patron', 'Providers'), |
100
|
|
|
'url' => 'patron/providers', |
101
|
|
|
], |
102
|
|
|
'patron.settings' => [ |
103
|
|
|
'label' => Craft::t('patron', 'Settings'), |
104
|
|
|
'url' => 'patron/settings', |
105
|
|
|
] |
106
|
|
|
] |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
* @return SettingsModel |
114
|
|
|
*/ |
115
|
|
|
public function createSettingsModel() |
116
|
|
|
{ |
117
|
|
|
return new SettingsModel(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
* @throws \yii\base\ExitException |
123
|
|
|
*/ |
124
|
|
|
public function getSettingsResponse() |
125
|
|
|
{ |
126
|
|
|
Craft::$app->getResponse()->redirect( |
127
|
|
|
UrlHelper::cpUrl('patron/settings') |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
Craft::$app->end(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/******************************************* |
134
|
|
|
* SERVICES |
135
|
|
|
*******************************************/ |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
139
|
|
|
* @return services\Providers |
140
|
|
|
*/ |
141
|
3 |
|
public function getProviders(): services\Providers |
142
|
|
|
{ |
143
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
144
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
145
|
3 |
|
return $this->get('providers'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
150
|
|
|
* @return services\Tokens |
151
|
|
|
*/ |
152
|
3 |
|
public function getTokens(): services\Tokens |
153
|
|
|
{ |
154
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
155
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
156
|
3 |
|
return $this->get('tokens'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
161
|
|
|
* @return services\ManageProviders |
162
|
|
|
*/ |
163
|
3 |
|
public function manageProviders(): services\ManageProviders |
164
|
|
|
{ |
165
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
166
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
167
|
3 |
|
return $this->get('manageProviders'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
172
|
|
|
* @return services\ManageTokens |
173
|
|
|
*/ |
174
|
3 |
|
public function manageTokens(): services\ManageTokens |
175
|
|
|
{ |
176
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
177
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
178
|
3 |
|
return $this->get('manageTokens'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
183
|
|
|
* @return services\Session |
184
|
|
|
*/ |
185
|
3 |
|
public function getSession(): services\Session |
186
|
|
|
{ |
187
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
188
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
189
|
3 |
|
return $this->get('session'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/******************************************* |
194
|
|
|
* MODULES |
195
|
|
|
*******************************************/ |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @noinspection PhpDocMissingThrowsInspection |
199
|
|
|
* @return cp\Cp |
200
|
|
|
*/ |
201
|
|
|
public function getCp(): cp\Cp |
202
|
|
|
{ |
203
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
204
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
205
|
|
|
return $this->getModule('cp'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/******************************************* |
209
|
|
|
* EVENTS |
210
|
|
|
*******************************************/ |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param RegisterUrlRulesEvent $event |
214
|
|
|
*/ |
215
|
|
|
public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
216
|
|
|
{ |
217
|
|
|
$event->rules = array_merge( |
218
|
|
|
$event->rules, |
219
|
|
|
[ |
220
|
|
|
// SETTINGS |
221
|
|
|
'patron/settings' => 'patron/cp/view/settings/index', |
222
|
|
|
|
223
|
|
|
'patron' => 'patron/cp/view/general/index', |
224
|
|
|
'patron/providers' => 'patron/cp/view/providers/index', |
225
|
|
|
'patron/providers/new' => 'patron/cp/view/providers/upsert', |
226
|
|
|
'patron/providers/<identifier:\d+>' => 'patron/cp/view/providers/upsert', |
227
|
|
|
'patron/providers/<identifier:\d+>/tokens' => 'patron/cp/view/providers/tokens', |
228
|
|
|
] |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/******************************************* |
234
|
|
|
* LOGGING |
235
|
|
|
*******************************************/ |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Logs an informative message. |
239
|
|
|
* |
240
|
|
|
* @param $message |
241
|
|
|
* @param string $category |
242
|
|
|
*/ |
243
|
|
|
public static function info($message, $category = 'patron') |
244
|
|
|
{ |
245
|
|
|
Craft::info($message, $category); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Logs a warning message. |
250
|
|
|
* |
251
|
|
|
* @param $message |
252
|
|
|
* @param string $category |
253
|
|
|
*/ |
254
|
|
|
public static function warning($message, $category = 'patron') |
255
|
|
|
{ |
256
|
|
|
Craft::warning($message, $category); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Logs an error message. |
261
|
|
|
* |
262
|
|
|
* @param $message |
263
|
|
|
* @param string $category |
264
|
|
|
*/ |
265
|
|
|
public static function error($message, $category = 'patron') |
266
|
|
|
{ |
267
|
|
|
Craft::error($message, $category); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|