Class midcom_baseclasses_components_request

Description

Base 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:

  • mixed fixed_args: This is either a string or an array and defines the fixed arguments that have to be present at the beginning of the URL to be handeld. A string denotes a single argument, an array is used if more then one fixed argument is needed. If you do not have any fixed arguments, set this parameter to null, which is the default.
  • int variable_args: Usually, there are a number of variables in the URL, like article IDs, or article names. This can be 0, indicating that no variable arguments are required, which is the default.
  • bool no_cache: For those cases where you want to prevent a certain "type" of request being cached. Set to false by default.
  • int expires: Set the default expiration time of a given type of request. The default -1 is used to indicate no expiration setting. Any positive integer will cause its value to be passed to the caching engine, indicating the expiration time in seconds.
  • mixed handler: This is a definition of what method should be invoked to handle the request. You have two options here. First you can refer to a method of this request handler class, in that case you just supply the name of the method. Alternativly, you can refer to an external class for request processing using an array syntax. The first array member must either contain the name of a existing class or a reference to an already instantinated class. This value has no default and must be set. The actual methods called will have either an _handle_ or _show_ prefixed to the exec_handler value, respecitvly. See below for automatic handler instances, the preferred way to set things up.
Example:

  1. <?php
  2. $this->_request_switch[] = Array
  3. (
  4. 'fixed_args' => Array ('registratons', 'view'),
  5. 'variable_args' => 1,
  6. 'no_cache' => false,
  7. 'expires' => -1,
  8. 'handler' => 'view_registration'
  9. //
  10. // Alternative, use a class with automatic instantination:
  11. // 'handler' => Array('net_nemein_registrations_regadmin', 'view')
  12. //
  13. // Alternative, use existing class (first parameter must be a reference):
  14. // 'handler' => Array(&$regadmin, 'view')
  15. );
  16. ?>

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

  1. <?php
  2. /**
  3. * can_handle example, with Docblock:
  4. * @param mixed $handler_id The ID of the handler.
  5. * @param Array $args The argument list.
  6. * @param Array $data The local request data.
  7. * @return bool True if the request can be handled, false otherwise.
  8. */
  9. function _can_handle_xxx ($handler_id, $args, &$data) {}
  10.  
  11. /**
  12. * Exec handler example, with Docblock:
  13. * @param mixed $handler_id The ID of the handler.
  14. * @param Array $args The argument list.
  15. * @param Array $data The local request data.
  16. * @return bool Indicating success.
  17. */
  18. function _handler_xxx ($handler_id, $args, &$data) {}
  19.  
  20. /**
  21. * Show handler example, with Docblock:
  22. * @param mixed $handler_id The ID of the handler.
  23. * @param Array $data The local request data.
  24. * @return bool Indicating success.
  25. */
  26. function _show_xxx ($handler_id, &$data) {}
  27. ?>

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:

  1. <?php
  2. // Bind the view data, remember the reference assignment:
  3. $view =& $GLOBALS['midcom']->get_custom_context_data('request_data');
  4. ?>

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:

  1. <?php
  2. class my_handler extends midcom_baseclasses_components_handler
  3. {
  4. function my_handler()
  5. {
  6. // just call the base class constructor, avoid
  7. // additional code at this point.
  8. parent::midcom_baseclasses_components_handler();
  9. }
  10.  
  11. function _on_initialize()
  12. {
  13. // Add class initialization code here, all members have
  14. // been prepared, and the instance is already stable, so
  15. // you can safely work with references to $this here.
  16. }
  17. }
  18. ?>

The two methods for each handler have the same signature as if they were in the same class.

  • todo: Exec Handler Baseclass
  • todo: Automatic exec handler instantination
  • todo: AIS Handler Subclass

Located in /midcom/baseclasses/components/request.php (line 208)


	
			
Direct descendents
Class Description
 class midcom_baseclasses_components_request_admin Base class to encaspulate a admin request to the component, instantinated by the MidCOM component interface.
 class midcom_helper_helloworld_viewer MidCOM Hello World site interface class.
 class midcom_helper_search_viewer MidCOM Indexer Front-End, Viewer Class
 class net_nemein_calendar_viewer Calendar Viewer interface class.
 class net_nemein_hourview_viewer Net.nemein.hourview site interface class.
 class no_odindata_quickform_viewer Request class for handling simple topics and articles.
Variable Summary
Method Summary
 midcom_baseclasses_components_request midcom_baseclasses_components_request (MidgardTopic $topic, midcom_helper_configuration $config)
 bool can_handle (int $argc, Array $argv)
 Array get_metadata ()
 bool handle (int $argc, Array $argv)
 void initialize (string $component)
 void show ()
 bool _on_handle (mixed $handler, Array $args, Array &$data)
 void _on_initialize ()
 bool _on_show (mixed $handler)
Variables
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.

  • access: protected
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.

  • access: protected
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.

  • access: protected
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.

  • access: private
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.

  • access: protected
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.

  • access: protected
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.

  • access: protected
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.

  • access: protected
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.

  • access: protected
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.

  • access: protected
Methods
Constructor midcom_baseclasses_components_request (line 344)

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.

midcom_baseclasses_components_request midcom_baseclasses_components_request (MidgardTopic $topic, midcom_helper_configuration $config)
can_handle (line 434)

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.

  • return: Indicating wether the request can be handled by the class, or not.
bool can_handle (int $argc, Array $argv)
  • int $argc: The argument count
  • Array $argv: The argument list
get_metadata (line 587)

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.

Array get_metadata ()

Redefined in descendants as:
handle (line 512)

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.

  • return: Indicating wether the request was handled successfully.
  • see: _on_handle();
bool handle (int $argc, Array $argv)
  • int $argc: The argument count
  • Array $argv: The argument list
initialize (line 357)

Initializes the request handler class, called by the component interface after instantination. Required to allow safe $this references during startup.

void initialize (string $component)
  • string $component: The name of the component.
show (line 615)

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.

  • see: _on_show();
void show ()
_on_get_metadata (line 689)

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.

  • return: The metadata object of the currently displayed object.
  • deprecated: in MidCOM 2.4
midcom_helper_metadata &_on_get_metadata ()
_on_handle (line 674)

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.

  • return: Return false to abort the handle phase, true to continue normally.
bool _on_handle (mixed $handler, Array $args, Array &$data)
  • mixed $handler: The ID (Array-Key) of the handler that is responsible to handle the request.
  • Array $args: The argument list.
  • Array $data: A reference to the local request data.

Redefined in descendants as:
_on_initialize (line 650)

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.

void _on_initialize ()

Redefined in descendants as:
_on_show (line 703)

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.

  • return: Return false to override the regular component output.
bool _on_show (mixed $handler)
  • mixed $handler: The ID (Array-Key) of the handler that is responsible to handle the request.
_prepare_handler (line 551)

Helper function, which prepares the handler callback for execution.

This will create the handler class instance if required.

  • access: private
void _prepare_handler ()
_prepare_request_switch (line 383)

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.

void _prepare_request_switch ()

Documentation generated on Mon, 21 Nov 2005 18:21:16 +0100 by phpDocumentor 1.3.0RC3