MediaWiki master
PoolCounterWorkViaCallback.php
Go to the documentation of this file.
1<?php
8
9use InvalidArgumentException;
10
20 protected $doWork;
22 protected $doCachedWork;
24 protected $fallback;
26 protected $error;
27
42 public function __construct( $pool, string $key, array $callbacks ) {
43 if ( is_string( $pool ) ) {
44 $type = $pool;
45 $pool = null;
46 } else {
47 $type = $pool->getType();
48 }
49
50 parent::__construct( $type, $key, $pool );
51
52 foreach ( [ 'doWork', 'doCachedWork', 'fallback', 'error' ] as $name ) {
53 if ( isset( $callbacks[$name] ) ) {
54 if ( !is_callable( $callbacks[$name] ) ) {
55 throw new InvalidArgumentException( "Invalid callback provided for '$name' function." );
56 }
57 $this->$name = $callbacks[$name];
58 }
59 }
60 if ( !$this->doWork ) {
61 throw new InvalidArgumentException( "No callback provided for 'doWork' function." );
62 }
63 $this->cacheable = (bool)$this->doCachedWork;
64 }
65
67 public function doWork() {
68 return ( $this->doWork )();
69 }
70
72 public function getCachedWork() {
73 if ( $this->doCachedWork ) {
74 return ( $this->doCachedWork )();
75 }
76 return false;
77 }
78
80 public function fallback( $fast ) {
81 if ( $this->fallback ) {
82 return ( $this->fallback )( $fast );
83 }
84 return false;
85 }
86
88 public function error( $status ) {
89 if ( $this->error ) {
90 return ( $this->error )( $status );
91 }
92 return false;
93 }
94}
95
97class_alias( PoolCounterWorkViaCallback::class, 'PoolCounterWorkViaCallback' );
Convenience class for dealing with PoolCounter using callbacks.
doWork()
Actually perform the work, caching it if needed.mixed|false Work result or false
error( $status)
Do something with the error, like showing it to the user.mixed|false
getCachedWork()
Retrieve the work from cache.mixed|false Work result or false
fallback( $fast)
A work not so good (eg.expired one) but better than an error message.mixed|false Work result or false
__construct( $pool, string $key, array $callbacks)
Build a PoolCounterWork class from a type, key, and callback map.
Class for dealing with PoolCounters using class members.