function var_export54($var, $indent = "") { switch (gettype($var)) { case "string": return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"'; case "array": $indexed = array_keys($var) === range(0, count($var) - 1); $r = []; foreach ($var as $key => $value) { $r[] = "{$indent} " . ($indexed ? "" : var_export54($key) . " => ") . var_export54($value, "{$indent} "); } return "[\n" . implode(",\n", $r) . "\n" . $indent . "]"; case "boolean": return $var ? "TRUE" : "FALSE"; default: return var_export($var, TRUE); } }
/** * Write a filename from the specified grouping in this handler * @param string $grouping * @return array */ public function write($grouping) { $fallbackParser = function ($pathInfo, $array) { switch ($pathInfo['extension']) { case 'xml': return xml_encode($array, 1024, ['root' => $pathInfo['filename']]); case 'json': return json_encode($array); case 'php': return "<?php\n\nreturn " . var_export54($array, true); case 'ini': $result = []; foreach ($array as $key => $value) { if (is_array($value)) { $result[] = "[{$key}]"; foreach ($value as $skey => $svalue) { $result[] = "{$skey} = " . (is_numeric($svalue) ? $svalue : '"' . $svalue . '"'); } } else { $result[] = "{$key} = " . (is_numeric($value) ? $value : '"' . $value . '"'); } } return implode(PHP_EOL, $result); } }; if (!isset($this[$grouping])) { throw new \RuntimeException(sprintf("The grouping '%s' is not set in the handler.", $grouping)); } $pathInfo = $this[$grouping . '_pathinfo']; $path = $pathInfo['dirname'] . '/' . $pathInfo['basename']; $array = $this[$grouping]->getArrayCopy(); $parser = array_key_exists($pathInfo['extension'], $this->readParsers) ? $this->readParsers[$pathInfo['extension']] : $fallbackParser; $contents = $parser($pathInfo, $array); $handle = fopen($path, 'w'); fwrite($handle, (string) $contents); fclose($handle); }