/** * Schedule $callable for delayed execution. * * $callable will be executed after the response has been sent to the client. * This is useful for expensive operations which do not need to send * anything to the client. * * At the first call to defer, deferring will be "activated". This means that * output buffering is enabled, keepalive disabled and user-abort is ignored. * You can check to see if deferring is enabled by doing a truth check on * gb::$deferred. The event "did-activate-deferring" is also posted. * * Use deferring wth caution. * * A good example of when delayed execution is a good idea, is how the * email-notification plugin defers the mail action (this is actually part of * GBMail but this plugin makes good use of it). * * Events: * * - "did-activate-deferring" * Posted when defer is activated. * */ static function defer($callable) { if (self::$deferred === null) { if (headers_sent()) { return false; } ob_start(); header('Transfer-Encoding: identity'); header('Connection: close'); self::$deferred = array(); register_shutdown_function(array('gb', 'run_deferred')); ignore_user_abort(true); gb::event('did-activate-deferring'); } self::$deferred[] = array($callable, array_slice(func_get_args(), 1)); return true; }