[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/poolcounter/ -> PoolCounterWorkViaCallback.php (source)

   1  <?php
   2  /**
   3   * Provides of semaphore semantics for restricting the number
   4   * of workers that may be concurrently performing the same task.
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19   * http://www.gnu.org/copyleft/gpl.html
  20   *
  21   * @file
  22   */
  23  
  24  /**
  25   * Convenience class for dealing with PoolCounters using callbacks
  26   * @since 1.22
  27   */
  28  class PoolCounterWorkViaCallback extends PoolCounterWork {
  29      /** @var callable */
  30      protected $doWork;
  31      /** @var callable|null */
  32      protected $doCachedWork;
  33      /** @var callable|null */
  34      protected $fallback;
  35      /** @var callable|null */
  36      protected $error;
  37  
  38      /**
  39       * Build a PoolCounterWork class from a type, key, and callback map.
  40       *
  41       * The callback map must at least have a callback for the 'doWork' method.
  42       * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback',
  43       * and 'error' methods. Methods without callbacks will be no-ops that return false.
  44       * If a 'doCachedWork' callback is provided, then execute() may wait for any prior
  45       * process in the pool to finish and reuse its cached result.
  46       *
  47       * @param string $type
  48       * @param string $key
  49       * @param array $callbacks Map of callbacks
  50       * @throws MWException
  51       */
  52  	public function __construct( $type, $key, array $callbacks ) {
  53          parent::__construct( $type, $key );
  54          foreach ( array( '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 call_user_func_array( $this->doWork, array() );
  70      }
  71  
  72  	public function getCachedWork() {
  73          if ( $this->doCachedWork ) {
  74              return call_user_func_array( $this->doCachedWork, array() );
  75          }
  76          return false;
  77      }
  78  
  79  	public function fallback() {
  80          if ( $this->fallback ) {
  81              return call_user_func_array( $this->fallback, array() );
  82          }
  83          return false;
  84      }
  85  
  86  	public function error( $status ) {
  87          if ( $this->error ) {
  88              return call_user_func_array( $this->error, array( $status ) );
  89          }
  90          return false;
  91      }
  92  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1