Gearman.class.php 1.3 KB
<?php 
/**
 * 任务调度使用类
 * 
 * @example
 * <pre>
 * 	$c = Util_Gearman::client();
 *  $w = Util_Gearman::worker();
 * </pre>
 * 
 * @name Util_Gearman
 * @package Util
 * @copyright yoho.inc
 * @version 1.1 (2012-07-31 10:39:21)
 * @author fei.hong <hf@yoho.cn>
 * @since 1.0
 */
class Util_Gearman {
	
	/**
	 * @var array (Util_Gearman_Client | Util_Gearman_Worker )
	 */
	private static $gearman = array ();
	
	/**
	 * 工厂: 生成任务调度的操作对象
	 * 
	 * @param String $router ("Client" | "Worker")
	 * @throws Util_Exception
	 * @return Util_Gearman_Client | Util_Gearman_Worker
	 */
	public static function factory($router = 'Client') {
		$gearClass = 'Util_Gearman_' . ucfirst ( $router );
		if (false === class_exists ( $gearClass )) {
			throw new Util_Gearman_Exception('没有' . $gearClass . '类文件.' . __CLASS__);
		}
		if (! isset ( self::$gearman[$gearClass] )) {
		    self::$gearman [$gearClass] = new $gearClass ();
		}
		return self::$gearman [$gearClass];
	}
	
	/**
	 * 客户端使用类
	 * 
	 * @return Util_Gearman_Client
	 */
	public static function client() {
	    return Util_Gearman::factory ( 'Client' );
	}
	
	/**
	 * 工作者使用类
	 * 
	 * @return Util_Gearman_Worker
	 */
	public static function worker() {
	    return Util_Gearman::factory ( 'Worker' );
	}
	
}