| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\PoolCounter; |
| 22 | |
| 23 | use InvalidArgumentException; |
| 24 | |
| 25 | /** |
| 26 | * Convenience class for dealing with PoolCounter using callbacks |
| 27 | * @since 1.22 |
| 28 | * @newable |
| 29 | * @note marked as newable in 1.35 for lack of a better alternative, |
| 30 | * but should use a factory in the future. |
| 31 | */ |
| 32 | class PoolCounterWorkViaCallback extends PoolCounterWork { |
| 33 | /** @var callable */ |
| 34 | protected $doWork; |
| 35 | /** @var callable|null */ |
| 36 | protected $doCachedWork; |
| 37 | /** @var callable|null */ |
| 38 | protected $fallback; |
| 39 | /** @var callable|null */ |
| 40 | protected $error; |
| 41 | |
| 42 | /** |
| 43 | * Build a PoolCounterWork class from a type, key, and callback map. |
| 44 | * |
| 45 | * The callback map must at least have a callback for the 'doWork' method. |
| 46 | * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback', |
| 47 | * and 'error' methods. Methods without callbacks will be no-ops that return false. |
| 48 | * If a 'doCachedWork' callback is provided, then execute() may wait for any prior |
| 49 | * process in the pool to finish and reuse its cached result. |
| 50 | * |
| 51 | * @stable to call |
| 52 | * @param string $type The class of actions to limit concurrency for |
| 53 | * @param string $key |
| 54 | * @param array $callbacks Map of callbacks |
| 55 | */ |
| 56 | public function __construct( $type, $key, array $callbacks ) { |
| 57 | parent::__construct( $type, $key ); |
| 58 | foreach ( [ 'doWork', 'doCachedWork', 'fallback', 'error' ] as $name ) { |
| 59 | if ( isset( $callbacks[$name] ) ) { |
| 60 | if ( !is_callable( $callbacks[$name] ) ) { |
| 61 | throw new InvalidArgumentException( "Invalid callback provided for '$name' function." ); |
| 62 | } |
| 63 | $this->$name = $callbacks[$name]; |
| 64 | } |
| 65 | } |
| 66 | if ( !isset( $this->doWork ) ) { |
| 67 | throw new InvalidArgumentException( "No callback provided for 'doWork' function." ); |
| 68 | } |
| 69 | $this->cacheable = isset( $this->doCachedWork ); |
| 70 | } |
| 71 | |
| 72 | public function doWork() { |
| 73 | return ( $this->doWork )(); |
| 74 | } |
| 75 | |
| 76 | public function getCachedWork() { |
| 77 | if ( $this->doCachedWork ) { |
| 78 | return ( $this->doCachedWork )(); |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | public function fallback( $fast ) { |
| 84 | if ( $this->fallback ) { |
| 85 | return ( $this->fallback )( $fast ); |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | public function error( $status ) { |
| 91 | if ( $this->error ) { |
| 92 | return ( $this->error )( $status ); |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** @deprecated class alias since 1.42 */ |
| 99 | class_alias( PoolCounterWorkViaCallback::class, 'PoolCounterWorkViaCallback' ); |