midcom_baseclasses_components_requestBase class to encaspulate a request to the component, instantinated by the MidCOM component interface.
It provides an automatic mechanism for URL processing and validation, minimizing the required work to get a new component running.
Request switch configuration
The class uses an array which aids in URL-to-function mapping. Handlers are distinguished by the "URL-space" they handle. For each handler two functions are needed, one for the request handle decision ("Can Handle Phase"), one for the request handling ("Handle Phase") and one for output ("Output Phase"). These handlers can either be contained in this class or refer to another class which gets instantinated, if neccesary.
All request handlers are contained in a single array, whose keys identify the various switch configurations. These identifiers are only for informational purposes (they appear in the debug log), so you could just resort to automatic array index numbering using the [] operator.
Each request handler definition in the switch must contain these key/value pairs:
- <?php
- $this->_request_switch[] = Array
- (
- 'fixed_args' => Array ('registratons', 'view'),
- 'variable_args' => 1,
- 'no_cache' => false,
- 'expires' => -1,
- 'handler' => 'view_registration'
- //
- // Alternative, use a class with automatic instantination:
- // 'handler' => Array('net_nemein_registrations_regadmin', 'view')
- //
- // Alternative, use existing class (first parameter must be a reference):
- // 'handler' => Array(&$regadmin, 'view')
- );
- ?>
This definition is usually located in either in the _on_initialize event handler (preferred) or the subclass' constructor (discouraged, as you can't use references to $this safely there).
The handlers are processed in the order which they have been added to the array. This has several implications:
First, if you have two handlers with similar signatures, the latter might be hidden by the former, for example the handler 'view' with two variable arguments includes the urls that could match 'view','registration' with a single variable argument if processed in this order. In these cases you have to add the most specific handlers first.
Second, for performance reasons, you should try to add the handler which will be accessed most of the time first (unless it conflicts with the first rule above), as this will speed up average request processing.
Subclasses may add additional configuration data to the handler declarations, this is done, for example by the config_dm handler defined in the request_admin subclass. They must only be used to configure predefined requests, you should refer to the documentation of these handlers for details.
Callback method signatures
- <?php
- /**
- * can_handle example, with Docblock:
- * @param mixed $handler_id The ID of the handler.
- * @param Array $args The argument list.
- * @param Array $data The local request data.
- * @return bool True if the request can be handled, false otherwise.
- */
- function _can_handle_xxx ($handler_id, $args, &$data) {}
- /**
- * Exec handler example, with Docblock:
- * @param mixed $handler_id The ID of the handler.
- * @param Array $args The argument list.
- * @param Array $data The local request data.
- * @return bool Indicating success.
- */
- function _handler_xxx ($handler_id, $args, &$data) {}
- /**
- * Show handler example, with Docblock:
- * @param mixed $handler_id The ID of the handler.
- * @param Array $data The local request data.
- * @return bool Indicating success.
- */
- function _show_xxx ($handler_id, &$data) {}
- ?>
The three callbacks match the regular processing sequence of MidCOM.
_can_handle_xxx notes: For ease of use, the _can_handle_xxx callback is optional, it will only be called if the method actually exists. Normally you want to override this only if you request handler can hide stuff which is not under the control of your topic. A prominent example is a hander definition which has only a single variable argument. It would hide all subtopics if you don't check what objects actually belong to you, and what not.
The main callbacks _handle_xxx and _show_xxx are mandatory.
As you can see, the system provides you with an easy way to keep track of the data of your request, without having dozens of members for trivial flags. This data array is automatically registered in the custom component context under the name 'request_data', making it easily available within style elements avoiding the problems of the view_* globals, this is the new recommended way of passing information to the style elements:
- <?php
- // Bind the view data, remember the reference assignment:
- $view =& $GLOBALS['midcom']->get_custom_context_data('request_data');
- ?>
The data array can also be accessed by using the $_request_data member of this class, which is the orignal data storage location for the request data.
Note, that the request data, for ease of use, already contains references to the L10n Databases of the Component and MidCOM itself located in this class. They are stored as 'l10n' and 'l10n_midcom'. Also availbale as 'config' is the current component configuratio and 'topic' will hold the current conten topic.
For those asking about "avoiding the problems of the view_* globals", this basically breaks down to the fact that multiple components use these variables simultaneously. If you invoke a dynamic_load within a style elemnt, you have good chances, that after it your original global $view variables have been overwritten by the style code of the dynamically loaded component.
Automatic handler class instantination
If you specify a class name instead of a class isntance as a exec handler, MidCOM will automatically create an instance of that class type and initialize it. These so-called handler classes must be a subclass of midcom_baseclasses_components_handler.
The subclasses you create should look about this:
- <?php
- class my_handler extends midcom_baseclasses_components_handler
- {
- function my_handler()
- {
- // just call the base class constructor, avoid
- // additional code at this point.
- parent::midcom_baseclasses_components_handler();
- }
- function _on_initialize()
- {
- // Add class initialization code here, all members have
- // been prepared, and the instance is already stable, so
- // you can safely work with references to $this here.
- }
- }
- ?>
The two methods for each handler have the same signature as if they were in the same class.
Located in /midcom/baseclasses/components/request.php (line 208)
| Class | Description |
|---|---|
midcom_baseclasses_components_request_admin
|
Base class to encaspulate a admin request to the component, instantinated by the MidCOM component interface. |
midcom_helper_helloworld_viewer
|
MidCOM Hello World site interface class. |
midcom_helper_search_viewer
|
MidCOM Indexer Front-End, Viewer Class |
net_nemein_calendar_viewer
|
Calendar Viewer interface class. |
net_nemein_hourview_viewer
|
Net.nemein.hourview site interface class. |
no_odindata_quickform_viewer
|
Request class for handling simple topics and articles. |
midcom_baseclasses_components_request
midcom_baseclasses_components_request
(MidgardTopic $topic, midcom_helper_configuration $config)
int
$errcode
= MIDCOM_ERROK (line 290)
The last error code of the component. Set this when the request handling fails.
Error state variable.
string
$errstr
= '' (line 300)
The last error message of the component. Set this when the request handling fails.
Error state variable.
string
$_component
= '' (line 273)
Internal helper, holds the name of the component. Should be used whenever the components' name is required instead of hardcoding it.
Request state variable, set during startup. There should be no need to change it in most cases.
Array
$_component_data
= null (line 257)
Component data storage area.
Request state variable, set during startup. There should be no need to change it in most cases.
midcom_helper_configuration
$_config
= null (line 229)
The current configuration.
Request state variable, set during startup. There should be no need to change it in most cases.
Array
$_handler
= null (line 330)
This is a reference to the handler which declared to be able to handle the request. The array will contain the original index of the handler in the 'id' member for backtracking purposes. The variable argument list will be placed into 'args' for performance reasons.
Internal request handling state variable, you should not use this directly.
midcom_helper_services_i18n
$_i18n
= null (line 236)
A handle to the i18n service.
Request state variable, set during startup. There should be no need to change it in most cases.
midcom_services__i18n_l10n
$_l10n
= null (line 243)
The components' L10n string database
Request state variable, set during startup. There should be no need to change it in most cases.
midcom_services__i18n_l10n
$_l10n_midcom
= null (line 250)
The global MidCOM string database
Request state variable, set during startup. There should be no need to change it in most cases.
Array
$_request_data
= array() (line 265)
Request specific data storage area. Registered in the component context as ''.
Request state variable, set during startup. There should be no need to change it in most cases.
Array
$_request_switch
= array() (line 314)
Request execution switch configuration.
The main request switch data. You need to set this during construction, it will be post-processed afterwards during initialize to provide a unified set of data. Therefore you must not modify this switch after construction.
MidgardTopic
$_topic
= null (line 222)
The topic for which we are handling a requiest.
Request state variable, set during startup. There should be no need to change it in most cases.
Initializes the class, only basic variable assignement. Your own constructor should call this function first.
Note, that it is recommended to put all further initialization work into the _on_initialize event handler.
CAN_HANDLE Phase interface, checks against all registered handlers if a vaild one can be found. You should not need to override this, instead, use the HANDLE Phase for further checks.
If available, the function calls the _can_handle callback of the eventhandlers which potentially match the argument declaration.
Returns the metadata of the currently selected object. It uses a callback to determine a metadata object which contains the neccessary information.
This interface function is no longer in use in MidCOM 2.4 upwards. Instead, the framework accesses the metadata information directly.
This method handles the request using the handler determined by the can_handle check.
Before doing anything, it will call the _on_handle event handler to allow for generic request preparation.
Initializes the request handler class, called by the component interface after instantination. Required to allow safe $this references during startup.
Display the content, it uses the handler as determined by can_handle.
Before doing anything, it will call the _on_showent handler to allow for generic preparation. If this function returns false, the regular output handler will not be called.
Returns a metadata object for the currently selected object, or null if no Metadata is available. The default implementation defaults to the topics' metadata object.
Event Handler callback.
This event handler is no longer in use in MidCOM 2.4 upwards. Instead, the framework accesses the metadata information directly.
Component specific initialization code for the handle phase. AIS, for example, uses this to preapre the toolbar arrays. The name of the request handler is passed as an argumet to the event handler.
Event Handler callback.
If you discover that you cannot handle the request already at this stage, return false and set the error variables accordingly. The reminder of the handle phase is skipped then, returning immediately to MidCOM.
Note, that while you have the complete information around the request (handler id, args and request data) available, it is strongly discouraged to handle everything here. Instead, stay with the specific request handler methods as far as sensible.
Initialization event handler, called at the end of the initialization process immediately before the request handler configuration is read.
Event Handler callback.
Use this function instead of the constructor for all initialization work, as it makes your life much easier with references to $this being available. You can safely populate the request switch from here.
Generic output initialization code. The return value lets you control wether the output method accociated with the handler declaration is called, return false to override this automatism, true, the default, will call the output handler normally.
Event Handler callback.
Helper function, which prepares the handler callback for execution.
This will create the handler class instance if required.
This private helper post-processes the initial information as set by the constructor.
It fills all missing fields with sensible defaults, see the class introdction for deatils.
Documentation generated on Mon, 21 Nov 2005 18:21:16 +0100 by phpDocumentor 1.3.0RC3