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