Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / libs / ScopedCallback.php
blob1ec9eaa62702b01a877cd78e07a12c2833367ba9
1 <?php
2 /**
3 * This file deals with RAII style scoped callbacks.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Class for asserting that a callback happens when an dummy object leaves scope
26 * @since 1.21
28 class ScopedCallback {
29 /** @var callable */
30 protected $callback;
31 /** @var array */
32 protected $params;
34 /**
35 * @param callable|null $callback
36 * @param array $params Callback arguments (since 1.25)
37 * @throws Exception
39 public function __construct( $callback, array $params = array() ) {
40 if ( $callback !== null && !is_callable( $callback ) ) {
41 throw new InvalidArgumentException( "Provided callback is not valid." );
43 $this->callback = $callback;
44 $this->params = $params;
47 /**
48 * Trigger a scoped callback and destroy it.
49 * This is the same is just setting it to null.
51 * @param ScopedCallback $sc
53 public static function consume( ScopedCallback &$sc = null ) {
54 $sc = null;
57 /**
58 * Destroy a scoped callback without triggering it
60 * @param ScopedCallback $sc
62 public static function cancel( ScopedCallback &$sc = null ) {
63 if ( $sc ) {
64 $sc->callback = null;
66 $sc = null;
69 /**
70 * Trigger the callback when this leaves scope
72 function __destruct() {
73 if ( $this->callback !== null ) {
74 call_user_func_array( $this->callback, $this->params );