PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 12 ■ ENTERPRISE PATTERNS

addvenue

AddSpace


So here, the addvenue view is associated with the AddVenue command (as set in the Request object’s
cmd parameter). This means that the addvenue.php view will always be included when the AddVenue
command is invoked. Always, that is, unless the status condition is matched. If the AddVenue class sets a
flag of CMD_OK, the default view for the Command is overridden.
The status element could simply contain another view that would be included in place of the
default. Here, though, the forward element comes into play. By forwarding to another command, the
configuration file delegates all responsibility for handling views to the new element.


Parsing the Configuration File


This is a reasonably flexible model for controlling display and command flow logic. The document,
though, is not something that you would want to parse for every single request. You have already seen a
solution to this problem. The ApplicationHelper class provides a mechanism for caching configuration
data.
Here is an extract:


private function getOptions() {
$this->ensure( file_exists( $this->config ),
"Could not find options file" );
$options = @SimpleXml_load_file( $this->config );


// ...set DSN...


$map = new ControllerMap();


foreach ( $options->control->view as $default_view ) {
$stat_str = trim($default_view['status']);
$status = \woo\command\Command::statuses( $stat_str );
$map->addView( 'default', $status, (string)$default_view );
}
// ... more parse code omitted ...
\woo\base\ApplicationRegistry::setControllerMap( $map );
}


Parsing XML, even with the excellent SimpleXML package, is a wordy business and not particularly
challenging, so I leave most of the details out here. The key thing to note is that the getOptions() method
is only invoked if configuration has not been cached into the ApplicationRegistry object.


Storing the Configuration Data


The cached object in question is a ControllerMap. ControllerMap is essentially a wrapper around three
arrays. I could use raw arrays, of course, but ControllerMap gives us the security of knowing that each
array will follow a particular format. Here is the ControllerMap class:


namespace woo\controller;

Free download pdf