Skip to content

Commit

Permalink
Replace custom config parser #889
Browse files Browse the repository at this point in the history
YAML with Spyc class
  • Loading branch information
ulrichblock committed Jun 27, 2016
1 parent b4a3850 commit 81ec1f1
Show file tree
Hide file tree
Showing 3 changed files with 1,203 additions and 18 deletions.
5 changes: 4 additions & 1 deletion THIRDPARTY
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ https://github.com/milesj/decoda
Released under the MIT license
http://opensource.org/licenses/MIT


* Spyc -- A Simple PHP YAML Class
https://github.com/mustangostang/spyc/
Released under the MIT license
http://opensource.org/licenses/MIT

Shipped with Easy-WI but modified and fitted to Easy-WI:

Expand Down
56 changes: 39 additions & 17 deletions stuff/methods/class_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ private function protectedSettingsToArray () {

$line = str_replace(array("\r"), '', $line);

if (preg_match('/^(\[[\w\/\.\-\_]{1,}\]|\[[\w\/\.\-\_]{1,}\] (xml|ini|cfg|lua|json|ddot|yml))$/', $line)) {
if (preg_match('/^(\[[\w\/\.\-\_]{1,}\]|\[[\w\/\.\-\_]{1,}\] (xml|ini|cfg|lua|json|ddot|yml|yaml))$/', $line)) {

$exploded = preg_split("/\s+/", $line, -1, PREG_SPLIT_NO_EMPTY);

Expand All @@ -927,9 +927,9 @@ private function protectedSettingsToArray () {

$splitLine = preg_split("/\s+/", $line, -1, PREG_SPLIT_NO_EMPTY);

} else if ($cvarProtectArray[$configPathAndFile]['type'] == 'yml') {
} else if ($cvarProtectArray[$configPathAndFile]['type'] == 'yml' or $cvarProtectArray[$configPathAndFile]['type'] == 'yaml') {

$splitLine = preg_split("/(?:(?<!-))\s+/", $line);
$splitLine = preg_split("/(?:(?<!-))\s*:\s*/", $line);

} else if (in_array($cvarProtectArray[$configPathAndFile]['type'], array('ini','lua'))) {

Expand Down Expand Up @@ -1045,6 +1045,15 @@ private function isAssociative($array) {
private function replaceArrayValues($givenArray, $replacements) {

foreach(array_keys($givenArray) as $key) {

if (is_array($givenArray[$key])) {
$givenArray[$key] = $this->replaceArrayValues($givenArray[$key], $replacements);
}

if (isset($this->undefinedRequiredVars[$key])) {
unset($this->undefinedRequiredVars[$key]);
}

if (isset($replacements[$key])) {
$givenArray[$key] = $replacements[$key];
}
Expand All @@ -1063,10 +1072,6 @@ private function generateIniString($array) {

foreach($array as $key => $value) {

if (isset($this->undefinedRequiredVars[$key])) {
unset($this->undefinedRequiredVars[$key]);
}

if (is_array($value)) {

foreach($value as $arrayValue) {
Expand Down Expand Up @@ -1107,6 +1112,19 @@ private function replaceIni($stored, $replacements) {
return $iniString;
}

private function replaceYaml($stored, $replacements) {

$this->undefinedRequiredVars = $replacements;

$replacedArray = $this->replaceArrayValues($stored, $replacements);

foreach ($this->undefinedRequiredVars as $key => $value) {
$replacedArray[$key] = $value;
}

return Spyc::YAMLDump($replacedArray);
}

private function correctProtectedFiles () {

$protectedConfigs = $this->protectedSettingsToArray();
Expand Down Expand Up @@ -1152,6 +1170,20 @@ private function correctProtectedFiles () {

$ftpObect->writeContentToTemp($this->replaceIni($parsedConfig, $values['cvars']));

} else if ($values['type'] === 'yml' or $values['type'] === 'yaml') {

if (!class_exists('Spyc')) {
include(EASYWIDIR . '/third_party/spyc/Spyc.php');
}

$parsedConfig = Spyc::YAMLLoadString($configFileContent);

if (!$parsedConfig) {
$parsedConfig = array();
}

$ftpObect->writeContentToTemp($this->replaceYaml($parsedConfig, $values['cvars']));

} else {

$cvarsNotFound = $values['cvars'];
Expand Down Expand Up @@ -1185,16 +1217,6 @@ private function correctProtectedFiles () {

$ftpObect->writeContentToTemp((isset($splitLine[1])) ? $splitLine[0] . $cvar . ' ' . $value : $cvar . ' ' . $value);

} else if ($values['type'] == 'yml' and preg_match('/^[\s\/]{0,}' . strtolower($cvar) . '\s+(.*)$/', $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'] == 'ddot' and preg_match('/^[\s\/]{0,}' . strtolower($cvar) . '[\s+]{0,}\:[\s+]{0,}(.*)$/', $loweredSingleLine)) {

$edited = true;
Expand Down
Loading

0 comments on commit 81ec1f1

Please sign in to comment.