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