2013-07-28 21:44:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace JsonRPC;
|
|
|
|
|
2015-08-15 03:33:39 +02:00
|
|
|
use Exception;
|
2016-05-08 21:53:45 +02:00
|
|
|
use JsonRPC\Request\RequestBuilder;
|
|
|
|
use JsonRPC\Response\ResponseParser;
|
2015-08-15 03:33:39 +02:00
|
|
|
|
2014-09-19 18:54:48 +02:00
|
|
|
/**
|
|
|
|
* JsonRPC client class
|
|
|
|
*
|
|
|
|
* @package JsonRPC
|
2015-08-15 03:33:39 +02:00
|
|
|
* @author Frederic Guillot
|
2014-09-19 18:54:48 +02:00
|
|
|
*/
|
2013-07-28 21:44:51 +02:00
|
|
|
class Client
|
|
|
|
{
|
2014-09-19 18:54:48 +02:00
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* If the only argument passed to a function is an array
|
|
|
|
* assume it contains named arguments
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-05-08 21:53:45 +02:00
|
|
|
* @var boolean
|
2014-09-19 18:54:48 +02:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
private $isNamedArguments = true;
|
2014-09-19 18:54:48 +02:00
|
|
|
|
2015-06-21 15:56:36 +02:00
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Do not immediately throw an exception on error. Return it instead.
|
2015-06-21 15:56:36 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
private $returnException = false;
|
2015-06-21 15:56:36 +02:00
|
|
|
|
2014-09-19 18:54:48 +02:00
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* True for a batch request
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-05-08 21:53:45 +02:00
|
|
|
* @var boolean
|
2014-09-19 18:54:48 +02:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
private $isBatch = false;
|
2014-09-19 18:54:48 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Batch payload
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-05-08 21:53:45 +02:00
|
|
|
* @var array
|
2014-09-19 18:54:48 +02:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
private $batch = array();
|
2014-09-19 18:54:48 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Http Client
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-05-08 21:53:45 +02:00
|
|
|
* @var HttpClient
|
2014-12-24 03:28:26 +01:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
private $httpClient;
|
2014-12-24 03:28:26 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Constructor
|
2014-12-24 03:28:26 +01:00
|
|
|
*
|
|
|
|
* @access public
|
2016-05-08 21:53:45 +02:00
|
|
|
* @param string $url Server URL
|
|
|
|
* @param bool $returnException Return exceptions
|
|
|
|
* @param HttpClient $httpClient HTTP client object
|
2014-12-24 03:28:26 +01:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
public function __construct($url = '', $returnException = false, HttpClient $httpClient = null)
|
|
|
|
{
|
|
|
|
$this->httpClient = $httpClient ?: new HttpClient($url);
|
|
|
|
$this->returnException = $returnException;
|
|
|
|
}
|
2014-12-24 03:28:26 +01:00
|
|
|
|
2014-09-19 18:54:48 +02:00
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Arguments passed are always positional
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-05-08 21:53:45 +02:00
|
|
|
* @return $this
|
2014-09-19 18:54:48 +02:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
public function withPositionalArguments()
|
|
|
|
{
|
|
|
|
$this->isNamedArguments = false;
|
|
|
|
return $this;
|
|
|
|
}
|
2015-08-15 03:33:39 +02:00
|
|
|
|
2015-01-20 02:00:16 +01:00
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Get HTTP Client
|
2015-08-15 03:33:39 +02:00
|
|
|
*
|
2015-01-20 02:00:16 +01:00
|
|
|
* @access public
|
2016-05-08 21:53:45 +02:00
|
|
|
* @return HttpClient
|
2015-01-20 02:00:16 +01:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
public function getHttpClient()
|
|
|
|
{
|
|
|
|
return $this->httpClient;
|
|
|
|
}
|
2013-07-28 21:44:51 +02:00
|
|
|
|
2014-09-19 18:54:48 +02:00
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Set username and password
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-05-08 21:53:45 +02:00
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @return $this
|
2014-09-19 18:54:48 +02:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
public function authentication($username, $password)
|
2013-07-28 21:44:51 +02:00
|
|
|
{
|
2016-05-08 21:53:45 +02:00
|
|
|
$this->httpClient
|
|
|
|
->withUsername($username)
|
|
|
|
->withPassword($password);
|
|
|
|
|
|
|
|
return $this;
|
2013-07-28 21:44:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 18:54:48 +02:00
|
|
|
/**
|
|
|
|
* Automatic mapping of procedures
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $method Procedure name
|
|
|
|
* @param array $params Procedure arguments
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-12-24 03:28:26 +01:00
|
|
|
public function __call($method, array $params)
|
2014-02-08 20:13:14 +01:00
|
|
|
{
|
2016-05-08 21:53:45 +02:00
|
|
|
if ($this->isNamedArguments && count($params) === 1 && is_array($params[0])) {
|
2014-12-24 03:28:26 +01:00
|
|
|
$params = $params[0];
|
|
|
|
}
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
return $this->execute($method, $params);
|
|
|
|
}
|
2014-09-19 18:54:48 +02:00
|
|
|
|
|
|
|
/**
|
2014-12-24 03:28:26 +01:00
|
|
|
* Start a batch request
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
public function batch()
|
|
|
|
{
|
2016-05-08 21:53:45 +02:00
|
|
|
$this->isBatch = true;
|
2014-12-24 03:28:26 +01:00
|
|
|
$this->batch = array();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a batch request
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function send()
|
|
|
|
{
|
2016-05-08 21:53:45 +02:00
|
|
|
$this->isBatch = false;
|
|
|
|
return $this->sendPayload('['.implode(', ', $this->batch).']');
|
2014-12-24 03:28:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a procedure
|
2014-09-19 18:54:48 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2016-08-06 02:27:09 +02:00
|
|
|
* @param string $procedure Procedure name
|
|
|
|
* @param array $params Procedure arguments
|
|
|
|
* @param array $reqattrs
|
2014-09-19 18:54:48 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-08-06 02:27:09 +02:00
|
|
|
public function execute($procedure, array $params = array(), array $reqattrs = array())
|
2013-07-28 21:44:51 +02:00
|
|
|
{
|
2016-05-08 21:53:45 +02:00
|
|
|
$payload = RequestBuilder::create()
|
|
|
|
->withProcedure($procedure)
|
|
|
|
->withParams($params)
|
2016-08-06 02:27:09 +02:00
|
|
|
->withRequestAttributes($reqattrs)
|
2016-05-08 21:53:45 +02:00
|
|
|
->build();
|
2013-07-28 21:44:51 +02:00
|
|
|
|
2016-05-08 21:53:45 +02:00
|
|
|
if ($this->isBatch) {
|
|
|
|
$this->batch[] = $payload;
|
|
|
|
return $this;
|
2014-09-19 18:54:48 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 21:53:45 +02:00
|
|
|
return $this->sendPayload($payload);
|
2013-07-28 21:44:51 +02:00
|
|
|
}
|
2015-08-15 03:33:39 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-08 21:53:45 +02:00
|
|
|
* Send payload
|
2015-08-15 03:33:39 +02:00
|
|
|
*
|
|
|
|
* @access private
|
2016-05-08 21:53:45 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @param string $payload
|
|
|
|
* @return Exception|Client
|
2015-08-15 03:33:39 +02:00
|
|
|
*/
|
2016-05-08 21:53:45 +02:00
|
|
|
private function sendPayload($payload)
|
2015-08-15 03:33:39 +02:00
|
|
|
{
|
2016-08-06 02:27:09 +02:00
|
|
|
return ResponseParser::create()
|
|
|
|
->withReturnException($this->returnException)
|
|
|
|
->withPayload($this->httpClient->execute($payload))
|
|
|
|
->parse();
|
2015-08-15 03:33:39 +02:00
|
|
|
}
|
2013-07-28 21:44:51 +02:00
|
|
|
}
|