diff options
author | Andreas Gohr <andi@splitbrain.org> | 2024-02-20 19:23:43 +0100 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2024-12-04 10:51:13 +0100 |
commit | a1e045f72e3be1dcea57282e31aeb0835ab73e23 (patch) | |
tree | ac8f146114cbf644a2e5eaf95ed1d9ae55623a52 | |
parent | cf2dcf1b9ac1f331d4667a6c82d326f1a3e5d4c7 (diff) | |
download | dokuwiki-a1e045f72e3be1dcea57282e31aeb0835ab73e23.tar.gz dokuwiki-a1e045f72e3be1dcea57282e31aeb0835ab73e23.zip |
ExtensionManager: allow initialization using an ID only
This is a more convenient method
-rw-r--r-- | lib/plugins/extension/Extension.php | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/lib/plugins/extension/Extension.php b/lib/plugins/extension/Extension.php index d8be00e01..934880802 100644 --- a/lib/plugins/extension/Extension.php +++ b/lib/plugins/extension/Extension.php @@ -39,6 +39,27 @@ class Extension } /** + * Initializes an extension from an id + * + * @param string $id The id of the extension + * @return Extension + */ + public static function createFromId($id) + { + $extension = new self(); + $extension->initFromId($id); + return $extension; + } + + protected function initFromId($id) + { + [$type, $base] = $this->idToTypeBase($id); + $this->type = $type; + $this->base = $base; + $this->readLocalInfo(); + } + + /** * Initializes an extension from a directory * * The given directory might be the one where the extension has already been installed to @@ -103,14 +124,7 @@ class Extension { if (!isset($data['plugin'])) throw new RuntimeException('Invalid remote data'); - [$type, $base] = sexplode(':', $data['plugin'], 2); - if ($base === null) { - $base = $type; - $type = self::TYPE_PLUGIN; - } else { - $type = self::TYPE_TEMPLATE; - } - + [$type, $base] = $this->idToTypeBase($data['plugin']); $this->remoteInfo = $data; $this->type = $type; $this->base = $base; @@ -493,7 +507,7 @@ class Extension */ protected function readLocalInfo() { - if (!$this->currentDir) return; + if (!$this->getCurrentDir()) return; $file = $this->currentDir . '/' . $this->type . '.info.txt'; if (!is_readable($file)) return; $this->localInfo = confToHash($file, true); @@ -539,4 +553,28 @@ class Extension } // endregion + + // region utilities + + /** + * Convert an extension id to a type and base + * + * @param string $id + * @return array [type, base] + */ + protected function idToTypeBase($id) + { + [$type, $base] = sexplode(':', $id, 2); + if ($base === null) { + $base = $type; + $type = self::TYPE_PLUGIN; + } elseif ($type === self::TYPE_TEMPLATE) { + $type = self::TYPE_TEMPLATE; + } else { + throw new RuntimeException('Invalid extension id: ' . $id); + } + + return [$type, $base]; + } + // endregion } |