MediaWiki  master
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 InvalidArgumentException( "Invalid callback provided for '$name' function." );
58  }
59  $this->$name = $callbacks[$name];
60  }
61  }
62  if ( !isset( $this->doWork ) ) {
63  throw new InvalidArgumentException( "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( $fast ) {
80  if ( $this->fallback ) {
81  return ( $this->fallback )( $fast );
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 }
Convenience class for dealing with PoolCounter using callbacks.
__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.
getCachedWork()
Retrieve the work from cache.
fallback( $fast)
A work not so good (eg.
doWork()
Actually perform the work, caching it if needed.
Class for dealing with PoolCounters using class members.