Skip to content

Instantly share code, notes, and snippets.

@ringmaster
Created April 12, 2013 15:04
Show Gist options
  • Save ringmaster/5372683 to your computer and use it in GitHub Desktop.
Save ringmaster/5372683 to your computer and use it in GitHub Desktop.
/**
* Add objects or functions to the post-processing queue, executed via self::process_queue()
* @param QueryRecord|Callable $fn A QueryRecord to call ->update() on, or a function to execute
* @param null|string $name An index to save the queue under, defaults to the SPL object hash for objects
*/
public static function queue($fn, $name = null)
{
if(is_null($name) && is_object($fn)) {
$name = spl_object_hash($fn);
}
if(is_object($name) && is_object($fn)) {
$name = spl_object_hash($name);
}
if(is_null($name)) {
self::$write_queue[] = $fn;
}
else {
self::$write_queue[$name] = $fn;
}
}
/**
* Call update() on QueryRecords or execute the function queued with self::queue()
*/
public static function process_queue()
{
foreach(self::$write_queue as $fn) {
if($fn instanceof QueryRecord) {
$fn->update();
}
else {
$fn();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment