From: Jason S. <jsw...@ya...> - 2003-05-06 02:25:27
|
Several people seem to have had troubles applying the diff (probably because the mail client mangled the line wraps). Here is my edited version of the ActionController.php for anyone who is interested. Regards, Jason <?php /** * The ActionController class represents the controller in the * Model-View-Controller (MVC) design pattern. The ActionController receives * and processes all requests that change the state of the application. * * Generally, a "Model2" application is architected as follows: * <ul> * <li>The user interface will generally be created with either PHP pages, * which will not themselves contain any business logic. These pages represent * the "view" component of an MVC architecture.</li> * <li>Forms and hyperlinks in the user interface that require business logic * to be executed will be submitted to a request URI that is mapped to the * ActionController. The ActionController receives and processes all requests * that change the state of a user's interaction with the application. This * component represents the "controller" component of an MVC architecture.</li> * <li>The ActionController will select and invoke an Action class to perform * the requested business logic.</li> * <li>The Action classes will manipulate the state of the application's * interaction with the user, typically by creating or modifying classes that * are stored as session attributes. Such classes represent the "model" * component of an MVC architecture.</li> * <li>Instead of producing the next page of the user interface directly, * Action classes will forward control to an appropriate PHP page to produce * the next page of the user interface.</li> * </ul> * The standard version of ActionController implements the following logic for * each incoming HTTP request. You can override some or all of this * functionality by subclassing this class and implementing your own version of * the processing. * <ul> * <li>Identify, from the incoming request URI, the substring that will be used * to select an Action procedure.</li> * <li>Use this substring to map to the class name of the corresponding Action * class (a subclass of the Action class).</li> * <li>If this is the first request for a particular Action class, instantiate * an instance of that class and cache it for future use.</li> * <li>Optionally populate the properties of an ActionForm class associated * with this ActionMapping and cache it for future use.</li> * <li>Call the perform() method of this Action class. Passing in the mapping * and the request that were passed to the ActionController by the bootstrap. * </li> * </ul> * * The standard version of ActionController is configured based on the * following initialization parameters, which you will specify in the options * for your application. Subclasses that specialize this ActionController are * free to define additional initialization parameters. * <ul> * <li><b>options</b> - This sets the ActionController options.</li> * </ul> * * @author Arnold Cano * @version $Id: ActionController.php,v 1.1.1.1 2002/11/19 16:46:54 arcano Exp $ */ class ActionController extends Object { /** * @var array */ var $_options; /** * @var HashMap */ var $_actionMappings; /** * @var HashMap */ var $_actionForms; /** * @var HashMap */ var $_actions; /** * Create a ActionController specifying the options. * * @access public * @param array $options */ function ActionController($options) { if (!is_array($options)) { trigger_error('Invalid options file'); return; } $this->_options = $options; //initialize cache $this->_actionMappings = new HashMap(); $this->_actionForms = new HashMap(); $this->_actions = new HashMap(); } /** * Process the request. * * @access public * @param array $mappings * @param array $request */ function process($mappings, $request) { if (!is_array($mappings)) { trigger_error('Invalid mappings file'); return; } if (!is_array($request)) { trigger_error('Invalid request'); return; } if(!defined('DISABLE_PHRAME_ERROR_HANDLING') || DISABLE_PHRAME_ERROR_HANDLING === false) error_reporting($this->_options[_ERROR_REPORTING]); $actionMapping = $this->_processMapping($mappings, $request); $actionForm = $this->_processForm($mappings, $request); if (is_object($actionForm)) { $_SESSION[_FORM] = $actionForm; if ($actionMapping->getValidate()) { if (!$this->_processValidate($actionMapping, $actionForm)) { return; } } } $actionForward = $this->_processAction($actionMapping, $actionForm); if (is_object($actionForward)) { $this->_processForward($actionForward); } } /** * Identify and return an appropriate ActionMapping. * * @access private * @param array $mappings * @param array $request * @return ActionMapping */ function &_processMapping($mappings, $request) { $name = $request[_ACTION]; $mapping = $mappings[_ACTION_MAPPINGS][$name]; $actionMapping = $this->_actionMappings->get($name); if (!is_object($actionMapping)) { $actionMapping = new ActionMapping($name, $mapping); if ($this->_options[_CACHE]) { $this->_actionMappings->put($name, $actionMapping); } } return $actionMapping; } /** * Identify and optionally return an appropriate populated ActionForm. * * @access private * @param array $mappings * @param array $request * @return ActionForm */ function &_processForm($mappings, $request) { $name = $request[_ACTION]; $mapping = $mappings[_ACTION_MAPPINGS][$name]; //verify that a form has been mapped if (isset($mapping[_NAME])) { $form = $mappings[_ACTION_FORMS][$mapping[_NAME]]; $type = $form[_TYPE]; $actionForm = $this->_actionForms->get($name); if (!is_object($actionForm)) { if (!class_exists($type)) { require_once "forms/$type.php"; } if (!class_exists($type)) { trigger_error("Invalid ActionForm '$name' type '$type'"); return; } $actionForm = new $type(); if ($this->_options[_CACHE]) { $this->_actionForms->put($name, $actionForm); } } //reset all properties to their default state $actionForm->reset(); //populate the properties from the request $actionForm->putAll($request); } return $actionForm; } /** * Call the validate() method of the specified ActionForm. * * @access private * @param ActionMapping $actionMapping * @param ActionForm $actionForm * @return boolean */ function _processValidate(&$actionMapping, &$actionForm) { $isValid = TRUE; if(!defined('DISABLE_PHRAME_ERROR_HANDLING') || DISABLE_PHRAME_ERROR_HANDLING === false) set_error_handler($this->_options[_ERROR_HANDLER]); if (!$actionForm->validate()) { $input = $actionMapping->getInput(); //forward errors back to view header("Location: $input&".SID); $isValid = FALSE; } if(!defined('DISABLE_PHRAME_ERROR_HANDLING') || DISABLE_PHRAME_ERROR_HANDLING === false) restore_error_handler(); return $isValid; } /** * Ask the specified Action instance to handle this request. * * @access private * @param ActionMapping $actionMapping * @param ActionForm $actionForm * @return ActionForward */ function &_processAction(&$actionMapping, &$actionForm) { $name = $actionMapping->getName(); $type = $actionMapping->getType(); $action = $this->_actions->get($name); if (!is_object($action)) { if (!class_exists($type)) { require_once "actions/$type.php"; } if (!class_exists($type)) { trigger_error("Invalid Action '$name' type '$type'"); return; } $action = new $type(); if ($this->_options[_CACHE]) { $this->_actions->put($name, $action); } } set_error_handler($this->_options[_ERROR_HANDLER]); $actionForward = $action->perform($actionMapping, $actionForm); restore_error_handler(); return $actionForward; } /** * Forward to the specified destination. * * @access private * @param ActionForward $actionForward */ function _processForward(&$actionForward) { $redirect = $actionForward->getRedirect(); $path = $actionForward->getPath(); if (!$redirect) { header("Location: $path&".SID); exit; } else { $_SESSION = array(); session_destroy(); header("Location: $path"); exit; } } } ?> --- Jason Sweat <jsw...@ya...> wrote: > One other change that was implemented in this diff file was the inclusion of > actions and forms from their respective directories if the appropriate class > is > not yet defined. Right now the directories actions and forms are hard coded, > and should probably be changed to Phrame constants. This changes allows for > only a minimum number of classes to be included when the Phrame application > starts. > > Regards, > > Jason > > --- Jason Sweat <jsw...@ya...> wrote: > > These changes are a bit more extensive, but I do not believe they have any > > backwards compatability issues. > > > > The first change is to allow for the _possible_ definition of the PHP > > constant > > DISABLE_PHRAME_ERROR_HANDLING. If this constant is defined, and it is not > > set > > to false, then this will _NOT_ set the custom error handling. This is very > > useful for debugging, as the error handler will generally hide PHP fatal > > errors > > when enabled. > > > > The second change is to pass by reference where objects are expected (and > > return by reference as well). > > > > The third change is the exit; after header(); mentioned in the prior post. > > > > Jason > > > > $ diff ActionController.php ActionController.jes.php > > 110c110,111 > > < error_reporting($this->_options[_ERROR_REPORTING]); > > --- > > > if(!defined('DISABLE_PHRAME_ERROR_HANDLING') || > > DISABLE_PHRAME_ERROR_ > > HANDLING === false) > > > error_reporting($this->_options[_ERROR_REPORTING]); > > 134c135 > > < function _processMapping($mappings, $request) > > --- > > > function &_processMapping($mappings, $request) > > 155c156 > > < function _processForm($mappings, $request) > > --- > > > function &_processForm($mappings, $request) > > 165a167,169 > > > require_once "forms/$type.php"; > > > } > > > if (!class_exists($type)) { > > 189c193 > > < function _processValidate($actionMapping, $actionForm) > > --- > > > function _processValidate(&$actionMapping, &$actionForm) > > 192c196,197 > > < set_error_handler($this->_options[_ERROR_HANDLER]); > > --- > > > if(!defined('DISABLE_PHRAME_ERROR_HANDLING') || > > DISABLE_PHRAME_ERROR_ > > HANDLING === false) > > > set_error_handler($this->_options[_ERROR_HANDLER]); > > 199c204,205 > > < restore_error_handler(); > > --- > > > if(!defined('DISABLE_PHRAME_ERROR_HANDLING') || > > DISABLE_PHRAME_ERROR_ > > HANDLING === false) > > > restore_error_handler(); > > 210c216 > > < function _processAction($actionMapping, $actionForm) > > --- > > > function &_processAction(&$actionMapping, &$actionForm) > > 216a223,225 > > > require_once "actions/$type.php"; > > > } > > > if (!class_exists($type)) { > > 236c245 > > < function _processForward($actionForward) > > --- > > > function _processForward(&$actionForward) > > 241a251 > > > exit; > > 245a256 > > > exit; > > > > __________________________________ > > Do you Yahoo!? > > The New Yahoo! Search - Faster. Easier. Bingo. > > http://search.yahoo.com > > > > > > ------------------------------------------------------- > > This sf.net email is sponsored by:ThinkGeek > > Welcome to geek heaven. > > http://thinkgeek.com/sf > > _______________________________________________ > > Phrame-devel mailing list > > Phr...@li... > > https://lists.sourceforge.net/lists/listinfo/phrame-devel > > > __________________________________ > Do you Yahoo!? > The New Yahoo! Search - Faster. Easier. Bingo. > http://search.yahoo.com > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Phrame-devel mailing list > Phr...@li... > https://lists.sourceforge.net/lists/listinfo/phrame-devel __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com |