PHPによる動的エージェントの実現


          ,              ,   :
class Retry {

    /**
     * @var object     
     */
    private $__proxy;

    /**
     * @var int     
     */
    private $__retry_times = 1;

    /**
     * @var int     
     */
    private $__delay_time = 1000000;

    /**
     * @param $name
     * @param $args
     * @return mixed
     * @throws Exception
     */
    public function __call($name, $args){

        if(!method_exists($this->get_proxy(), $name)){
            throw new Exception('cannot found method.');
        }

        $times = 0;

        $reflection = new ReflectionClass($this->get_proxy());
        $method = $reflection->getMethod($name);

        if (!$method->isPublic() || $method->isAbstract()) {
            throw new Exception('method is not public or is abstract.');
        }

        while ($times < $this->get_retry_times()){
            try{
                $res = $method->invokeArgs($this->get_proxy(), $args);
                return $res;
            }catch (Exception $e){
                $times++;
                if($times >= $this->__retry_times){
                    throw $e;
                }
                usleep($this->get_delay_microseconds());
            }
        }
    }

    public function set_proxy($real_subject){
        $this->__proxy = $real_subject;
    }

    public function get_proxy(){
        return $this->__proxy;
    }

    public function set_retry_times($retry_times){
        $this->__retry_times = $retry_times;
    }

    public function get_retry_times(){
        return $this->__retry_times;
    }

    public function set_delay_microseconds($delay_time){
        $this->__delay_time = $delay_time;
    }

    public function get_delay_microseconds(){
        return $this->__delay_time;
    }
}
         invokeArgs  ,           invoke,       。