modle factory

来源:互联网 发布:国际象棋在线对弈软件 编辑:程序博客网 时间:2024/06/05 17:01
interface InterfaceFactory {    /**     * @param string $name     * @return mixed     */    public static function create($name);}

model

class ModelFactory implements InterfaceFactory{    private static $_models = array();    public static function create($name) {        if (strrpos($name, 'Model') === false) {            $name .= 'Model';        }        $params = array_slice(func_get_args(), 1);        // @attention keep val same type, for generate same uniqName        foreach ($params as & $v) {            if (is_numeric($v)) {                $v = (int) $v;            }        }        $uniqName = $name . '_' . md5(serialize($params));        if (!isset(self::$_models[$uniqName])) {            $obj = new ReflectionClass($name);            self::$_models[$uniqName] = $obj->newInstanceArgs($params);            self::_setLogger(self::$_models[$uniqName]);        }        return self::$_models[$uniqName];    }    /**     * @param BaseModel $model     */    private static function _setLogger($model) {        if (method_exists($model, 'setLogger')) {            // @todo get dir&level from config            global $sharePrefix;            $logDir = LOGPATH . $sharePrefix;            $logger = Utils_Logger::getInstance($logDir,defined('ROTATE_LOGGING_LEVEL') ? ROTATE_LOGGING_LEVEL : Utils_Logger::OFF);            $model->setLogger($logger);        }    }    /**     * @param array $configs     * @return BaseModel     */    public static function createByConfig($configs) {        $params = array_slice(func_get_args(), 1);        $uniqName = 'anonymous' . '_' . md5(serialize($configs) . md5(serialize($params)));        if (!isset(self::$_models[$uniqName])) {            $obj = new ReflectionClass('BaseModel');            /** @var BaseModel $model */            $model = $obj->newInstanceArgs($params);            $model->setProperties($configs);            self::_setLogger($model);            self::$_models[$uniqName] = $model;        }        return self::$_models[$uniqName];    }}


control

$app = isset($_GET['_a']) ? $_GET['_a'] : 'Admin';$module = ucfirst((isset($_GET['_m']) ? $_GET['_m'] : 'MysteryChest_')) . 'Module';$controller = ucfirst((isset($_GET['_c']) ? $_GET['_c'] : 'MysteryChestTracker_')) . 'Controller';$action = (isset($_REQUEST['_ac']) ? $_REQUEST['_ac'] : 'index') . 'Action';$cls = new $controller();if(method_exists($cls,$action)) {    try {        $cls->$action();    }catch (CException $e) {        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {            echo json_encode(array('error_code' => $e->getCodeBE(), 'msg' => $e->getMessage(), 'data' => array()));        } else {            throw $e;        }    }} else {    throw new CException(404, "Method [{$controller}::{$action}] Not Found");}class MysteryChestTracker_Controller {    const  END_OF_WORLD = 32503651200;// 3000-1-1    public function __get($name) {        if ($name == '_tpl') {            return $this->_tpl = new TemplateLoader(array('tplDir' => PAGEPATH . 'admin/templates/'));        }        return NULL;    }    private function _initServersInfo() {    }    public function indexAction() {        $this->_initServersInfo();        $this->_tpl->display('MysteryChestTracker/index.php');    }    public  function showTableAction() {        $starttime = 0;        $endtime = self::END_OF_WORLD;        if (isset($_GET['starttime']) && !empty($_GET['starttime']) ) {            $starttime = strtotime($_GET['starttime']);        }        if (isset($_GET['endtime']) && !empty($_GET['endtime']) ) {            $endtime = strtotime($_GET['endtime']);        }        $this->_tpl->assign('trackerInfo', ModuleFactory::create('MysteryChest_TrackerModule')->getTrackerInfo($starttime,$endtime));        $this->_tpl->display('MysteryChestTracker/table.php');    }


0 0
原创粉丝点击