gnope php gtk gui gnope.org  

basic command processor

 
Post new topic   Reply to topic    gnope.org Forum Index -> Chatterbox
View previous topic :: View next topic  
Author Message
leon_pegg



Joined: 12 Jun 2006
Posts: 113
Location: hemel / UK

PostPosted: Tue Sep 05, 2006 4:16 pm    Post subject: basic command processor Reply with quote

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(){
      
   }
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    gnope.org Forum Index -> Chatterbox All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Templates enhanced by DigiWiki. Hosting by Tradebit.