Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / poolcounter / PoolCounterWorkViaCallback.php
blobaf83d2e06931b67f2f2687a9046e7a84fa7463e8
1 <?php
2 /**
3 * Provides of semaphore semantics for restricting the number
4 * of workers that may be concurrently performing the same task.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 /**
25 * Convenience class for dealing with PoolCounters using callbacks
26 * @since 1.22
28 class PoolCounterWorkViaCallback extends PoolCounterWork {
29 /** @var callable */
30 protected $doWork;
31 /** @var callable|null */
32 protected $doCachedWork;
33 /** @var callable|null */
34 protected $fallback;
35 /** @var callable|null */
36 protected $error;
38 /**
39 * Build a PoolCounterWork class from a type, key, and callback map.
41 * The callback map must at least have a callback for the 'doWork' method.
42 * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback',
43 * and 'error' methods. Methods without callbacks will be no-ops that return false.
44 * If a 'doCachedWork' callback is provided, then execute() may wait for any prior
45 * process in the pool to finish and reuse its cached result.
47 * @param string $type
48 * @param string $key
49 * @param array $callbacks Map of callbacks
50 * @throws MWException
52 public function __construct( $type, $key, array $callbacks ) {
53 parent::__construct( $type, $key );
54 foreach ( array( 'doWork', 'doCachedWork', 'fallback', 'error' ) as $name ) {
55 if ( isset( $callbacks[$name] ) ) {
56 if ( !is_callable( $callbacks[$name] ) ) {
57 throw new MWException( "Invalid callback provided for '$name' function." );
59 $this->$name = $callbacks[$name];
62 if ( !isset( $this->doWork ) ) {
63 throw new MWException( "No callback provided for 'doWork' function." );
65 $this->cacheable = isset( $this->doCachedWork );
68 public function doWork() {
69 return call_user_func_array( $this->doWork, array() );
72 public function getCachedWork() {
73 if ( $this->doCachedWork ) {
74 return call_user_func_array( $this->doCachedWork, array() );
76 return false;
79 public function fallback() {
80 if ( $this->fallback ) {
81 return call_user_func_array( $this->fallback, array() );
83 return false;
86 public function error( $status ) {
87 if ( $this->error ) {
88 return call_user_func_array( $this->error, array( $status ) );
90 return false;