extension can have version

This commit is contained in:
maxpozdeev 2022-09-19 15:51:23 +03:00
parent 81d32e0a1b
commit 767c669435
4 changed files with 54 additions and 31 deletions

View file

@ -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) {

View file

@ -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']);
}

View file

@ -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;
}

View file

@ -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 .= "<a href='#' data-settings-link='ext-deactivate' data-ext='". htmlspecialchars($ext). "'>". __('set_deactivate', true). '</a>';
$instance = MTTExtensionLoader::extensionInstance($ext);