From 767c669435393533e7f5291bd5b81c90134b3681 Mon Sep 17 00:00:00 2001 From: maxpozdeev Date: Mon, 19 Sep 2022 15:51:23 +0300 Subject: [PATCH] extension can have version --- src/includes/api/ExtSettingsController.php | 6 +-- src/includes/class.lang.php | 25 ++++++++--- src/includes/classes.php | 52 +++++++++++++--------- src/settings.php | 2 +- 4 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/includes/api/ExtSettingsController.php b/src/includes/api/ExtSettingsController.php index 404b18e..ef3eb21 100644 --- a/src/includes/api/ExtSettingsController.php +++ b/src/includes/api/ExtSettingsController.php @@ -7,7 +7,7 @@ class ExtSettingsController extends ApiController { * @return void * @throws Exception */ - function get($ext) + function get(string $ext) { checkWriteAccess(); @@ -52,7 +52,7 @@ EOD; * @return void * @throws Exception */ - function put($ext) + function put(string $ext) { checkWriteAccess(); @@ -70,7 +70,7 @@ EOD; $this->response->data = $a; } - private function extInstance($ext): ?MTTExtensionSettingsInterface + private function extInstance(string $ext): ?MTTExtensionSettingsInterface { $instance = MTTExtensionLoader::extensionInstance($ext); if (!$instance) { diff --git a/src/includes/class.lang.php b/src/includes/class.lang.php index df00818..ae2ac2d 100644 --- a/src/includes/class.lang.php +++ b/src/includes/class.lang.php @@ -18,7 +18,7 @@ class Lang protected $default = 'en'; protected $strings; - public static function instance() + public static function instance(): Lang { if (!isset(self::$instance)) { $c = __CLASS__; @@ -162,23 +162,36 @@ class Lang return $this->code; } - public function loadExtensionLang(string $ext) + public function getExtensionLang(string $ext): ?array { $langDir = MTT_EXT. $ext. '/lang/'; if (!is_dir($langDir)) { - return; + return null; } if (!file_exists($langDir. 'en.json')) { - return; + return null; } - if (file_exists($langDir. $this->code. '.json')) { + $lang = []; + if ($this->code != 'en') { + if (!file_exists($langDir. $this->code. '.json')) { + return null; + } $langStr = file_get_contents($langDir. $this->code. '.json'); $lang = json_decode($langStr, true) ?? []; } $defStr = file_get_contents($langDir. 'en.json'); $def = json_decode($defStr, true) ?? []; - $lang = array_replace($def, $lang); + return $lang; + } + + public function loadExtensionLang(string $ext) + { + $lang = $this->getExtensionLang($ext); + if (!$lang) { + return; + } + if (isset($lang['_header'])) { unset($lang['_header']); } diff --git a/src/includes/classes.php b/src/includes/classes.php index 68160a4..ce6be4c 100644 --- a/src/includes/classes.php +++ b/src/includes/classes.php @@ -76,6 +76,28 @@ abstract class MTTExtension const bundleId = ''; const name = ''; abstract function init(); + + public static function extMetaInfo(string $ext): ?array + { + $file = MTT_EXT. $ext. '/extension.json'; + if ( file_exists($file) + && false !== ($json = file_get_contents($file)) + && ($meta = json_decode($json, true)) + && is_array($meta) ) + { + // check mandatory keys + if (!isset($meta['bundleId']) || !isset($meta['name']) || !isset($meta['version']) || !isset($meta['description'])) { + return null; + } + if (!is_string($meta['bundleId']) || !is_string($meta['name']) || !is_string($meta['version']) || !is_string($meta['description'])) { + return null; + } + return $meta; + } + error_log("$ext/extension.json is missing or invalid"); + return null; + } + } interface MTTHttpApiExtender @@ -155,31 +177,19 @@ class MTTExtensionLoader $files = array_diff(scandir(MTT_EXT) ?? [], ['.', '..']); foreach ($files as $ext) { if ( !is_dir(MTT_EXT. $ext) - || !file_exists(MTT_EXT. $ext. '/loader.php') - || !file_exists(MTT_EXT. $ext. '/extension.json') ) { + || !file_exists(MTT_EXT. $ext. '/loader.php') ) { continue; } - $jsonData = file_get_contents(MTT_EXT. $ext. '/extension.json'); - if ($jsonData === false) { + + $meta = MTTExtension::extMetaInfo($ext); + if (!$meta) { continue; } - $meta = json_decode($jsonData, true); - if (!is_array($meta) || !isset($meta['bundleId']) || !isset($meta['name']) || !isset($meta['description'])) { - continue; - } - if (!is_string($meta['bundleId']) || !is_string($meta['name']) || !is_string($meta['description'])) { - continue; - } - if ( $lang->langCode() != 'en' && is_dir(MTT_EXT. $ext. '/lang') ) { - $lf = MTT_EXT. $ext. '/lang/'. $lang->langCode(). '.json'; - if (file_exists($lf)) { - $jsonText = file_get_contents($lf) ?? ''; - $json = json_decode($jsonText, true) ?? []; - $lt = $json['ext.'.$ext.'.name'] ?? null; - if ($lt !== null) { - $meta['name'] = $lt; - } - } + + if ( $lang->langCode() != 'en' + && null !== ($translation = $lang->getExtensionLang($ext)) + && null !== ($locName = $translation['ext.'.$ext.'.name'] ?? null) ) { + $meta['name'] = $locName; } $a[$ext] = $meta; } diff --git a/src/settings.php b/src/settings.php index 902ba8b..6610557 100644 --- a/src/settings.php +++ b/src/settings.php @@ -194,7 +194,7 @@ function listExtensions() $activatedExts = Config::get('extensions'); if (!is_array($activatedExts)) $activatedExts = []; foreach ($extBundles as $ext => $meta) { - $out = htmlspecialchars($meta['name']). ' '; + $out = htmlspecialchars($meta['name']. ' v'. $meta['version']). ' '; if (in_array($ext, $activatedExts)) { $out .= "". __('set_deactivate', true). ''; $instance = MTTExtensionLoader::extensionInstance($ext);