[ Team LiB ] Previous Section Next Section

The RequestHelper and DataStore Classes

The RequestHelper class acts like a notice board for our application. Different objects leave messages on it that others might find useful. This is a useful solution to the problem of sharing information between applications and tiers because we allow information to move between objects that might otherwise have no knowledge of one another. Listing 24.2 shows our RequestHelper class.

Listing 24.2 The RequestHelper Class
 1: <?
 2: // controller/RequestHelper.php
 3: // qframe license: http://resources.corrosive.co.uk/pkg/qframe/license.txt
 4:
 5: require 'command/DataStore.php';
 6:
 7: class RequestHelper {
 8:   private $params = array ();
 9:   private $commandArray = array ();
10:   private $datastore;
11:
12:   function RequestHelper( ) {
13:     $this->datastore = DataStore::getInstance();
14:     $this->params = $_REQUEST;
15:   }
16:
17:   function getCommand() {
18:     return $this->params['cmd'];
19:   }
20:
21:   function overrideParams ( $params ) {
22:     $this->params = $params;
23:   }
24:
25:   function getOrigParams() {
26:     return $_REQUEST;
27:   }
28:
29:   function getParams() {
30:     return $this->params;
31:   }
32:
33:   function saveVar( $name, $value ) {
34:     $this->datastore->setVar( $name, $value );
35:   }
36:
37:   function getVar( $name ) {
38:     return $this->datastore->getVar( $name );
39:   }
40:
41:   function getVars() {
42:     return $this->datastore->getVars();
43:   }
44:
45:   function setMessage( $message ) {
46:     $this->datastore->setMessage( $message );
47:   }
48:
49:   function registerCommand( $name, $status ) {
50:    $this->commandArray[] = array( $name, $status );
51:   }
52:
53:   function getCommandArray() {
54:     return $this->commandArray;
55:   }
56: }
57: ?>

As you can see, RequestHelper is really a housekeeper; it does not do anything at all flashy. On line 14 it sets a $params property using the $_REQUEST array and allows user access to this array using the getParams() accessor method on line 29. Why have we put a wrapper around a perfectly good superglobal array? The reason is that we could override $_REQUEST using the overrideParams() method on line 21 if we were testing the framework or running it from the command line.

RequestHelper provides convenient methods that provide access to a DataStore object. The DataStore class enables us to store keys and values and make them available across the entire application. It is the main means by which commands communicate data to the presentation layer. The RequestHelper acquires the application's DataStore object on line 13 and provides the saveVar(), getVar(), and getVars() methods (lines 33–43) to register data with it and get data from it. The setMessage() method on line 45 also works with the DataStore class. It really only calls the DataStore method of the same name and sets a simple message that is made available to the presentation layer.

The RequestHelper class has one more role to play. Every Command object uses the registerCommand() method on line 49 to save its name and return value. The RequestHelper object saves this information to an array, which it makes available on line 53 via the getCommandArray() method. You will later see how the Dispatcher object uses this information to select a view to present to the client.

We have mentioned the DataStore object several times in this section. Listing 24.3 demonstrates it.

Listing 24.3 The DataStore Class
 1: <?php
 2: // command/DataStore.php
 3: // qframe license: http://resources.corrosive.co.uk/pkg/qframe/license.txt
 4:
 5: class DataStore {
 6:   private static $instance;
 7:   private $vars=array();
 8:
 9:   private function __construct() {
10:     // no access
11:   }
12:
13:   public static function getInstance() {
14:     if ( empty ( DataStore::$instance ) ) {
15:       DataStore::$instance = new DataStore();
16:     }
17:     return DataStore::$instance;
18:   }
19:
20:   function setMessage( $msg ) {
21:     $this->vars['message'] = $msg;
22:   }
23:
24:   function getMessage() {
25:     return $this->vars['message'];
26:   }
27:
28:   function setVar( $name, $value ) {
29:     $this->vars[$name] = $value;
30:   }
31:
32:   function getVar( $name ) {
33:     return $this->vars[$name];
34:   }
35:
36:   function getVars() {
37:     return $this->vars;
38:   }
39: }
40:
41: ?>

As you can see, the DataStore class does little more than provide an interface for storing and retrieving keys and values. For good or ill, we take full advantage of PHP's loose typing and allow client coders to store data of any type in the class.

The real reason we need this class as well as the RequestHelper class lies on line 9. Notice that we have declared our constructor private, meaning that no one can directly instantiate a DataStore object. We provide a private static property called $instance that stores a DataStore instance for us. We also provide a class-level mechanism by which client code can gain access to a single instance of the DataStore class in the static getInstance() method on line 13. Because it is a static method, getInstance() is called using the class, not via a DataStore object. If the $instance property is empty, as it is the first time DataStore::getInstance() is called, the method instantiates a new DataStore object and stores it in the property. Then the property is returned. A DataStore object is therefore always accessible anywhere in the framework by calling DataStore::getInstance(). Most importantly, it also means that only one DataStore object exists and that all code works with the same one. So, a command can save some data at one point during execution and a view can later access the same data by acquiring the single DataStore instance that the system allows. This is known as the Singleton pattern.

We have now covered the two principle conduits for messages in our system. Refer to Listing 24.1 to remind yourself of the RequestHelper class's first appearance on line 18; then let's move on to the CommandFactory class.

    [ Team LiB ] Previous Section Next Section