From: jenkins-bot Date: Wed, 20 Mar 2013 18:19:40 +0000 (+0000) Subject: Merge "Added a PoolCounterWorkViaCallback convenience class." X-Git-Tag: 1.31.0-rc.0~20271 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=efc9adf4e5bf7547af28999e64309d18ea867463;p=lhc%2Fweb%2Fwiklou.git Merge "Added a PoolCounterWorkViaCallback convenience class." --- efc9adf4e5bf7547af28999e64309d18ea867463 diff --cc includes/PoolCounter.php index c6a3077ed4,01a2047064..38c6f046bc --- a/includes/PoolCounter.php +++ b/includes/PoolCounter.php @@@ -246,4 -239,78 +246,74 @@@ abstract class PoolCounterWork return $this->error( $status ); } } - - function __construct( $type, $key ) { - $this->poolCounter = PoolCounter::factory( $type, $key ); - } } + + /** + * Convenience class for dealing with PoolCounters using callbacks + * @since 1.22 + */ + class PoolCounterWorkViaCallback extends PoolCounterWork { + /** @var callable */ + protected $doWork; + /** @var callable|null */ + protected $doCachedWork; + /** @var callable|null */ + protected $fallback; + /** @var callable|null */ + protected $error; + + /** + * Build a PoolCounterWork class from a type, key, and callback map. + * + * The callback map must at least have a callback for the 'doWork' method. + * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback', + * and 'error' methods. Methods without callbacks will be no-ops that return false. + * If a 'doCachedWork' callback is provided, then execute() may wait for any prior + * process in the pool to finish and reuse its cached result. + * + * @param string $type + * @param string $key + * @param array $callbacks Map of callbacks + * @throws MWException + */ + public function __construct( $type, $key, array $callbacks ) { + parent::__construct( $type, $key ); + foreach ( array( 'doWork', 'doCachedWork', 'fallback', 'error' ) as $name ) { + if ( isset( $callbacks[$name] ) ) { + if ( !is_callable( $callbacks[$name] ) ) { + throw new MWException( "Invalid callback provided for '$name' function." ); + } + $this->$name = $callbacks[$name]; + } + } + if ( !isset( $this->doWork ) ) { + throw new MWException( "No callback provided for 'doWork' function." ); + } + $this->cacheable = isset( $this->doCachedWork ); + } + + public function doWork() { + return call_user_func_array( $this->doWork, array() ); + } + + public function getCachedWork() { + if ( $this->doCachedWork ) { + return call_user_func_array( $this->doCachedWork, array() ); + } + return false; + } + + function fallback() { + if ( $this->fallback ) { + return call_user_func_array( $this->fallback, array() ); + } + return false; + } + + function error( $status ) { + if ( $this->error ) { + return call_user_func_array( $this->error, array( $status ) ); + } + return false; + } + }