Bugfix: Config $sAdditionalFileName never worked

Changed: Plugin Config now stored in JSON
This commit is contained in:
the-djmaze 2022-11-09 14:26:17 +01:00
parent 2f0e410b80
commit fbabf0a283
2 changed files with 51 additions and 30 deletions

View file

@ -34,10 +34,10 @@ abstract class AbstractConfig implements \JsonSerializable
$this->sFile = \APP_PRIVATE_DATA.'configs/'.\trim($sFileName); $this->sFile = \APP_PRIVATE_DATA.'configs/'.\trim($sFileName);
$sAdditionalFileName = \trim($sAdditionalFileName); $sAdditionalFileName = \trim($sAdditionalFileName);
if (\strlen($sAdditionalFileName)) { if ($sAdditionalFileName) {
$sAdditionalFileName = \APP_PRIVATE_DATA.'configs/'.$sAdditionalFileName; $sAdditionalFileName = \APP_PRIVATE_DATA.'configs/'.$sAdditionalFileName;
if (\file_exists($this->sAdditionalFile)) { if (\file_exists($this->sAdditionalFile)) {
$this->sAdditionalFile = $this->sAdditionalFile; $this->sAdditionalFile = $sAdditionalFileName;
} }
} }
@ -58,7 +58,16 @@ abstract class AbstractConfig implements \JsonSerializable
#[\ReturnTypeWillChange] #[\ReturnTypeWillChange]
public function jsonSerialize() public function jsonSerialize()
{ {
return $this->aData; $aData = [];
foreach ($this->aData as $sSectionKey => $aSectionValue) {
if (\is_array($aSectionValue)) {
$aData[$sSectionKey] = [];
foreach ($aSectionValue as $sParamKey => $mParamValue) {
$aData[$sSectionKey][$sParamKey] = $mParamValue[0];
}
}
}
return $aData;
} }
/** /**
@ -179,14 +188,22 @@ abstract class AbstractConfig implements \JsonSerializable
public function Load() : bool public function Load() : bool
{ {
if (\file_exists($this->sFile) && \is_readable($this->sFile)) $sFile = $this->sFile;
if (!\file_exists($sFile) && \str_ends_with($sFile, '.json')) {
$sFile = \str_replace('.json', '.ini', $sFile);
}
if (\file_exists($sFile) && \is_readable($sFile))
{ {
if ($this->loadDataFromCache()) if ($this->loadDataFromCache())
{ {
return true; return true;
} }
$aData = \parse_ini_file($this->sFile, true); if (\str_ends_with($sFile, '.json')) {
$aData = \json_decode(\file_get_contents($sFile), true);
} else {
$aData = \parse_ini_file($sFile, true);
}
if ($aData && \count($aData)) if ($aData && \count($aData))
{ {
foreach ($aData as $sSectionKey => $aSectionValue) foreach ($aData as $sSectionKey => $aSectionValue)
@ -204,10 +221,14 @@ abstract class AbstractConfig implements \JsonSerializable
if (\file_exists($this->sAdditionalFile) && \is_readable($this->sAdditionalFile)) if (\file_exists($this->sAdditionalFile) && \is_readable($this->sAdditionalFile))
{ {
$aSubData = \parse_ini_file($this->sAdditionalFile, true); if (\str_ends_with($this->sAdditionalFile, '.json')) {
if ($aSubData && \count($aSubData)) $aData = \json_decode(\file_get_contents($this->sAdditionalFile), true);
} else {
$aData = \parse_ini_file($this->sAdditionalFile, true);
}
if ($aData && \count($aData))
{ {
foreach ($aSubData as $sSectionKey => $aSectionValue) foreach ($aData as $sSectionKey => $aSectionValue)
{ {
if (\is_array($aSectionValue)) if (\is_array($aSectionValue))
{ {
@ -219,7 +240,7 @@ abstract class AbstractConfig implements \JsonSerializable
} }
} }
unset($aSubData); unset($aData);
} }
$this->storeDataToCache(); $this->storeDataToCache();
@ -238,38 +259,38 @@ abstract class AbstractConfig implements \JsonSerializable
return false; return false;
} }
if (\str_ends_with($this->sFile, '.json')) {
$this->clearCache();
\RainLoop\Utils::saveFile($this->sFile, \json_encode($this, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
// Remove old .ini file
$sFile = \str_replace('.json', '.ini', $this->sFile);
\file_exists($sFile) && \unlink($sFile);
return true;
}
$sNewLine = "\n"; $sNewLine = "\n";
$aResultLines = array(); $aResultLines = array();
foreach ($this->aData as $sSectionKey => $aSectionValue) foreach ($this->aData as $sSectionKey => $aSectionValue) {
{ if (\is_array($aSectionValue)) {
if (\is_array($aSectionValue))
{
$aResultLines[] = ''; $aResultLines[] = '';
$aResultLines[] = '['.$sSectionKey.']'; $aResultLines[] = '['.$sSectionKey.']';
$bFirst = true; $bFirst = true;
foreach ($aSectionValue as $sParamKey => $mParamValue) foreach ($aSectionValue as $sParamKey => $mParamValue) {
{ if (\is_array($mParamValue)) {
if (\is_array($mParamValue)) // Add comments
{ if (!empty($mParamValue[1])) {
if (!empty($mParamValue[1])) if (!$bFirst) {
{
$sDesk = \str_replace("\r", '', $mParamValue[1]);
$aDesk = \explode("\n", $sDesk);
$aDesk = \array_map(function ($sLine) {
return '; '.$sLine;
}, $aDesk);
if (!$bFirst)
{
$aResultLines[] = ''; $aResultLines[] = '';
} }
foreach (\explode("\n", \str_replace("\r", '', $mParamValue[1])) as $sLine) {
$aResultLines[] = \implode($sNewLine, $aDesk); $aResultLines[] = '; ' . $sLine;
}
} }
// Add value
$bFirst = false; $bFirst = false;
$sValue = '""'; $sValue = '""';

View file

@ -28,7 +28,7 @@ class Plugin extends \RainLoop\Config\AbstractConfig
} }
} }
parent::__construct('plugin-'.$sPluginName.'.ini', '; SnappyMail plugin ('.$sPluginName.')'); parent::__construct('plugin-'.$sPluginName.'.json');
} }
protected function defaultValues() : array protected function defaultValues() : array