Files
nexusphp/nexus/Plugin/Plugin.php

58 lines
1.5 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
}
public function enabled($name): bool
{
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)) {
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 = [];
}
}
}
}