diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Digitigrade/PluginLoader.php | 46 | ||||
| -rw-r--r-- | Digitigrade/PluginMetadata.php | 17 | ||||
| -rw-r--r-- | index.php | 2 | ||||
| -rw-r--r-- | plugins/.gitkeep | 0 |
5 files changed, 67 insertions, 1 deletions
@@ -1,4 +1,5 @@ vendor/ config.ini upload/* -VERSION
\ No newline at end of file +VERSION +plugins/*.phar
\ No newline at end of file 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 @@ -11,6 +11,8 @@ function require_all_glob(string $pattern) { require_all_glob('routes/*.php'); require_all_glob('misc/*.php'); +Digitigrade\PluginLoader::loadDir('plugins'); + require_once __DIR__ . '/policies.conf.php'; diff --git a/plugins/.gitkeep b/plugins/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/plugins/.gitkeep |
