开发者

How to ignore/skip functions and methods when using Reflection in Zend

开发者 https://www.devze.com 2023-03-29 04:24 出处:网络
I am building a multiprotocol webservice using Zend_JSON_Server and Zend_XmlRpc_Server using PHP 5.2.6. and I am currently facing the problem, that the server is now announcing every single method fro

I am building a multiprotocol webservice using Zend_JSON_Server and Zend_XmlRpc_Server using PHP 5.2.6. and I am currently facing the problem, that the server is now announcing every single method from my classes and its parents.

class MyClass extends MyInterface {
  /**
   * To be published and accessible
   **/
  public func开发者_Python百科tion externalUse() {}
}

class MyInterface {
  /**
   * This method should not be published using the webservice,
   * but needs to be of type "public" for internal use
   * @access private
   * @ignore
   **/
   public function internalUseOnly() {}
}

$server = new Zend_Json_Server();
$server->setClass('MyClass');

// show service map
header('Content-Type: text/plain');
$server->setTarget('/service.json')
       ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);

$smd = $server->getServiceMap();
die($smd);

This code will also announce internalUseOnly as an accessible function using the webservice:

{ "SMDVersion" : "2.0",
  "contentType" : "application/json",
  "envelope" : "JSON-RPC-2.0",
  "methods" : { "externalUse" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        },
      "internalUseOnly" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        }
    },
  "services" : { "externalUse" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        },
      "internalUseOnly" : { "envelope" : "JSON-RPC-2.0",
          "parameters" : [  ],
          "returns" : "null",
          "transport" : "POST"
        }
    },
  "target" : "/service.json",
  "transport" : "POST"
}

As you can see I already tried @ignore and @access private, but both are ignored by Reflection. Is there any other option to remove such functions from my service map?

Thanks in advance!


Update

One solution I found is to make private / protected methods accessible again using overloading and dynamic function calls. But as call_user_func is not known to be one of the fasted methods around, this is only meant to be a hotfix.

class MyInterface {
    public function __call($method, $params) {
        $method = '_' . $method;
        if (method_exists($this, $method)) {
            call_user_func(array($this, $method), $params);
        }
    }

  /**
   * This method should not be published using the webservice,
   * but needs to be of type "public" for internal use
   * @ignore
   * @access private
   * @internal
   **/
  protected function _internalUseOnly() { die('internal use only!'); }
}


Prefix your methods with __. Zend\Server\Reflection\ReflectionClass ignore methods starting by __. At least in ZF2.

0

精彩评论

暂无评论...
验证码 换一张
取 消