Here is my go at an event register/execution environment:
This class executes events:
<?php
/*
* Application
* The controller to execute requests
*/
class Application
{
/* the name of the app */
var $name;
/* the datasource (at the moment only a DB) */
var $data_source;
/*
* Constructor:
* takes the name and a reference to a datasource
*/
function Application($name, &$data_source)
{
$this->name = $name;
$this->data_source =& $data_source;
}
/*
* execute(&$request)
* execute the event handler
*/
function execute(&$request)
{
foreach($request->get_handlers() as $event => $file)
{
$this->fire($event, $file);
}
}
/*
* fire($event, $file)
* fire an event
*/
function fire($event, $file)
{
require_once($file);
$handler =& new $event($this->data_source, $this);
$handler->execute()
}
}
?>
This class encapsulate a request. You can sub-class it to work with a specific request ie. $_GET, $_POST, XML-RPC etc
<?php
/*
* Request
* This class is the object that handles
* the verifying and parsing of request
* params(ie. $_REQUEST, $_GET etc)
*/
class Request
{
/* Handlers to be executed */
var $handlers;
/* the request params - associative array */
var $params;
/* the map file */
var $file;
/*
A map file:
param value Class file -- don't include this line
page index IndexEvent /Users/trickie/class.IndexEvent.php
page links LinksEvent /Users/trickie/class.LinksEvent.php
*/
/*
* Constructor:
* takes the full path to a
* event -> handler map file
*/
function Request($event_map_file, $params)
{
$this->handlers = array();
$this->file = $event_map_file;
$this->params = $params
}
/*
* get_handlers()
* returns the array of handler names
* to be executed.
*/
function get_handlers()
{
return $this->handlers;
}
/*
* map_handlers()
* parse the request params
* and use the file to map
* request params to handlers
*/
function map_handlers()
{
die('not implemented');
}
/*
* verify_request(&$security_object)
* secure the session, verify input etc
*/
function verify_request(&$security_object)
{
return $security_object->verify($this);
}
/*
* get_param($key)
* returns the value of $key
* if $key doesn't exist, return false
*/
function get_param($key)
{
if(array_key_exists($key, $this->params))
{
return $this->params[$key];
}
return false;
}
}
?>
This class encapsulates an event. Sub-class it to make specific event handlers:
<?php
/*
* Event
* This is the event handler that does a specific
* action. It is self contained. A base class.
*
* TODO:
* improve mechanism to fire new events/request
*/
class Event
{
/* The data source (ie DB) */
var $data_source;
/* The application object */
var $app;
/*
* Constructor:
* Takes the data source
*/
function Event(&$data_source, &$app)
{
$this->data_source =& $data_source;
$this->app =& $app;
}
/*
* execute()
* execute the event handler
*/
function execute()
{
die('not implmemented');
}
/*
* fire(&$request)
* fire a new request
*/
function fire($event, $file)
{
$this->app->fire($event, $file);
}
}
?>
This class is the skeleton of a security class. This is where you could check authenticate users etc:
<?php
class Security
{
function Security()
{
}
function verify(&$request)
{
}
}
?>
And finally here is an example of how you would execute events:
<?php
// require files etc
$data_source = //create the data source ie. DB
$app = new Application('New App', $data_source);
$security = new Security();
// $request_one will do stuff based on $_GET variables,
// and if necessary fire the execution of a POSTRequest
// in other words nesting events.
$request_one = new GETRequest('/Users/trickie/map1.inc.php', $_GET);
if($request_one->verify_request())
{
$request_one->map_handlers();
$app->execute($request_one);
}
?>
It is all a bit rough and ready but have a look and let me know what you think.