MediaWiki master
PoolCounterWorkViaCallback.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\PoolCounter;
22
23use InvalidArgumentException;
24
34 protected $doWork;
36 protected $doCachedWork;
38 protected $fallback;
40 protected $error;
41
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
99class_alias( PoolCounterWorkViaCallback::class, 'PoolCounterWorkViaCallback' );
Convenience class for dealing with PoolCounter using callbacks.
doWork()
Actually perform the work, caching it if needed.
__construct( $type, $key, array $callbacks)
Build a PoolCounterWork class from a type, key, and callback map.
error( $status)
Do something with the error, like showing it to the user.
Class for dealing with PoolCounters using class members.