From: Aaron Schulz Date: Thu, 14 Mar 2013 20:23:32 +0000 (-0700) Subject: Added ScopedCallback functions for early execution or cancellation. X-Git-Tag: 1.31.0-rc.0~20297^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=b141546c4e3fe4ea6df7a50406fad8514a4f4e91;p=lhc%2Fweb%2Fwiklou.git Added ScopedCallback functions for early execution or cancellation. Change-Id: I6493cfa81d778c47aa7283dc3196989384235f0a --- diff --git a/includes/ScopedCallback.php b/includes/ScopedCallback.php index 1d5b26bfb7..8ecd87471a 100644 --- a/includes/ScopedCallback.php +++ b/includes/ScopedCallback.php @@ -22,6 +22,8 @@ /** * Class for asserting that a callback happens when an dummy object leaves scope + * + * @since 1.21 */ class ScopedCallback { /** @var Closure */ @@ -34,7 +36,34 @@ class ScopedCallback { $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 ); + } } }