leon_pegg
Joined: 12 Jun 2006 Posts: 113 Location: hemel / UK
|
Posted: Tue Sep 05, 2006 4:16 pm Post subject: basic command processor |
|
|
i wrote this as an alternative to the pear command processor
hope someone will find it useful.
| Code: |
class cmd_process{
var $commands = array();
var $hooks = array();
function cmd_exist($tag){
if (isset($this->commands[$tag])) {
return true;
}else{
return false;
}
}
function add_cmd($tag, $function, $has_value = false){
$this->commands[$tag] = $has_value;
$this->hook_cmd($tag,$function);
}
function hook_cmd($tag, $function){
$this->hooks[$tag] = $function;
}
function debug_hook($tag){
call_user_func($this->hooks[$tag]);
}
function process($arguments){
$i = 0;
$get_value = false;
$last_tag = null;
$tags = null;
$return = null;
foreach ($arguments as $value){
if ($i !== 0){
if (!$get_value) {
$last_tag = $value;
if (isset($this->commands[$value])) {
print_r($tags);
$tags[$value] = null;
$get_value =$this->commands[$value];
}
} else {
$tags[$last_tag] = $value;
$get_value = false;
}
}
$i++;
}
if ($tags !== null) {
foreach ($tags as $tag => $value){
if (isset($this->hooks[$tag])) {
call_user_func($this->hooks[$tag],$value);
}
}
} else {
$tags = array();
$this->nocommand();
}
foreach ($tags as $key => $value){
if ($value !== null) {
$return[$key] = $value;
}
}
return $return;
}
function nocommand(){
}
}
|
|
|