Last active
November 9, 2015 10:15
-
-
Save wan2land/8d9861bded981d21f5e2 to your computer and use it in GitHub Desktop.
bash command record and play
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
#!/usr/bin/env php | |
<?php | |
if (!isset($argv[1])) { | |
$mode = 'record'; | |
} else if (in_array($argv[1], ['record', 'play'])) { | |
$mode = $argv[1]; | |
} else { | |
fwrite(\STDERR, "first parameter must be record or play.\n"); | |
exit(-1); | |
} | |
// load fakerObject | |
$fakerObject = []; | |
if (file_exists("faker.cached.php")) { | |
$fakerObject = require "faker.cached.php"; | |
} | |
if ($mode === 'record') { | |
fwrite(\STDOUT, "Record Mode...\n$ "); | |
while ('exit' !== $read = trim(fread(\STDIN, 100))) { | |
exec("script -q /dev/null {$read} > faker.output"); | |
fwrite(\STDOUT, $output = file_get_contents('./faker.output')); | |
$fakerObject[$read] = $output; | |
file_put_contents( | |
'faker.cached.php', | |
'<?php return ' . var_export($fakerObject, true). ';' | |
); | |
fwrite(\STDOUT, '$ '); | |
} | |
} else { | |
fwrite(\STDOUT, "Play Mode...\n$ "); | |
while ('exit' !== $read = trim(fread(\STDIN, 100))) { | |
if (isset($fakerObject[$read])) { | |
fwrite(\STDOUT, $fakerObject[$read]); | |
} else { | |
fwrite(\STDERR, "\"{$read}\" is not exists!\n"); | |
} | |
fwrite(\STDOUT, '$ '); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment