Quote Originally Posted by arborint
How to we add the state logic and possibly dispatch to the Input Controller? I am beginning to think that plug-in state machines (similar in concept/goals the plug-in Mappers in the Front Controller) is the way to go. That decouples the logic of the page moving it out of the housekeeping and structure code. The goal is that you can write a Controller with any behavior by changing just the state machine with the goal of not having the maximum amount of code outside the state machine. We could supply standard single form and wizard (multi-form sequence) state machines (and the requsite RoR emulator ).
That quote had me tinkering for a while, and here is what I came up with;

Basically, filters are rather trivial so let's forget about them and focus on rules and state.
I figured that the pivot of the ApplicationController is to maintain state. Different states would result in different Handlers. This is in contrast to the FrontController which doesn't rely on state to choose a handler, but rather on the Request. Besides this, the ApplicationController would also need to be able to change state. And the need for changing state could be described through rules.

So, the ApplicationController would have an array of Validators. When the ApplicationController is executed, it would run through the array and execute the Validators one by one until a Validator fails, or the array is empty. Each time a validator succeeds, the state is updated to reflect the new level.
This is quite flexible in that allows for any number of states (multiform wizards for example).

Of course I couldn't resist the urge to actually implement it, so :
PHP Code:
/**
  * @implements IHandler
  */
class ApplicationController
{
    var 
$state 'init';
    var 
$params;
    var 
$filter;
    var 
$validators = Array();

    function 
ApplicationController() {
        
$this->params =& new DataSpace();
        
$this->filter =& new FilterChain();
    }

    function 
addState($name, &$validator) {
        
$this->validators[$name] =& $validator;
    }

    
/**
      * @returns   void
      */
    
function execute(&$request, &$response) {
        if (
$request->method == 'POST') {
            
$this->params->import($this->filter->process($request));
            
$this->state 'submit';
        }

        foreach (
array_keys($this->validators) as $state) {
            if (
$this->validators[$state]->validate($this->params)) {
                
$this->state $state;
            } else {
                break;
            }
        }

        
$handler =& $this->getHandler($this->state);
        
$handler->execute($this->params$response);
    }

    function & 
getHandler($state) {
        
trigger_error("abstract method");
    }
// end class ApplicationController 
PHP Code:
class MyFormController extends ApplicationController
{
    function 
MyFormController() {
        
parent::ApplicationController();
        
$validator =& new Validator();
        
$validator->addRule(new RequiredFieldRule("foo"));
        
$this->addState('valid'$validator);
    }

    function & 
getHandler($state) {
        switch (
$state) {
            case 
'init' : return new ServerPage("forminit");
            case 
'submit' : return new ServerPage("forminvalid");
            case 
'valid' : return new ServerPage("formdone");
            default :
                
trigger_error("Unknown state '$state'");
        }
    }

// end class MyFormController