Files
nexusphp/nexus/Plugin/Plugin.php

62 lines
1.7 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;
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']);
}
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;
}
2022-06-08 15:23:34 +08:00
call_user_func([new $className, 'boot']);
}
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 = [];
}
}
}
}