Skip to content

Instantly share code, notes, and snippets.

@d9k
Last active August 29, 2015 14:19
Show Gist options
  • Save d9k/fd929bc46b0cb9aa353a to your computer and use it in GitHub Desktop.
Save d9k/fd929bc46b0cb9aa353a to your computer and use it in GitHub Desktop.
#!/usr/bin/env
<?php
$shortArrayOnOneString = false;
$maxArrayOnOneStringLen = 60;
$convertToNumberIfPossible = false;
function _arrayLinesToString($lines, $indent){
global $maxArrayOnOneStringLen;
global $shortArrayOnOneString;
$totalLen = 0;
$trimmedLines = [];
foreach ($lines as $line){
if ($totalLen > 0){
$totalLen += 2; //", " - 2 symbols len
}
$trimmedLine = trim($line);
$trimmedLines[] = $trimmedLine;
$totalLen += strlen($trimmedLine);
if ($totalLen > $maxArrayOnOneStringLen){
break;
}
}
return ($shortArrayOnOneString && $totalLen < $maxArrayOnOneStringLen) ?
"[" . implode(", ", $trimmedLines) . "]"
: "[\n" . implode(",\n", $lines) . "\n" . $indent . "]";
}
function var_export54($var, $indent="") {
global $convertToNumberIfPossible;
switch (gettype($var)) {
case "string":
if ($convertToNumberIfPossible && is_numeric($var)){
if ((($t = filter_var($var, FILTER_VALIDATE_INT)) !== false)
|| (($t = filter_var($var, FILTER_VALIDATE_FLOAT)) !== false)){
//don't strip leading zeroes or whitespaces
if (strlen((string)$t) == strlen($var)){
return (string)$t;
}
}
}
return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
case "array":
$indexed = array_keys($var) === range(0, count($var) - 1);
$lines = [];
foreach ($var as $key => $value) {
$lines[] = "$indent "
. ($indexed ? "" : var_export54($key) . " => ")
. var_export54($value, "$indent ");
}
// return "[\n" . implode(",\n", $lines) . "\n" . $indent . "]";
return _arrayLinesToString($lines, $indent);
case "boolean":
return $var ? "TRUE" : "FALSE";
case "double": //fix precision break issue
return (string)$var;
default:
return var_export($var, TRUE);
}
}
function q($s){
return '"' . $s . '"';
}
$outputToSameFile = false;
$filePath = null;
class ArgType{
const INT = 'int';
}
function getNextArg($n, $argType=ArgType::INT){
global $argv;
if (!isset ($argv[$n+1])){
return null;
}
$rawValue = $argv[$n+1];
$value = null;
if (is_string($rawValue) && $rawValue[0] == '-'){
return;
}
switch ($argType){
case ArgType::INT:
if (is_numeric($rawValue)){
if ($rawValue = filter_var($rawValue, FILTER_VALIDATE_INT)){
$value = $rawValue;
}
};
break;
default:
return null;
}
return $value;
}
function echoUsage(){
echo <<<'USAGE'
This util exports php var from file to square-bracketed php 5.4 style array
Usage: var-export [params] path_to_file'
Args:
-h : echo this help
-n : numeric mode - convert strings to numbers if possible
-s columns_num: shortmode. Write array on one line if it's short. Default columns num is {$maxArrayOnOneStringLen}
-o: overwrite original file
USAGE;
}
$n = 1;
while ($n < $argc) {
$arg = $argv[$n];
if ($arg == '-h') {
echoUsage();
exit();
} else if ($arg == '-s'){
$shortArrayOnOneString = true;
if ($t = getNextArg($n, ArgType::INT)){
$maxArrayOnOneStringLen = $t;
$n++;
}
} else if ($arg == '-n') {
$convertToNumberIfPossible = true;
} else if ($arg == '-o') {
$outputToSameFile = true;
} else if ($arg[0] != '-') {
$filePath = $arg;
}
$n++;
}
if(!$filePath) {
echoUsage();
exit(1);
}
echo 'processing ' . q($filePath)."\n\n";
if (!file_exists($filePath)){
echo 'Can\'t find file '.q($filePath);
exit(1);
}
if (!is_readable($filePath)){
echo 'Can\'t read file '.q($filePath);
exit(1);
}
$fileText = file_get_contents($filePath);
$a = eval('return '.$fileText.';');
if ( $a === false && ( $error = error_get_last() ) ) {
// echo $error['type'], $error['message'], $error['file'], $error['line'], null );
echo $error['message']."\n";
exit(1);
}
$exported = var_export54($a);
echo $exported."\n";
if ($outputToSameFile){
if (!is_writable($filePath)){
echo 'Can\'t write to file ' . q($filePath);
exit(1);
}
file_put_contents($filePath, $exported);
echo 'written to '.q($filePath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment