From: Jason S. <jsw...@ya...> - 2003-01-13 16:48:23
|
I did not like specifying the options and mappings as global arrays. I put together this class that could be included as part of the phrame distribtion. You would extend this class for each of your applications to generate the appropriate mappings. It has the advantage of looking a little more compact when defining the mappings, and has some validation regarding the actual structure of the arrays. A example extention and usage is shown below: MappingManager.php: <?php /** * class to create Phrame Mapping arrays * * @author Jason E. Sweat * @since 2003-01-13 */ class MappingManager extends Object { /** * @var array */ var $_aaMap = array( _ACTION_FORMS => array() ,_ACTION_MAPPINGS => array() ); /** * @var array */ var $_aaOptions = array(); /** * constructor * * @return void */ function MappingManager() { trigger_error("MappingManager is a virtual class, please extend for your application"); return false; } /** * add a form to the mapping * * "protected" function to be used by the constructor function of a derived class * @return boolean sucess */ function _AddForm($psIdent, $psType) { if (!is_string($psIdent) || 0 == strlen($psIdent)) { trigger_error("invalid form identifier '$psIdent'"); return false; } if (!is_string($psType) || 0 == strlen($psType)) { trigger_error("invalid form type '$psType'"); return false; } $a_new_form = array(_TYPE => $psType); $this->_aaMap[_ACTION_FORMS][$psIdent] = $a_new_form; } /** * add a map to the mapping * * "protected" function to be used by the constructor function of a derived class * @return boolean sucess */ function _AddMapping($psIdent, $psType, $psInput, $psForm='_DEFAULT_', $piValidate=0) { if (!is_string($psIdent) || 0 == strlen($psIdent)) { trigger_error("invalid mapping identifier '$psIdent'"); return false; } if (!is_string($psType) || 0 == strlen($psType)) { trigger_error("invalid mapping type '$psType'"); return false; } if ('_DEFAULT_' == $psForm) { $psForm = $psIdent; if (!array_key_exists($psForm, $this->_aaMap[_ACTION_FORMS])) { trigger_error("no form mapping to '$psForm' has been established"); return false; } } if (1 != $piValidate) { $piValidate = 0; } $a_new_map = array( _TYPE => $psType ,_NAME => $psForm ,_INPUT => $psInput ,_VALIDATE => $piValidate ,_ACTION_FORWARDS => array() ); $this->_aaMap[_ACTION_MAPPINGS][$psIdent] = $a_new_map; return true; } /** * add a forward to an existing mapping * * "protected" function to be used by the constructor function of a derived class * @return boolean sucess */ function _AddForward($psMapIdent, $psFwdIdent, $psPath='_DEFAULT_', $piRedir=0) { if (!array_key_exists($psMapIdent, $this->_aaMap[_ACTION_MAPPINGS])) { trigger_error("invalid mapping identifier '$psMapIdent'"); return false; } if (!is_string($psFwdIdent) || 0 == strlen($psFwdIdent)) { trigger_error("invalid mapping forward identifier '$psFwdIdent'"); return false; } if (!is_string($psPath) || 0 == strlen($psPath)) { trigger_error("invalid mapping forward path '$psPath'"); return false; } elseif ('_DEFAULT_' == $psPath) { $psPath = $this->_aaMap[_ACTION_MAPPINGS][$psMapIdent][_INPUT]; } if (1 != $piRedir) { $piRedir = 0; } $a_new_fwd = array ( _PATH => $psPath ,_REDIRECT => $piRedir ); $this->_aaMap[_ACTION_MAPPINGS][$psMapIdent][_ACTION_FORWARDS][$psFwdIdent] = $a_new_fwd; return true; } /** * * @return void */ function _SetOptions($psErrorHandler='handle_error' ,$piCache=0 ,$piErrorReporting=-1 ) { if (!1 == $piCache) { $piCache = 0; } if (-1 == $piErrorReporting) { $piErrorReporting = E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE; } $this->_aaOptions = array( _CACHE => $piCache ,_ERROR_REPORTING => $piErrorReporting ,_ERROR_HANDLER => $psErrorHandler ); } /** * retrieve mappings * * @return array */ function GetMappings() { return $this->_aaMap; } /** * retrieve option * * @return array */ function GetOptions() { return $this->_aaOptions; } } ?> An example application extended version of the class is: class AdMaintMap extends MappingManager { /** * * @return */ function AdMaintMap() { $this->_SetOptions(); $this->_AddForm('gidIns', 'GidForm'); $this->_AddForm('empUpd', 'CrmEmpForm'); $this->_AddForm('usrUpd', 'OtherForm'); $this->_AddForm('urlUpd', 'UrlForm'); $this->_AddMapping('gidIns', 'GidInsAction', APPL_BASE.'gid'); $this->_AddForward('gidIns', 'gid'); $this->_AddMapping('gidDepotIns', 'GidDepotInsAction', APPL_BASE.'dpg', 'gidIns'); $this->_AddForward('gidDepotIns', 'dpg'); $this->_AddMapping('gidUpd', 'GidUpdAction', APPL_BASE.'gids', 'gidIns'); $this->_AddForward('gidUpd', 'gids'); $this->_AddMapping('gidDepotUpd', 'GidDepotUpdAction', APPL_BASE.'dpgs', 'gidIns'); $this->_AddForward('gidDepotUpd', 'dpgs'); $this->_AddMapping('empUpd', 'CrmEmpUpdAction', APPL_BASE.'emp'); $this->_AddForward('empUpd', 'emp'); $this->_AddMapping('empIns', 'CrmEmpInsAction', APPL_BASE.'emp', 'empUpd'); $this->_AddForward('empIns', 'emp'); $this->_AddMapping('usrUpd', 'OtherUpdAction', APPL_BASE.'usr'); $this->_AddForward('usrUpd', 'usr'); $this->_AddMapping('usrIns', 'OtherInsAction', APPL_BASE.'usr', 'usrUpd'); $this->_AddForward('usrIns', 'usr'); $this->_AddMapping('urlUpd', 'UrlUpdAction', APPL_BASE.'url'); $this->_AddForward('urlUpd', 'url'); } } You could then use it in your application like this: $map = new AdMaintMap; if (!$_SESSION[_CONTROLLER]) { $controller = new ActionController($map->GetOptions); $_SESSION[_CONTROLLER] = $controller; } $controller->process($map->GetMappings(), $_REQUEST); __________________________________________________ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com |