From 4ac9fa081a7c045f6a9f1cfc529d82423f485b2e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sun, 8 Dec 2013 09:55:49 +0100 Subject: Update to MediaWiki 1.22.0 --- includes/ScopedCallback.php | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'includes/ScopedCallback.php') diff --git a/includes/ScopedCallback.php b/includes/ScopedCallback.php index 1d5b26bf..ef22e0a3 100644 --- a/includes/ScopedCallback.php +++ b/includes/ScopedCallback.php @@ -22,19 +22,52 @@ /** * Class for asserting that a callback happens when an dummy object leaves scope + * + * @since 1.21 */ class ScopedCallback { - /** @var Closure */ + /** @var callable */ protected $callback; /** - * @param $callback Closure + * @param callable $callback + * @throws MWException */ - public function __construct( Closure $callback ) { + public function __construct( $callback ) { + if ( !is_callable( $callback ) ) { + throw new MWException( "Provided callback is not valid." ); + } $this->callback = $callback; } + /** + * Trigger a scoped callback and destroy it. + * This is the same is just setting it to null. + * + * @param ScopedCallback $sc + */ + public static function consume( ScopedCallback &$sc = null ) { + $sc = null; + } + + /** + * Destroy a scoped callback without triggering it + * + * @param ScopedCallback $sc + */ + public static function cancel( ScopedCallback &$sc = null ) { + if ( $sc ) { + $sc->callback = null; + } + $sc = null; + } + + /** + * Trigger the callback when this leaves scope + */ function __destruct() { - call_user_func( $this->callback ); + if ( $this->callback !== null ) { + call_user_func( $this->callback ); + } } } -- cgit v1.2.2