Added a separate error message for mkdir failures
[lhc/web/wiklou.git] / includes / libs / ScopedCallback.php
1 <?php
2 /**
3 * This file deals with RAII style scoped callbacks.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * Class for asserting that a callback happens when an dummy object leaves scope
25 *
26 * @since 1.21
27 */
28 class ScopedCallback {
29 /** @var callable */
30 protected $callback;
31 /** @var array */
32 protected $params;
33
34 /**
35 * @param callable|null $callback
36 * @param array $params Callback arguments (since 1.25)
37 * @throws Exception
38 */
39 public function __construct( $callback, array $params = array() ) {
40 if ( $callback !== null && !is_callable( $callback ) ) {
41 throw new InvalidArgumentException( "Provided callback is not valid." );
42 }
43 $this->callback = $callback;
44 $this->params = $params;
45 }
46
47 /**
48 * Trigger a scoped callback and destroy it.
49 * This is the same is just setting it to null.
50 *
51 * @param ScopedCallback $sc
52 */
53 public static function consume( ScopedCallback &$sc = null ) {
54 $sc = null;
55 }
56
57 /**
58 * Destroy a scoped callback without triggering it
59 *
60 * @param ScopedCallback $sc
61 */
62 public static function cancel( ScopedCallback &$sc = null ) {
63 if ( $sc ) {
64 $sc->callback = null;
65 }
66 $sc = null;
67 }
68
69 /**
70 * Trigger the callback when this leaves scope
71 */
72 function __destruct() {
73 if ( $this->callback !== null ) {
74 call_user_func_array( $this->callback, $this->params );
75 }
76 }
77 }