Repository navigation
Expand file tree
/
Copy pathAppConfig.php
More file actions
285 lines (260 loc) · 8.79 KB
/
Copy pathAppConfig.php
File metadata and controls
285 lines (260 loc) · 8.79 KB
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
<?php
#App\GP247\Plugins\ScheduledTasks\AppConfig.php
namespace App\GP247\Plugins\ScheduledTasks;
use App\GP247\Plugins\ScheduledTasks\Models\ExtensionModel;
use App\GP247\Plugins\ScheduledTasks\Support\Routes;
use GP247\Core\ExtensionConfigDefault;
use GP247\Core\Models\AdminConfig;
use GP247\Core\Models\AdminHome;
use GP247\Core\Models\AdminMenu;
use GP247\Core\Models\AdminPermission;
/**
* Install / update / uninstall of the ScheduledTasks plugin.
*
* install() and update() share converge(): it brings any site (fresh, partially
* installed, older version) to the shape this version needs, and is a no-op on a
* healthy one.
*
* @aidlc-unit plugin-scheduled-tasks
* @aidlc-story US-plugin-scheduled-tasks-access-control
*/
class AppConfig extends ExtensionConfigDefault
{
private const MENU_PARENT_KEY = 'ADMIN_SYSTEM';
/**
* Read the plugin manifest.
*/
public function __construct()
{
$config = json_decode((string) file_get_contents(__DIR__.'/gp247.json'), true);
$this->configGroup = $config['configGroup'];
$this->configKey = $config['configKey'];
$this->configCode = $config['configCode'] ?? $this->configKey;
$this->requireCore = $config['requireCore'] ?? [];
$this->requireComposerPackages = $config['requireComposerPackages'] ?? [];
$this->requireGp247Extensions = $config['requireGp247Extensions'] ?? [];
$this->appPath = $this->configGroup . '/' . $this->configKey;
$this->title = trans($this->appPath.'::lang.title');
$this->image = $this->appPath.'/'.$config['image'];
$this->version = $config['version'];
$this->auth = $config['auth'];
$this->link = $config['link'];
}
/**
* The two permissions the plugin adds (view / configure).
*
* WHY two URI shapes: the route middleware matches GET routes exactly
* ("{id}" placeholder included), while ANY::…/* covers the list, the detail
* page and the Livewire write actions authorized as POST on those screens.
*
* @return list<array{slug: string, name: string, http_uri: string}>
*
* @aidlc-unit plugin-scheduled-tasks
* @aidlc-story US-plugin-scheduled-tasks-access-control
*/
public static function permissionDefinitions(): array
{
$base = (defined('GP247_ADMIN_PREFIX') ? GP247_ADMIN_PREFIX : 'gp247_admin') . '/' . Routes::URI;
return [
[
'slug' => 'scheduled-tasks.view',
'name' => 'Scheduled tasks — view',
'http_uri' => 'GET::' . $base . ',GET::' . $base . '/{id}',
],
[
'slug' => 'scheduled-tasks.manage',
'name' => 'Scheduled tasks — configure',
'http_uri' => 'ANY::' . $base . '/*',
],
];
}
/**
* Register the plugin, then converge tables, menu and permissions.
*
* @return array{error: int, msg: string}
*
* @aidlc-unit plugin-scheduled-tasks
* @aidlc-story US-plugin-scheduled-tasks-access-control
*/
public function install()
{
if (AdminConfig::where('key', $this->configKey)->where('group', $this->configGroup)->exists()) {
return ['error' => 1, 'msg' => gp247_language_render('admin.extension.plugin_exist')];
}
try {
AdminConfig::insert([[
'group' => $this->configGroup,
'code' => $this->configCode,
'key' => $this->configKey,
'sort' => 0,
'store_id' => GP247_STORE_ID_GLOBAL,
'value' => self::ON,
'detail' => $this->appPath.'::lang.title',
]]);
$this->converge();
return ['error' => 0, 'msg' => gp247_language_render('admin.extension.install_success')];
} catch (\Throwable $e) {
return ['error' => 1, 'msg' => $e->getMessage()];
}
}
/**
* Run after the 1-click updater replaced the plugin files.
*
* @param string|null $fromVersion Version installed before the update.
* @return array{error: int, msg: string}
*
* @aidlc-unit plugin-scheduled-tasks
* @aidlc-story US-plugin-scheduled-tasks-access-control
*/
public function update(?string $fromVersion = null)
{
try {
$this->converge();
} catch (\Throwable $e) {
return ['error' => 1, 'msg' => $e->getMessage()];
}
return ['error' => 0, 'msg' => ''];
}
/**
* Remove what install() created, including the plugin tables (history and the
* owner's schedule settings — every task then runs as coded).
*
* @return array{error: int, msg: string}
*
* @aidlc-unit plugin-scheduled-tasks
* @aidlc-story US-plugin-scheduled-tasks-access-control
*/
public function uninstall()
{
try {
(new AdminConfig)
->where('key', $this->configKey)
->orWhere('code', $this->configKey.'_config')
->delete();
AdminHome::where('extension', $this->appPath)->delete();
AdminMenu::where('key', $this->configKey)->delete();
AdminPermission::whereIn('slug', array_column(self::permissionDefinitions(), 'slug'))->delete();
app(ExtensionModel::class)->uninstallExtension();
return ['error' => 0, 'msg' => gp247_language_render('admin.extension.uninstall_success')];
} catch (\Throwable $e) {
return ['error' => 1, 'msg' => $e->getMessage()];
}
}
/**
* Enable the plugin.
*
* @return array{error: int, msg: string}
*/
public function enable()
{
(new AdminConfig)->where('group', $this->configGroup)->where('key', $this->configKey)->update(['value' => self::ON]);
return ['error' => 0, 'msg' => gp247_language_render('admin.extension.enable_success')];
}
/**
* Disable the plugin (every schedule then runs exactly as coded).
*
* @return array{error: int, msg: string}
*/
public function disable()
{
(new AdminConfig)->where('group', $this->configGroup)->where('key', $this->configKey)->update(['value' => self::OFF]);
return ['error' => 0, 'msg' => ''];
}
/**
* Site-wide plugin: nothing to set up per store.
*
* @param mixed $storeId
* @return void
*/
public function setupStore($storeId = null)
{
}
/**
* Site-wide plugin: nothing to remove per store.
*
* @param mixed $storeId
* @return void
*/
public function removeStore($storeId = null)
{
}
/**
* No action on the plugin button.
*
* @return void
*/
public function clickApp()
{
}
/**
* Plugin information for the extension screens.
*
* @return array<string, mixed>
*/
public function getInfo()
{
return [
'title' => $this->title,
'key' => $this->configKey,
'code' => $this->configCode,
'image' => $this->image,
'permission' => self::ALLOW,
'version' => $this->version,
'auth' => $this->auth,
'link' => $this->link,
'value' => 0,
'appPath' => $this->appPath,
];
}
/**
* Bring tables, menu entry and permissions to this version's shape, without data loss.
*
* @return void
*/
protected function converge(): void
{
app(ExtensionModel::class)->installExtension();
$this->installMenu();
$this->installPermissions();
}
/**
* Add the menu entry under System once.
*
* @return void
*/
protected function installMenu(): void
{
if (AdminMenu::where('key', $this->configKey)->exists()) {
return;
}
$parent = AdminMenu::where('key', self::MENU_PARENT_KEY)->first();
if (!$parent) {
return;
}
AdminMenu::insert([
'sort' => 90,
'parent_id' => $parent->id,
'title' => $this->appPath.'::lang.title',
'icon' => 'fas fa-clock',
'uri' => 'route_admin::admin_scheduled_tasks.index',
'key' => $this->configKey,
]);
}
/**
* Create or repair the two permissions.
*
* @return void
*/
protected function installPermissions(): void
{
foreach (self::permissionDefinitions() as $definition) {
$permission = AdminPermission::firstOrCreate(
['slug' => $definition['slug']],
['name' => $definition['name'], 'http_uri' => $definition['http_uri']]
);
if ($permission->http_uri !== $definition['http_uri']) {
$permission->update(['http_uri' => $definition['http_uri']]);
}
}
}
}