/** * Add an RPC method * * @param \Comodojo\RpcServer\RpcMethod $method * * @return bool */ public final function add(\Comodojo\RpcServer\RpcMethod $method) { $name = $method->getName(); if (array_key_exists($name, $this->rpc_methods)) { $this->logger->warning("Cannot add method " . $name . ": duplicate entry"); return false; } else { $this->rpc_methods[$name] = $method; $this->logger->info("Added method " . $name); return true; } }
public function testMethods() { $logger = new \Comodojo\RpcServer\Util\NullLogger(); $met = new \Comodojo\RpcServer\Component\Methods($logger); $one = \Comodojo\RpcServer\RpcMethod::create("test.one", function ($params) { return $params->get(); })->setDescription("Test Method One")->setReturnType('struct'); $two = \Comodojo\RpcServer\RpcMethod::create("test.two", function ($params) { return $params->get(); })->setDescription("Test Method Two")->setReturnType('struct'); $add = $met->add($one); $this->assertTrue($add); $add = $met->add($two); $this->assertTrue($add); $add = $met->add($two); $this->assertFalse($add); $get = $met->get('test.one'); $this->assertInstanceOf('\\Comodojo\\RpcServer\\RpcMethod', $get); $get = $met->get(); $this->assertInternalType('array', $get); $this->assertCount(2, $get); $this->assertInstanceOf('\\Comodojo\\RpcServer\\RpcMethod', $get['test.one']); $this->assertInstanceOf('\\Comodojo\\RpcServer\\RpcMethod', $get['test.two']); $delete = $met->delete('test.one'); $this->assertTrue($delete); $delete = $met->delete('test.bla'); $this->assertFalse($delete); $get = $met->get(); $this->assertInternalType('array', $get); $this->assertCount(1, $get); }
public function testNewMethod() { $method = \Comodojo\RpcServer\RpcMethod::create("test.sum", function ($params) { $a = $params->get('a'); $b = $params->get('b'); return is_null($a) || is_null($b) ? '42' : $a + $b; })->setDescription("Sum two integers")->setReturnType('string')->addSignature()->addParameter('int', 'a')->addParameter('int', 'b')->setReturnType('int'); $this->assertInstanceOf('\\Comodojo\\RpcServer\\RpcMethod', $method); $this->assertEquals("test.sum", $method->getName()); $this->assertEquals("Sum two integers", $method->getDescription()); $this->assertInstanceOf('Closure', $method->getCallback()); $this->assertNull($method->getMethod()); $signatures = $method->getSignatures(); $this->assertInternalType('array', $signatures); $this->assertCount(2, $signatures); $this->assertCount(1, $signatures[0]); $this->assertCount(3, $signatures[1]); $parameters = $method->getParameters(); $this->assertInternalType('array', $parameters); $this->assertArrayHasKey("a", $parameters); $this->assertArrayHasKey("b", $parameters); $parameters = $method->getParameters('NUMERIC'); $this->assertCount(2, $parameters); $this->assertEquals('int', $method->getReturnType()); $method->deleteParameter('b'); $parameters = $method->getParameters('NUMERIC'); $this->assertCount(1, $parameters); $method->selectSignature(0); $this->assertEquals('string', $method->getReturnType()); $signature = $method->getSignature(); $this->assertCount(1, $signature); $method->deleteSignature(1); $signatures = $method->getSignatures(); $this->assertCount(1, $signatures); }
protected function setUp() { $this->server = new RpcServer(RpcServer::XMLRPC); $method = RpcMethod::create("test.sum", function ($params) { $a = $params->get('a'); $b = $params->get('b'); return is_null($a) || is_null($b) ? 42 : intval($a) + intval($b); })->setDescription("Sum two integers")->setReturnType('int')->addParameter('int', 'a')->addParameter('int', 'b')->addSignature()->setReturnType('int'); $this->server->methods()->add($method); }
public function testEqualLengthMultipleSignatureMethod() { $method = \Comodojo\RpcServer\RpcMethod::create("test.multisignature", function ($params) { $a = $params->get('a'); return is_int($a) ? 'integer' : 'string'; })->setDescription("Test multiple, equal lenght, signatures")->setReturnType('string')->addParameter('int', 'a')->addSignature()->setReturnType('string')->addParameter('string', 'a'); $result = $this->server->methods()->add($method); $this->assertTrue($result); $request = $this->encodeRequest('test.multisignature', array(2)); $result = $this->server->setPayload($request)->serve(); $decoded = $this->decodeResponse($result); $this->assertEquals('integer', $decoded); $request = $this->encodeRequest('test.multisignature', array('test')); $result = $this->server->setPayload($request)->serve(); $decoded = $this->decodeResponse($result); $this->assertEquals('string', $decoded); }
/** * Check if a request is consistent (i.e. if it matches one of method's signatures) * * @param \Comodojo\RpcServer\RpcMethod $registered_method * @param array $parameters * * @return int * @throws \Comodojo\Exception\RpcException */ private function checkRequestConsistence($registered_method, $parameters) { $signatures = $registered_method->getSignatures(false); foreach ($signatures as $num => $signature) { if (self::checkSignatureMatch($parameters, $signature["PARAMETERS"]) === true) { return $num; } } throw new RpcException("Invalid params", -32602); }
/** * Inject introspection and reserved RPC methods * * @param \Comodojo\RpcServer\Component\Methods $methods */ private static function setIntrospectionMethods($methods) { $methods->add(RpcMethod::create("system.getCapabilities", "\\Comodojo\\RpcServer\\Reserved\\GetCapabilities", "execute")->setDescription("This method lists all the capabilites that the RPC server has: the (more or less standard) extensions to the RPC spec that it adheres to")->setReturnType('struct')); $methods->add(RpcMethod::create("system.listMethods", "\\Comodojo\\RpcServer\\Introspection\\ListMethods", "execute")->setDescription("This method lists all the methods that the RPC server knows how to dispatch")->setReturnType('array')); $methods->add(RpcMethod::create("system.methodHelp", "\\Comodojo\\RpcServer\\Introspection\\MethodHelp", "execute")->setDescription("Returns help text if defined for the method passed, otherwise returns an empty string")->setReturnType('string')->addParameter('string', 'method')); $methods->add(RpcMethod::create("system.methodSignature", "\\Comodojo\\RpcServer\\Introspection\\MethodSignature", "execute")->setDescription("Returns an array of known signatures (an array of arrays) for the method name passed." . "If no signatures are known, returns a none-array (test for type != array to detect missing signature)")->setReturnType('array')->addParameter('string', 'method')); $methods->add(RpcMethod::create("system.multicall", "\\Comodojo\\RpcServer\\Reserved\\Multicall", "execute")->setDescription("Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader\$1208 for details")->setReturnType('array')->addParameter('array', 'requests')); }
/** * Create an associative array of $name => $parameter from current signature * * @param array $provided * @param \Comodojo\RpcServer\RpcMethod $method * @param integer $selected_signature * * @return array */ private static function matchParameters($provided, $method, $selected_signature) { $parameters = array(); $requested_parameters = $method->selectSignature($selected_signature)->getParameters(); $requested_parameters_keys = array_keys($requested_parameters); foreach ($provided as $index => $parameter) { $parameters[$requested_parameters_keys[$index]] = $parameter; } return $parameters; }