diff options
| author | winter | 2025-03-26 21:59:44 +0000 |
|---|---|---|
| committer | winter | 2025-03-26 21:59:44 +0000 |
| commit | a05e8a564009ae7354f0b1ede5a69ed6d3c6e650 (patch) | |
| tree | 4c28cf9ccee0d7db21cec43a5d2f1979098f2932 /Digitigrade | |
| parent | f4e9161ddbb3d47f35286df1f9020bd2d48949f2 (diff) | |
add plugin support
Diffstat (limited to 'Digitigrade')
| -rw-r--r-- | Digitigrade/PluginLoader.php | 46 | ||||
| -rw-r--r-- | Digitigrade/PluginMetadata.php | 17 |
2 files changed, 63 insertions, 0 deletions
diff --git a/Digitigrade/PluginLoader.php b/Digitigrade/PluginLoader.php new file mode 100644 index 0000000..094f1b6 --- /dev/null +++ b/Digitigrade/PluginLoader.php @@ -0,0 +1,46 @@ +<?php +namespace Digitigrade; + +abstract class PluginLoader { + public static array $loadedPlugins = []; + + public static function load(string $path): bool { + try { + $phar = @new \Phar($path); + } catch (\Exception $e) { + Logger::getInstance()->warning("$path doesn't appear to be a valid phar file!"); + } + $metadata = $phar->getMetadata(); + if (!($metadata instanceof PluginMetadata)) { + Logger::getInstance()->warning("$path doesn't appear to be a valid plugin!"); + return false; + } + self::$loadedPlugins[$path] = $metadata; + + // setup autoloads + spl_autoload_register(function (string $className) use ($path, $metadata, $phar) { + /** @var PluginMetadata $metadata */ + /** @var \Phar $phar */ + if (str_starts_with($className, $metadata->autoloadPrefix)) { + require "phar://$path/" + . $metadata->autoloadDir + . str_replace('\\', '/', + substr_replace($className, '', 0, strlen($metadata->autoloadPrefix))) + . '.php'; + } + }); + + // run entrypoint if defined + if (isset($metadata->entrypoint)) { + require "phar://$path/$metadata->entrypoint"; + } + + return true; + } + + public static function loadDir(string $directory) { + foreach (glob("$directory/*.phar") as $file) { + self::load($file); + } + } +}
\ No newline at end of file diff --git a/Digitigrade/PluginMetadata.php b/Digitigrade/PluginMetadata.php new file mode 100644 index 0000000..296295f --- /dev/null +++ b/Digitigrade/PluginMetadata.php @@ -0,0 +1,17 @@ +<?php +namespace Digitigrade; + +class PluginMetadata { + // basic info + public string $name; + public string $author; + public ?string $homepage; + public string $version; + + // autoload info + public string $autoloadPrefix; + public string $autoloadDir; + + // entrypoint (ran when the plugin is loaded) + public ?string $entrypoint; +}
\ No newline at end of file |
