-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManager.php
More file actions
272 lines (216 loc) · 7.84 KB
/
Manager.php
File metadata and controls
272 lines (216 loc) · 7.84 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
<?php
namespace Bethropolis\PluginSystem;
use Bethropolis\PluginSystem\System;
use Bethropolis\PluginSystem\Error;
use Bethropolis\PluginSystem\LifeCycle;
class Manager
{
private static $pluginsDir;
private static $configFile;
private static $config;
private static $lifeCycle;
/**
* @param string|null $dir The plugins directory.
* @param string|null $configFile Path to store the plugins config.json.
*/
public static function initialize($dir = null, $configFile = null)
{
$pluginsDir = $dir ?? System::getPluginsDir();
self::setPluginsDir($pluginsDir);
// Default to a folder in the host app if not provided, avoiding __DIR__
self::$configFile = $configFile ?? sys_get_temp_dir() . '/plugin_system_config.json';
System::setConfigFile(self::$configFile); // Sync config path with System
self::$config = self::loadConfig();
self::$lifeCycle = new LifeCycle();
}
public static function setPluginsDir($dir)
{
self::$pluginsDir = $dir;
}
public static function installPlugin($pluginUrl)
{
// Check if the plugins directory is writable
if (!is_writable(self::$pluginsDir)) {
return;
}
// Get the plugin file name and create temporary file and directory paths
$pluginFileName = basename($pluginUrl);
$tempDir = sys_get_temp_dir();
$tempFilePath = $tempDir . '/' . $pluginFileName;
$extractedDirPath = $tempDir . '/' . pathinfo($pluginFileName, PATHINFO_FILENAME);
// Copy the plugin file to the temporary directory
if (!copy($pluginUrl, $tempFilePath)) {
return;
}
// Extract the plugin file
$zip = new \ZipArchive();
if ($zip->open($tempFilePath) === true) {
if (!$zip->extractTo($extractedDirPath)) {
return;
}
$zip->close();
} else {
return;
}
// Get the plugin directory path
$pluginDirPath = self::$pluginsDir . '/' . basename($extractedDirPath);
// Rename the extracted directory to the plugin directory if it doesn't already exist
if (is_dir($pluginDirPath)) {
return;
}
if (!rename($extractedDirPath, $pluginDirPath)) {
return;
}
// Register the plugin
$pluginName = basename($pluginDirPath);
self::registerPlugin($pluginName);
self::$lifeCycle->onInstallation($pluginName);
// Clean up the temporary file and register the plugin
unlink($tempFilePath);
return true;
}
public static function uninstallPlugin($pluginName)
{
$pluginNamespace = self::getpluginName($pluginName);
if (!self::pluginExists($pluginName)) {
Error::handleException(new \Exception("Plugin does not exist."));
return;
}
self::deactivatePlugin($pluginNamespace); // hard coding it for now
if (self::isPluginActive($pluginName)) {
Error::handleException(new \Exception("Cannot uninstall an active plugin. Deactivate it first."));
return;
}
$pluginDir = self::$pluginsDir . '/' . $pluginName;
if (!is_dir($pluginDir)) {
Error::handleException(new \Exception("Plugin directory not found."));
return;
}
if (!self::deleteDirectory($pluginDir)) {
Error::handleException(new \Exception("Unable to remove the plugin directory."));
return;
}
self::unregisterPlugin($pluginNamespace);
self::$lifeCycle->onUninstallation($pluginName);
self::saveConfig();
return true;
}
private static function deleteDirectory($dir)
{
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!self::deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
public static function registerPlugin($pluginName)
{
$pluginName = self::getpluginName($pluginName);
self::$config['plugins'][$pluginName] = [];
self::$config['activated_plugins'][$pluginName] = true;
self::saveConfig();
}
public static function unregisterPlugin($pluginName)
{
$pluginName = self::getpluginName($pluginName);
unset(self::$config['plugins'][$pluginName]);
unset(self::$config['activated_plugins'][$pluginName]);
self::saveConfig();
}
public static function activatePlugin($pluginName)
{
$pluginName = self::getpluginName($pluginName);
self::$config['activated_plugins'][$pluginName] = true;
return self::saveConfig();
}
public static function deactivatePlugin($pluginName)
{
$pluginName = self::getpluginName($pluginName);
self::$config['activated_plugins'][$pluginName] = false;
return self::saveConfig();
}
// toggle function
public static function togglePlugin($pluginName)
{
$pluginName = self::getpluginName($pluginName);
self::$config['activated_plugins'][$pluginName] = !self::$config['activated_plugins'][$pluginName];
return self::saveConfig();
}
public static function pluginExists($pluginName)
{
$pluginName = self::getpluginName($pluginName);
return isset(self::$config['plugins'][$pluginName]);
}
public static function isPluginActive($pluginName)
{
$pluginName = self::getpluginName($pluginName);
return isset(self::$config['activated_plugins'][$pluginName]) && self::$config['activated_plugins'][$pluginName];
}
public static function getPluginMetadata($pluginName)
{
$pluginName = self::getpluginName($pluginName);
return self::$config['plugins'][$pluginName] ?? null;
}
private static function convertNameToNamespace($pluginName)
{
return stripslashes(strtolower(__NAMESPACE__ . '\\' . $pluginName . "Plugin\\Load"));
}
private static function getpluginName($pluginName)
{
$pluginName = strtolower($pluginName);
if (isset(self::$config['plugins'][$pluginName])) {
return $pluginName;
}
return self::convertNameToNamespace($pluginName);
}
public static function updatePlugin($pluginName, $pluginUrl)
{
if (!self::pluginExists($pluginName)) {
throw new \Exception("Plugin does not exist.");
}
self::uninstallPlugin($pluginName);
self::installPlugin($pluginUrl);
}
private static function loadConfig()
{
if (file_exists(self::$configFile)) {
$config = json_decode(file_get_contents(self::$configFile), true);
} else {
$config = [];
}
// Check if the configuration is empty or doesn't exist
if (empty($config)) {
$config = self::initializeConfig();
self::saveConfig($config);
}
return $config;
}
private static function initializeConfig()
{
$plugins = System::getPlugins();
$plugins = array_map(function ($plugin) {
return stripslashes(strtolower($plugin));
}, $plugins);
$activatedPlugins = array_fill_keys($plugins, true);
return [
'plugins' => array_fill_keys($plugins, []),
'activated_plugins' => $activatedPlugins
];
}
private static function saveConfig($config = null)
{
$config = $config ?? self::$config;
file_put_contents(self::$configFile, json_encode($config, JSON_PRETTY_PRINT));
return true;
}
}