improve formatting in generated config file

This commit is contained in:
Crack
2010-07-30 19:45:53 +02:00
parent 157c4fb775
commit 7f75e7515f

View File

@@ -457,7 +457,10 @@ class ConfigFile
foreach ($server as $k => $v) { foreach ($server as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k); $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= "\$cfg['Servers'][\$i]['$k'] = " $ret .= "\$cfg['Servers'][\$i]['$k'] = "
. var_export($v, true) . ';' . $crlf; . (is_array($v) && $this->_isZeroBasedArray($v)
? $this->_exportZeroBasedArray($v, $crlf)
: var_export($v, true))
. ';' . $crlf;
} }
$ret .= $crlf; $ret .= $crlf;
} }
@@ -499,28 +502,50 @@ class ConfigFile
if (!is_array($var_value) || empty($var_value)) { if (!is_array($var_value) || empty($var_value)) {
return "\$cfg['$var_name'] = " . var_export($var_value, true) . ';' . $crlf; return "\$cfg['$var_name'] = " . var_export($var_value, true) . ';' . $crlf;
} }
$numeric_keys = true;
foreach (array_keys($var_value) as $k) {
if (!is_numeric($k)) {
$numeric_keys = false;
break;
}
}
if ($numeric_keys) {
for ($i = 0; $i < count($var_value); $i++) {
if (!isset($var_value[$i])) {
$numeric_keys = false;
break;
}
}
}
$ret = ''; $ret = '';
if ($numeric_keys) { if ($this->_isZeroBasedArray($var_value)) {
$ret = "\$cfg['$var_name'] = " . $this->_exportZeroBasedArray($var_value, $crlf)
. ');' . $crlf;
} else {
// string keys: $cfg[key][subkey] = value
foreach ($var_value as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= "\$cfg['$var_name']['$k'] = " . var_export($v, true) . ';' . $crlf;
}
}
return $ret;
}
/**
* Check whether $array is a continuous 0-based array
*
* @param array $array
* @return boolean
*/
private function _isZeroBasedArray(array $array)
{
for ($i = 0; $i < count($array); $i++) {
if (!isset($array[$i])) {
return false;
}
}
return true;
}
/**
* Exports continuous 0-based array
*
* @param array $array
* @param string $crlf
* @return string
*/
private function _exportZeroBasedArray(array $array, $crlf)
{
$retv = array(); $retv = array();
foreach ($var_value as $v) { foreach ($array as $v) {
$retv[] = var_export($v, true); $retv[] = var_export($v, true);
} }
$ret = "\$cfg['$var_name'] = array("; $ret = "array(";
if (count($retv) <= 4) { if (count($retv) <= 4) {
// up to 4 values - one line // up to 4 values - one line
$ret .= implode(', ', $retv); $ret .= implode(', ', $retv);
@@ -531,14 +556,7 @@ class ConfigFile
$ret .= ($i < $imax ? ($i > 0 ? ',' : '') : '') . $crlf . ' ' . $retv[$i]; $ret .= ($i < $imax ? ($i > 0 ? ',' : '') : '') . $crlf . ' ' . $retv[$i];
} }
} }
$ret .= ');' . $crlf; $ret .= ')';
} else {
// string keys: $cfg[key][subkey] = value
foreach ($var_value as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= "\$cfg['$var_name']['$k'] = " . var_export($v, true) . ';' . $crlf;
}
}
return $ret; return $ret;
} }
} }