<?php
function mockInputRead()
{
static $commands = [
'open foo',
'close bar',
'dance with the devil in the pale moonlight',
'exit',
];
return array_shift($commands);
}
function vprintfln($format, array $args = [])
{
vprintf($format . PHP_EOL, $args);
}
$handlers = [
'open' => function ($target) {
vprintfln('Opening "%s"', [$target]);
return 0;
},
'close' => function ($target) {
vprintfln('Closing "%s"', [$target]);
return 0;
},
'exit' => function () {
vprintfln('Exiting...');
return 1;
},
];
$invalidHandler = function ($command) {
vprintfln('## Invalid command "%s"', [$command]);
};
while (true)
{
list($command, $argument) = explode(' ', mockInputRead(), 2) + [null, null];
if (!empty($command))
{
if (isset($handlers[$command]))
{
if ($handlers[$command]($argument) > 0)
{
break;
}
}
else
{
$invalidHandler($command);
}
}
}
vprintfln('Bye!');
preferences:
55.75 ms | 402 KiB | 5 Q