Changeset 28

Show
Ignore:
Timestamp:
02/09/08 18:23:43 (5 years ago)
Author:
tobias382
Message:

Modified Command to intelligently detect and pass the intercepted event
instance only if the command method to be called requires it

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/Phergie/Event/Handler/Command.php

    r24 r28  
    1515    protected function processCommand($message, $event) 
    1616    { 
    17         $arguments = preg_split('/[ ]+/', trim($message)); 
    18         $command = 'onDo' . ucfirst(strtolower(array_shift($arguments))); 
    19         $arguments[] = $event; 
     17        preg_match('/^\S+/', $message, $match); 
     18        $command = 'onDo' . ucfirst(strtolower($match[0])); 
     19 
    2020        if (method_exists($this, $command)) { 
    21             call_user_func_array(array($this, $command), $arguments); 
     21            $method = new ReflectionMethod($this, $command); 
     22            $expected = $method->getNumberOfParameters(); 
     23            $params = preg_split('/\s+/', $message, $expected + 1);  
     24            array_shift($params); 
     25            foreach ($method->getParameters() as $key => $param) { 
     26                if ($param->getClass()->getName() == 'Phergie_Event_Request') { 
     27                    array_splice($params, $key, 0, array($event)); 
     28                    break; 
     29                } 
     30            } 
     31            if (count($params) == $expected) { 
     32                call_user_func_array(array($this, $command), $params); 
     33            } 
    2234        } 
    2335    }