Last active
December 19, 2015 23:59
-
-
Save luniki/6038234 to your computer and use it in GitHub Desktop.
#halt
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
<? | |
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