Skip to content

Instantly share code, notes, and snippets.

@luniki
Last active December 19, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luniki/6038234 to your computer and use it in GitHub Desktop.
Save luniki/6038234 to your computer and use it in GitHub Desktop.
#halt
<?
function words($string)
{
return preg_split('/ /', $string, -1, PREG_SPLIT_NO_EMPTY);
}
function halt(/* [status], [headers], [body] */)
{
$args = func_get_args();
$states = words('status headers body');
while (!empty($args) && !empty($states)) {
$state = current($states);
if ($state === 'status' && is_int(current($args))
|| $state === 'headers' && is_array(current($args))
|| $state === 'body') {
$$state = array_shift($args);
}
array_shift($states);
}
echo json_encode(func_get_args()) . "\n";
echo json_encode(compact('status', 'headers', 'body')) . "\n\n";
}
halt();
// => []
halt(201);
// => {"status":201}
halt("Done");
// => {"body":"Done"}
halt(100, "Continue");
// => {"status":100,"body":"Continue"}
halt(301, array('Location'=>'y'));
// => {"status":301,"headers":{"Location":"y"}}
halt(302, "Found");
// => {"status":302,"body":"Found"}
halt(303, array('Location'=>'y'), 'See other');
// => {"status":303,"headers":{"Location":"y"},"body":"See other"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment