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' ) && !defined( 'MW_PARSER_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 * @param string $event Event name
113 * @param array|callable $hook
114 * @param array $args Array of parameters passed to hook functions
115 * @param string|null $deprecatedVersion [optional]
116 * @param string &$fname [optional] Readable name of hook [returned]
117 * @return null|string|bool
119 private static function callHook( $event, $hook, array $args, $deprecatedVersion = null,
122 // Turn non-array values into an array. (Can't use casting because of objects.)
123 if ( !is_array( $hook ) ) {
127 if ( !array_filter( $hook ) ) {
128 // Either array is empty or it's an array filled with null/false/empty.
132 if ( is_array( $hook[0] ) ) {
133 // First element is an array, meaning the developer intended
134 // the first element to be a callback. Merge it in so that
135 // processing can be uniform.
136 $hook = array_merge( $hook[0], array_slice( $hook, 1 ) );
140 * $hook can be: a function, an object, an array of $function and
141 * $data, an array of just a function, an array of object and
142 * method, or an array of object, method, and data.
144 if ( $hook[0] instanceof Closure
) {
145 $fname = "hook-$event-closure";
146 $callback = array_shift( $hook );
147 } elseif ( is_object( $hook[0] ) ) {
148 $object = array_shift( $hook );
149 $method = array_shift( $hook );
151 // If no method was specified, default to on$event.
152 if ( $method === null ) {
153 $method = "on$event";
156 $fname = get_class( $object ) . '::' . $method;
157 $callback = [ $object, $method ];
158 } elseif ( is_string( $hook[0] ) ) {
159 $fname = $callback = array_shift( $hook );
161 throw new MWException( 'Unknown datatype in hooks for ' . $event . "\n" );
164 // Run autoloader (workaround for call_user_func_array bug)
165 // and throw error if not callable.
166 if ( !is_callable( $callback ) ) {
167 throw new MWException( 'Invalid callback ' . $fname . ' in hooks for ' . $event . "\n" );
170 // mark hook as deprecated, if deprecation version is specified
171 if ( $deprecatedVersion !== null ) {
172 wfDeprecated( "$event hook (used in $fname)", $deprecatedVersion );
176 $hook_args = array_merge( $hook, $args );
177 return call_user_func_array( $callback, $hook_args );
181 * Call hook functions defined in Hooks::register and $wgHooks.
183 * For the given hook event, fetch the array of hook events and
184 * process them. Determine the proper callback for each hook and
185 * then call the actual hook using the appropriate arguments.
186 * Finally, process the return value and return/throw accordingly.
188 * For hook event that are not abortable through a handler's return value,
189 * use runWithoutAbort() instead.
191 * @param string $event Event name
192 * @param array $args Array of parameters passed to hook functions
193 * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
194 * @return bool True if no handler aborted the hook
198 * @throws MWException
199 * @since 1.22 A hook function is not required to return a value for
200 * processing to continue. Not returning a value (or explicitly
201 * returning null) is equivalent to returning true.
203 public static function run( $event, array $args = [], $deprecatedVersion = null ) {
204 foreach ( self
::getHandlers( $event ) as $hook ) {
205 $retval = self
::callHook( $event, $hook, $args, $deprecatedVersion );
206 if ( $retval === null ) {
210 // Process the return value.
211 if ( is_string( $retval ) ) {
212 // String returned means error.
213 throw new FatalError( $retval );
214 } elseif ( $retval === false ) {
215 // False was returned. Stop processing, but no error.
224 * Call hook functions defined in Hooks::register and $wgHooks.
226 * @param string $event Event name
227 * @param array $args Array of parameters passed to hook functions
228 * @param string|null $deprecatedVersion [optional] Mark hook as deprecated with version number
229 * @return bool Always true
230 * @throws MWException If a callback is invalid, unknown
231 * @throws UnexpectedValueException If a callback returns an abort value.
234 public static function runWithoutAbort( $event, array $args = [], $deprecatedVersion = null ) {
235 foreach ( self
::getHandlers( $event ) as $hook ) {
237 $retval = self
::callHook( $event, $hook, $args, $deprecatedVersion, $fname );
238 if ( $retval !== null && $retval !== true ) {
239 throw new UnexpectedValueException( "Invalid return from $fname for unabortable $event." );