mirror of
https://github.com/easy-wi/developer.git
synced 2025-02-20 11:23:28 +08:00
Replace custom config parser #889
This commit is contained in:
parent
0ebcdca774
commit
e007e84fec
@ -1088,9 +1088,11 @@ class AppServer {
|
||||
|
||||
private function replaceIni($stored, $replacements) {
|
||||
|
||||
$this->undefinedRequiredVars = $replacements;
|
||||
$iniString = "";
|
||||
if (!$stored) {
|
||||
$stored = array();
|
||||
}
|
||||
|
||||
$iniString = "";
|
||||
$arrayKeys = array_keys($stored);
|
||||
|
||||
if (count($arrayKeys) !== 0) {
|
||||
@ -1112,9 +1114,11 @@ class AppServer {
|
||||
return $iniString;
|
||||
}
|
||||
|
||||
private function replaceYaml($stored, $replacements) {
|
||||
private function replaceArray($stored, $replacements) {
|
||||
|
||||
$this->undefinedRequiredVars = $replacements;
|
||||
if (!$stored) {
|
||||
$stored = array();
|
||||
}
|
||||
|
||||
$replacedArray = $this->replaceArrayValues($stored, $replacements);
|
||||
|
||||
@ -1122,7 +1126,19 @@ class AppServer {
|
||||
$replacedArray[$key] = $value;
|
||||
}
|
||||
|
||||
return Spyc::YAMLDump($replacedArray);
|
||||
return $replacedArray;
|
||||
}
|
||||
|
||||
private function replaceYaml($stored, $replacements) {
|
||||
return Spyc::YAMLDump($this->replaceArray($stored, $replacements));
|
||||
}
|
||||
|
||||
private function replaceJSON($stored, $replacements) {
|
||||
return json_encode($this->replaceArray($stored, $replacements), JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
private function replaceLua($stored, $replacements) {
|
||||
return Lua::arrayToLua($this->replaceArray($stored, $replacements));
|
||||
}
|
||||
|
||||
private function correctProtectedFiles () {
|
||||
@ -1158,17 +1174,12 @@ class AppServer {
|
||||
|
||||
// We have one temp handle for all files to reduce the amount of needed ram
|
||||
$ftpObect->tempHandle = null;
|
||||
$this->undefinedRequiredVars = $values['cvars'];
|
||||
|
||||
//TODO handle each type of file with specific parser
|
||||
if ($values['type'] === 'ini') {
|
||||
|
||||
$parsedConfig = @parse_ini_string($configFileContent, false, INI_SCANNER_RAW);
|
||||
|
||||
if (!$parsedConfig) {
|
||||
$parsedConfig = array();
|
||||
}
|
||||
|
||||
$ftpObect->writeContentToTemp($this->replaceIni($parsedConfig, $values['cvars']));
|
||||
$ftpObect->writeContentToTemp($this->replaceIni(@parse_ini_string($configFileContent, false, INI_SCANNER_RAW), $values['cvars']));
|
||||
|
||||
} else if ($values['type'] === 'yml' or $values['type'] === 'yaml') {
|
||||
|
||||
@ -1178,11 +1189,21 @@ class AppServer {
|
||||
|
||||
$parsedConfig = Spyc::YAMLLoadString($configFileContent);
|
||||
|
||||
if (!$parsedConfig) {
|
||||
$parsedConfig = array();
|
||||
$ftpObect->writeContentToTemp($this->replaceYaml($parsedConfig, $values['cvars']));
|
||||
|
||||
} else if ($values['type'] == 'json') {
|
||||
|
||||
$ftpObect->writeContentToTemp($this->replaceJSON(@parse_ini_string(@json_decode($configFileContent), false, INI_SCANNER_RAW), $values['cvars']));
|
||||
|
||||
} else if ($values['type'] == 'lua') {
|
||||
|
||||
if (!class_exists('Lua')) {
|
||||
include(EASYWIDIR . '/stuff/methods/class_lua.php');
|
||||
}
|
||||
|
||||
$ftpObect->writeContentToTemp($this->replaceYaml($parsedConfig, $values['cvars']));
|
||||
$parsedConfig = Lua::luaToArray($configFileContent);
|
||||
|
||||
$ftpObect->writeContentToTemp($this->replaceLua($parsedConfig, $values['cvars']));
|
||||
|
||||
} else {
|
||||
|
||||
@ -1225,26 +1246,6 @@ class AppServer {
|
||||
|
||||
$ftpObect->writeContentToTemp($cvar . ':' . $value);
|
||||
|
||||
} else if ($values['type'] == 'lua' and preg_match("/^(.*)" . strtolower($cvar) . "[\s+]{0,}\=[\s+]{0,}(.*)[\,]$/", $loweredSingleLine)) {
|
||||
|
||||
$edited = true;
|
||||
|
||||
unset($cvarsNotFound[$cvar]);
|
||||
|
||||
$splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
$ftpObect->writeContentToTemp((isset($splitLine[1])) ? $splitLine[0] . $cvar. ' = ' .$value : $cvar . '=' . $value);
|
||||
|
||||
} else if ($values['type'] == 'json' and preg_match("/^(.*)[\"]" . strtolower($cvar) . "[\s+]{0,}:[\s+]{0,}(.*)[\,]{0,1}$/", $loweredSingleLine)) {
|
||||
|
||||
$edited = true;
|
||||
|
||||
unset($cvarsNotFound[$cvar]);
|
||||
|
||||
$splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
$ftpObect->writeContentToTemp((isset($splitLine[1])) ? $splitLine[0] . $cvar. ' : ' .$value : $cvar . ':' . $value);
|
||||
|
||||
} else if ($values['type'] == 'xml' and @preg_match("/^(.*)\<" . strtolower($cvar) . "\>(.*)\<\/" . strtolower($cvar) . "\>(.*)$/", $loweredSingleLine)) {
|
||||
|
||||
$edited = true;
|
||||
|
115
stuff/methods/class_lua.php
Normal file
115
stuff/methods/class_lua.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* File: class_lua.php.
|
||||
* Author: Ulrich Block
|
||||
* Date: 17.07.16
|
||||
* Contact: <ulrich.block@easy-wi.com>
|
||||
*
|
||||
* This file is part of Easy-WI.
|
||||
*
|
||||
* Easy-WI is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Easy-WI is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Easy-WI. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Diese Datei ist Teil von Easy-WI.
|
||||
*
|
||||
* Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
|
||||
* der GNU General Public License, wie von der Free Software Foundation,
|
||||
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
|
||||
* veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
|
||||
*
|
||||
* Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
|
||||
* OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
|
||||
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
|
||||
* Siehe die GNU General Public License fuer weitere Details.
|
||||
*
|
||||
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
|
||||
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
class Lua {
|
||||
|
||||
public static function luaToArray($string) {
|
||||
$Lua = new Lua;
|
||||
return $Lua->parseBlocks($string);
|
||||
}
|
||||
|
||||
private function parseBlocks($string) {
|
||||
|
||||
$parsedBlocks = array();
|
||||
|
||||
preg_match_all('/([^\s]+)[\s]*=[\s]*[\s]+(\{[\s\S]+?[\}](?:[\r\n\s,]*\})*)/m', $string, $blocks, PREG_SET_ORDER);
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
|
||||
if (isset($block[1]) and isset($block[2])) {
|
||||
|
||||
$parsedBlocks[$block[1]] = $this->parseBlocks($block[2]);
|
||||
|
||||
foreach ($this->parseBlock($block[2], $this->getIndention($block[2])) as $key => $value) {
|
||||
$parsedBlocks[$block[1]][$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $parsedBlocks;
|
||||
}
|
||||
|
||||
private function parseBlock($string, $indention) {
|
||||
|
||||
$parsedBlock = array();
|
||||
|
||||
preg_match_all('/' . $indention . '([^\s]+)[\s]*=[\s]*(?:([\d.]+)|"([\s\S]+?)"),?/', $string, $keyValues, PREG_SET_ORDER);
|
||||
|
||||
foreach($keyValues as $keyValue) {
|
||||
if (isset($keyValue[1]) and (isset($keyValue[2]) or isset($keyValue[3]))) {
|
||||
$parsedBlock[$keyValue[1]] = (isset($keyValue[3])) ? $keyValue[3] : $keyValue[2];
|
||||
}
|
||||
}
|
||||
|
||||
return $parsedBlock;
|
||||
}
|
||||
|
||||
private function getIndention($string) {
|
||||
|
||||
preg_match('/[\r\n]+(\s+)/', $string, $indention);
|
||||
|
||||
return '[\r\n]+' . $indention[1];
|
||||
}
|
||||
|
||||
public static function arrayToLua($array) {
|
||||
$Lua = new Lua;
|
||||
return $Lua->arrayOrValue($array, '');
|
||||
}
|
||||
|
||||
private function arrayOrValue($array, $indention) {
|
||||
|
||||
$newIndention = ' ' . $indention;
|
||||
$luaStringParts = array();
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
|
||||
if (is_array($value)) {
|
||||
$luaStringParts[] = $indention . $key . " =\n" . $indention . "{" . $this->arrayOrValue($value, $newIndention) . "\n$indention}";
|
||||
} else {
|
||||
$luaStringParts[] = $indention . $key . ' = ' . $value;
|
||||
}
|
||||
}
|
||||
|
||||
if ($indention === '') {
|
||||
return implode(",\n", $luaStringParts);
|
||||
}
|
||||
|
||||
return "\n" . implode(",\n", $luaStringParts);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user