Files
nexusphp/nexus/Plugin/Plugin.php

91 lines
2.4 KiB
PHP
Raw Normal View History

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 = [];
2024-08-24 06:49:55 +08:00
// public function __construct()
// {
// $this->start();
// }
public function start(): void
2022-06-03 03:42:53 +08:00
{
$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']);
}
2024-11-17 02:44:41 +08:00
public static function listEnabled(): array
{
$result = [];
//plugins are more exactly
foreach (self::$plugins as $id => $plugin) {
$result[$id] = 1;
}
return $result;
}
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;
$pluginIdName = "$className::ID";
if (defined($pluginIdName)) {
self::$plugins[constant($pluginIdName)] = $plugin;
}
2023-07-06 03:23:28 +08:00
call_user_func([$plugin, 'boot']);
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
}