Last active
January 25, 2016 13:10
-
-
Save PEMapModder/0d634d91e41b2db80a25 to your computer and use it in GitHub Desktop.
A YAML/JSON reader/writer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM Disclaimer: This file is based on start.cmd of PocketMine-MP | |
@echo off | |
TITLE YAML|JSON Reader|Writer | |
cd /d %~dp0 | |
if exist bin\php\php.exe ( | |
set PHP_BINARY=bin\php\php.exe | |
) else ( | |
set PHP_BINARY=php | |
) | |
set PHP_FILE=YJRW.php | |
if not exist %PHP_FILE% ( | |
echo "Couldn't find main file. Please name the file YJRW.php" | |
pause | |
exit 1 | |
) | |
if exist bin\mintty.exe ( | |
start "" bin\mintty.exe -o AllowBlinking=0 -o CursorType=0 -h error -t "YAML/JSON Reader/Writer" -w max %PHP_BINARY% %PHP_FILE% --enable-ansi %* | |
) else ( | |
%PHP_BINARY% %PHP_FILE% %* | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$console = fopen("php://stdin", "r"); | |
$running = true; | |
while($running){ | |
$line = trim(fgets($console)); | |
if($line){ | |
// echo "Command issued: \"$line\"".PHP_EOL; | |
$args = explode(" ", $line); | |
switch($cmd = strtolower(trim(array_shift($args)))){ | |
case "yaml": | |
case "yml": | |
case "json": | |
if(isset($args[0])){ | |
$export = false; | |
if($args[0] === "-e"){ | |
$export = true; | |
array_shift($args); | |
} | |
$filename = implode(" ", $args); | |
$path = dirname(__FILE__)."/$filename"; | |
if(!is_file($path)){ | |
echo "Not a file!"; | |
} | |
else{ | |
$contents = file_get_contents($path); | |
$data = stripos($cmd, "j") === false ? yaml_parse($contents):json_decode($contents); | |
echo "Contents at $path:".PHP_EOL; | |
$str = var_export($data, true); | |
echo $str.PHP_EOL; | |
if($export){ | |
if(!is_file($file = dirname(__FILE__)."/output.php")){ | |
file_put_contents($file, "<?php".PHP_EOL); | |
} | |
file_put_contents($file, $str.";".PHP_EOL, FILE_APPEND); | |
} | |
} | |
} | |
else{ | |
echo "Please give filename!".PHP_EOL; | |
} | |
break; | |
case "writeyaml": | |
if(!isset($args[0])){ | |
echo "Please give filename!".PHP_EOL; | |
break; | |
} | |
$filename = array_shift($args); | |
if(file_exists($file = dirname(__FILE__)."/$filename")){ | |
echo "[WARNING] File already exists!".PHP_EOL; | |
break; | |
} | |
$data = array(); | |
$current = false; | |
for($i = 0; $i < count($args); $i++){ | |
if($args[$i]{0} === "-"){ | |
if($current !== false){ | |
$data[$current] = substr($data[$current], 0, -1); | |
} | |
$current = substr($args[$i], 1); | |
$data[$current] = ""; | |
continue; | |
} | |
if($current === false){ | |
echo "[ERROR] Incorrect syntax of writeyaml"; | |
break 2; | |
} | |
$data[$current] .= $args[$i]." "; | |
} | |
if($current !== false){ | |
$data[$current] = substr($data[$current], 0, -1); | |
} | |
file_put_contents($file, yaml_emit($data)); | |
break; | |
default: | |
echo "No such command!".PHP_EOL; | |
} | |
} | |
usleep(500000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment