2022-06-03 03:42:53 +08:00
|
|
|
<?php
|
|
|
|
|
namespace Nexus\Plugin;
|
|
|
|
|
|
|
|
|
|
class Plugin
|
|
|
|
|
{
|
|
|
|
|
private static mixed $providers = null;
|
|
|
|
|
|
2023-07-04 01:00:02 +08:00
|
|
|
private static array $plugins = [];
|
|
|
|
|
|
2022-06-03 03:42:53 +08:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->loadProviders();
|
2022-06-08 14:15:59 +08:00
|
|
|
$this->bootPlugins();
|
2022-06-03 03:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-22 21:07:06 +08:00
|
|
|
public static function enabled($name): bool
|
2022-06-03 03:42:53 +08:00
|
|
|
{
|
|
|
|
|
return !empty(self::$providers[$name]['providers']);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 01:00:02 +08:00
|
|
|
public static function getById($id) :BasePlugin|null
|
|
|
|
|
{
|
|
|
|
|
return self::$plugins[$id] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 03:42:53 +08:00
|
|
|
public function getMainClass($name)
|
|
|
|
|
{
|
|
|
|
|
if (isset(self::$providers[$name]['providers'][0])) {
|
|
|
|
|
$className = self::$providers[$name]['providers'][0];
|
2022-06-08 14:15:59 +08:00
|
|
|
$className = str_replace('ServiceProvider', 'Repository', $className);
|
2022-06-03 03:42:53 +08:00
|
|
|
if (class_exists($className)) {
|
|
|
|
|
return new $className;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function bootPlugins()
|
|
|
|
|
{
|
|
|
|
|
foreach (self::$providers as $name => $providers) {
|
|
|
|
|
$provider = $providers['providers'][0];
|
|
|
|
|
$parts = explode('\\', $provider);
|
|
|
|
|
if ($parts[0] == 'NexusPlugin') {
|
2022-06-08 14:15:59 +08:00
|
|
|
$className = str_replace('ServiceProvider', 'Repository', $provider);
|
2022-06-08 15:23:34 +08:00
|
|
|
if (class_exists($className)) {
|
2022-08-11 01:04:38 +08:00
|
|
|
$constantName = "$className::COMPATIBLE_VERSION";
|
|
|
|
|
if (defined($constantName) && version_compare(VERSION_NUMBER, constant($constantName), '<')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-07-04 01:00:02 +08:00
|
|
|
$plugin = new $className;
|
|
|
|
|
call_user_func([$plugin, 'boot']);
|
|
|
|
|
$pluginIdName = "$className::ID";
|
|
|
|
|
if (defined($pluginIdName)) {
|
|
|
|
|
self::$plugins[constant($pluginIdName)] = $plugin;
|
|
|
|
|
}
|
2022-06-08 15:23:34 +08:00
|
|
|
}
|
2022-06-03 03:42:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function loadProviders()
|
|
|
|
|
{
|
|
|
|
|
if (is_null(self::$providers)) {
|
|
|
|
|
$path = ROOT_PATH . 'bootstrap/cache/packages.php';
|
|
|
|
|
if (file_exists($path)) {
|
|
|
|
|
self::$providers = require $path;
|
|
|
|
|
} else {
|
|
|
|
|
self::$providers = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-07-04 01:00:02 +08:00
|
|
|
|
|
|
|
|
|
2022-06-03 03:42:53 +08:00
|
|
|
}
|