Factory pattern in Zend data grid

来源:互联网 发布:最新网络电影预告片 编辑:程序博客网 时间:2024/05/22 12:59

In the abstract class Bvb_Grid, there is static method named factory() which acts as a factory to create specific grid deploy class.

    public static function factory($defaultClass = 'Table', $options = array(), $id = '', $classCallbacks = array(), $requestParams = false)    {        self::initDeployClass();//initialize Zend_Loader_PluginLoader and add the prefix path        try {            $defaultClass = self::loadDeployClass($defaultClass);// load the deploy class         } catch (Zend_Loader_PluginLoader_Exception $e) {                        if (!class_exists($defaultClass)) {                throw $e;            }        }        if (false === $requestParams) {            // use request parameters            $requestParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();        }        if ($options instanceof Zend_Config) {            $options = $options->toArray();        }        // use this as request parameters        if (!isset($options['grid'])) {            $options['grid'] = array('requestParams' => $requestParams);        } else {            $options['grid']['requestParams'] = $requestParams;        }        // handle _exportTo parameter compatible with calling with grid id and without        if (isset($requestParams['_exportTo' . $id])) {            $exportTo = $requestParams['_exportTo' . $id];        } elseif (isset($requestParams['_exportTo'])) {            $exportTo = $requestParams['_exportTo'];        } else {            $exportTo = false;        }        if (false === $exportTo) {            // return instance of the main Bvb object, because this is not and export request            $grid = new $defaultClass($options);            $lClass = $defaultClass;        } else {            $lClass = strtolower($exportTo);            // support translating of parameters specifig for the export initiator class            if (isset($requestParams['_exportFrom'])) {                // TODO support translating of parameters specifig for the export initiator class                $requestParams = $requestParams;            }            // now we need to find and load the right Bvb deploy class            // TODO support user defined classes            $className = self::loadDeployClass($exportTo);            if (Zend_Loader_Autoloader::autoload($className)) {                $grid = new $className($options);            } else {                $grid = new $defaultClass($options);                $lClass = $defaultClass;            }        }        // add the powerfull configuration callback function        if (isset($classCallbacks[$lClass])) {            $grid->_configCallbacks = $classCallbacks[$lClass]; // register callbacks        }        if (is_string($id)) {            $grid->setGridId($id);        }        return $grid;    }
If we want to use one specific grid deploy class, we can also use it directly.

<?phpclass Bvb_Grid_Deploy_Table extends Bvb_Grid implements Bvb_Grid_Deploy_DeployInterface {public function __construct (array $options = array())    {        $this->_setRemoveHiddenFields(true);        parent::__construct($options);        if (isset($this->_options['grid']['id'])) {            $this->setGridId($this->_options['grid']['id']);        }        $this->_gridSession = new Zend_Session_Namespace('Bvb_Grid_' . $this->getGridId());        $this->addTemplateDir('Bvb/Grid/Template', 'Bvb_Grid_Template', 'table');        if ($this->getParam('add') || $this->getParam('edit')) {            if ($this->getParam('add')) {                $this->_willShow[] = 'form';                $this->_willShow[] = 'formAdd';            }            if ($this->getParam('edit')) {                $this->_willShow[] = 'form';                $this->_willShow[] = 'formEdit';            }        } else {            if ($this->getParam('detail') ||  $this->getParam('delete') ){                $this->_willShow[] = 'detail';            } else {                $this->_willShow[] = 'listing';            }        }    }}


原创粉丝点击