MediaWiki  1.34.0
PoolCounterWorkViaCallback.php
Go to the documentation of this file.
1 <?php
30  protected $doWork;
32  protected $doCachedWork;
34  protected $fallback;
36  protected $error;
37 
52  public function __construct( $type, $key, array $callbacks ) {
53  parent::__construct( $type, $key );
54  foreach ( [ '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." );
58  }
59  $this->$name = $callbacks[$name];
60  }
61  }
62  if ( !isset( $this->doWork ) ) {
63  throw new MWException( "No callback provided for 'doWork' function." );
64  }
65  $this->cacheable = isset( $this->doCachedWork );
66  }
67 
68  public function doWork() {
69  return ( $this->doWork )();
70  }
71 
72  public function getCachedWork() {
73  if ( $this->doCachedWork ) {
74  return ( $this->doCachedWork )();
75  }
76  return false;
77  }
78 
79  public function fallback() {
80  if ( $this->fallback ) {
81  return ( $this->fallback )();
82  }
83  return false;
84  }
85 
86  public function error( $status ) {
87  if ( $this->error ) {
88  return ( $this->error )( $status );
89  }
90  return false;
91  }
92 }
PoolCounterWorkViaCallback
Convenience class for dealing with PoolCounters using callbacks.
Definition: PoolCounterWorkViaCallback.php:28
PoolCounterWorkViaCallback\fallback
fallback()
A work not so good (eg.
Definition: PoolCounterWorkViaCallback.php:79
PoolCounterWorkViaCallback\doWork
doWork()
Actually perform the work, caching it if needed.
Definition: PoolCounterWorkViaCallback.php:68
PoolCounterWorkViaCallback\$fallback
callable null $fallback
Definition: PoolCounterWorkViaCallback.php:34
PoolCounterWorkViaCallback\error
error( $status)
Do something with the error, like showing it to the user.
Definition: PoolCounterWorkViaCallback.php:86
MWException
MediaWiki exception.
Definition: MWException.php:26
PoolCounterWorkViaCallback\getCachedWork
getCachedWork()
Retrieve the work from cache.
Definition: PoolCounterWorkViaCallback.php:72
PoolCounterWorkViaCallback\$error
callable null $error
Definition: PoolCounterWorkViaCallback.php:36
PoolCounterWorkViaCallback\__construct
__construct( $type, $key, array $callbacks)
Build a PoolCounterWork class from a type, key, and callback map.
Definition: PoolCounterWorkViaCallback.php:52
$status
return $status
Definition: SyntaxHighlight.php:347
PoolCounterWork
Class for dealing with PoolCounters using class members.
Definition: PoolCounterWork.php:27
PoolCounterWorkViaCallback\$doCachedWork
callable null $doCachedWork
Definition: PoolCounterWorkViaCallback.php:32
PoolCounterWorkViaCallback\$doWork
callable $doWork
Definition: PoolCounterWorkViaCallback.php:30
PoolCounterWork\$type
string $type
Definition: PoolCounterWork.php:29