aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/PluginLoader.php
diff options
context:
space:
mode:
authorwinter2025-03-26 21:59:44 +0000
committerwinter2025-03-26 21:59:44 +0000
commita05e8a564009ae7354f0b1ede5a69ed6d3c6e650 (patch)
tree4c28cf9ccee0d7db21cec43a5d2f1979098f2932 /Digitigrade/PluginLoader.php
parentf4e9161ddbb3d47f35286df1f9020bd2d48949f2 (diff)
add plugin support
Diffstat (limited to 'Digitigrade/PluginLoader.php')
-rw-r--r--Digitigrade/PluginLoader.php46
1 files changed, 46 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