4 * A tool for running hook functions.
6 * Copyright 2004, 2005 Evan Prodromou <evan@wikitravel.org>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 * @author Evan Prodromou <evan@wikitravel.org>
30 * Used to supersede $wgHooks, because globals are EVIL.
36 * Array of events mapped to an array of callbacks to be run
37 * when that event is triggered.
39 protected static $handlers = [];
42 * Attach an event handler to a given hook.
44 * @param string $name Name of hook
45 * @param callable $callback Callback function to attach
49 public static function register( $name, $callback ) {
50 if ( !isset( self
::$handlers[$name] ) ) {
51 self
::$handlers[$name] = [];
54 self
::$handlers[$name][] = $callback;
58 * Clears hooks registered via Hooks::register(). Does not touch $wgHooks.
59 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
61 * @param string $name The name of the hook to clear.
64 * @throws MWException If not in testing mode.
66 public static function clear( $name ) {
67 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
68 throw new MWException( 'Cannot reset hooks in operation.' );
71 unset( self
::$handlers[$name] );
75 * Returns true if a hook has a function registered to it.
76 * The function may have been registered either via Hooks::register or in $wgHooks.
80 * @param string $name Name of hook
81 * @return bool True if the hook has a function registered to it
83 public static function isRegistered( $name ) {
85 return !empty( $wgHooks[$name] ) ||
!empty( self
::$handlers[$name] );
89 * Returns an array of all the event functions attached to a hook
90 * This combines functions registered via Hooks::register and with $wgHooks.
94 * @param string $name Name of the hook
97 public static function getHandlers( $name ) {
100 if ( !self
::isRegistered( $name ) ) {
102 } elseif ( !isset( self
::$handlers[$name] ) ) {
103 return $wgHooks[$name];
104 } elseif ( !isset( $wgHooks[$name] ) ) {
105 return self
::$handlers[$name];
107 return array_merge( self
::$handlers[$name], $wgHooks[$name] );
112 * Call hook functions defined in Hooks::register and $wgHooks.
114 * For a certain hook event, fetch the array of hook events and
115 * process them. Determine the proper callback for each hook and
116 * then call the actual hook using the appropriate arguments.
117 * Finally, process the return value and return/throw accordingly.
119 * @param string $event Event name
120 * @param array $args Array of parameters passed to hook functions
121 * @param string|null $deprecatedVersion Optionally, mark hook as deprecated with version number
122 * @return bool True if no handler aborted the hook
126 * @throws MWException
127 * @since 1.22 A hook function is not required to return a value for
128 * processing to continue. Not returning a value (or explicitly
129 * returning null) is equivalent to returning true.
131 public static function run( $event, array $args = [], $deprecatedVersion = null ) {
132 foreach ( self
::getHandlers( $event ) as $hook ) {
133 // Turn non-array values into an array. (Can't use casting because of objects.)
134 if ( !is_array( $hook ) ) {
138 if ( !array_filter( $hook ) ) {
139 // Either array is empty or it's an array filled with null/false/empty.
141 } elseif ( is_array( $hook[0] ) ) {
142 // First element is an array, meaning the developer intended
143 // the first element to be a callback. Merge it in so that
144 // processing can be uniform.
145 $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
149 * $hook can be: a function, an object, an array of $function and
150 * $data, an array of just a function, an array of object and
151 * method, or an array of object, method, and data.
153 if ( $hook[0] instanceof Closure
) {
154 $func = "hook-$event-closure";
155 $callback = array_shift( $hook );
156 } elseif ( is_object( $hook[0] ) ) {
157 $object = array_shift( $hook );
158 $method = array_shift( $hook );
160 // If no method was specified, default to on$event.
161 if ( $method === null ) {
162 $method = "on$event";
165 $func = get_class( $object ) . '::' . $method;
166 $callback = [ $object, $method ];
167 } elseif ( is_string( $hook[0] ) ) {
168 $func = $callback = array_shift( $hook );
170 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
173 // Run autoloader (workaround for call_user_func_array bug)
174 // and throw error if not callable.
175 if ( !is_callable( $callback ) ) {
176 throw new MWException( 'Invalid callback ' . $func . ' in hooks for ' . $event . "\n" );
180 * Call the hook. The documentation of call_user_func_array says
181 * false is returned on failure. However, if the function signature
182 * does not match the call signature, PHP will issue an warning and
183 * return null instead. The following code catches that warning and
184 * provides better error message.
188 $hook_args = array_merge( $hook, $args );
190 // mark hook as deprecated, if deprecation version is specified
191 if ( $deprecatedVersion !== null ) {
192 wfDeprecated( "$event hook (used in $func)", $deprecatedVersion );
195 $retval = call_user_func_array( $callback, $hook_args );
197 // Process the return value.
198 if ( is_string( $retval ) ) {
199 // String returned means error.
200 throw new FatalError( $retval );
201 } elseif ( $retval === false ) {
202 // False was returned. Stop processing, but no error.