update v1.0.7.9 R.C.
This is a Release Candidate. We are still testing.
This commit is contained in:
87
vendor/zendframework/zend-json/src/Server/Cache.php
vendored
Normal file
87
vendor/zendframework/zend-json/src/Server/Cache.php
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
use Zend\Server\Cache as ServerCache;
|
||||
use Zend\Stdlib\ErrorHandler;
|
||||
|
||||
/**
|
||||
* Zend\Json\Server\Cache: cache Zend\Json\Server\Server server definition and SMD
|
||||
*/
|
||||
class Cache extends ServerCache
|
||||
{
|
||||
/**
|
||||
* Cache a service map description (SMD) to a file
|
||||
*
|
||||
* Returns true on success, false on failure
|
||||
*
|
||||
* @param string $filename
|
||||
* @param \Zend\Json\Server\Server $server
|
||||
* @return bool
|
||||
*/
|
||||
public static function saveSmd($filename, Server $server)
|
||||
{
|
||||
if (!is_string($filename) || (!file_exists($filename) && !is_writable(dirname($filename)))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ErrorHandler::start();
|
||||
$test = file_put_contents($filename, $server->getServiceMap()->toJson());
|
||||
ErrorHandler::stop();
|
||||
|
||||
if (0 === $test) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a cached SMD
|
||||
*
|
||||
* On success, returns the cached SMD (a JSON string); a failure, returns
|
||||
* boolean false.
|
||||
*
|
||||
* @param string $filename
|
||||
* @return string|false
|
||||
*/
|
||||
public static function getSmd($filename)
|
||||
{
|
||||
if (!is_string($filename) || !file_exists($filename) || !is_readable($filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ErrorHandler::start();
|
||||
$smd = file_get_contents($filename);
|
||||
ErrorHandler::stop();
|
||||
|
||||
if (false === $smd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $smd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a file containing a cached SMD
|
||||
*
|
||||
* @param string $filename
|
||||
* @return bool
|
||||
*/
|
||||
public static function deleteSmd($filename)
|
||||
{
|
||||
if (is_string($filename) && file_exists($filename)) {
|
||||
unlink($filename);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
193
vendor/zendframework/zend-json/src/Server/Client.php
vendored
Normal file
193
vendor/zendframework/zend-json/src/Server/Client.php
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
use Zend\Http\Client as HttpClient;
|
||||
use Zend\Server\Client as ServerClient;
|
||||
|
||||
class Client implements ServerClient
|
||||
{
|
||||
/**
|
||||
* Full address of the JSON-RPC service.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $serverAddress;
|
||||
|
||||
/**
|
||||
* HTTP Client to use for requests.
|
||||
*
|
||||
* @var HttpClient
|
||||
*/
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* Request of the last method call.
|
||||
*
|
||||
* @var Request
|
||||
*/
|
||||
protected $lastRequest;
|
||||
|
||||
/**
|
||||
* Response received from the last method call.
|
||||
*
|
||||
* @var Response
|
||||
*/
|
||||
protected $lastResponse;
|
||||
|
||||
/**
|
||||
* Request ID counter.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $id = 0;
|
||||
|
||||
/**
|
||||
* Create a new JSON-RPC client to a remote server.
|
||||
*
|
||||
* @param string $server Full address of the JSON-RPC service.
|
||||
* @param HttpClient $httpClient HTTP Client to use for requests.
|
||||
*/
|
||||
public function __construct($server, HttpClient $httpClient = null)
|
||||
{
|
||||
$this->httpClient = $httpClient ?: new HttpClient();
|
||||
$this->serverAddress = $server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the HTTP client object to use for connecting the JSON-RPC server.
|
||||
*
|
||||
* @param HttpClient $httpClient New HTTP client to use.
|
||||
* @return Client Self instance.
|
||||
*/
|
||||
public function setHttpClient(HttpClient $httpClient)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the HTTP client object.
|
||||
*
|
||||
* @return HttpClient HTTP client.
|
||||
*/
|
||||
public function getHttpClient()
|
||||
{
|
||||
return $this->httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* The request of the last method call.
|
||||
*
|
||||
* @return Request Request instance.
|
||||
*/
|
||||
public function getLastRequest()
|
||||
{
|
||||
return $this->lastRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* The response received from the last method call.
|
||||
*
|
||||
* @return Response Response instance.
|
||||
*/
|
||||
public function getLastResponse()
|
||||
{
|
||||
return $this->lastResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a JSON-RPC request and return a response.
|
||||
*
|
||||
* @param Request $request Request.
|
||||
* @return Response Response.
|
||||
* @throws Exception\HttpException When HTTP communication fails.
|
||||
*/
|
||||
public function doRequest($request)
|
||||
{
|
||||
$this->lastRequest = $request;
|
||||
|
||||
$httpRequest = $this->httpClient->getRequest();
|
||||
if ($httpRequest->getUriString() === null) {
|
||||
$this->httpClient->setUri($this->serverAddress);
|
||||
}
|
||||
|
||||
$headers = $httpRequest->getHeaders();
|
||||
$headers->addHeaders([
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
]);
|
||||
|
||||
if (!$headers->get('User-Agent')) {
|
||||
$headers->addHeaderLine('User-Agent', 'Zend_Json_Server_Client');
|
||||
}
|
||||
|
||||
$this->httpClient->setRawBody($request->__toString());
|
||||
$this->httpClient->setMethod('POST');
|
||||
$httpResponse = $this->httpClient->send();
|
||||
|
||||
if (!$httpResponse->isSuccess()) {
|
||||
throw new Exception\HttpException(
|
||||
$httpResponse->getReasonPhrase(),
|
||||
$httpResponse->getStatusCode()
|
||||
);
|
||||
}
|
||||
|
||||
$response = new Response();
|
||||
|
||||
$this->lastResponse = $response;
|
||||
|
||||
// import all response data from JSON HTTP response
|
||||
$response->loadJson($httpResponse->getBody());
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a JSON-RPC request to the service (for a specific method).
|
||||
*
|
||||
* @param string $method Name of the method we want to call.
|
||||
* @param array $params Array of parameters for the method.
|
||||
* @return mixed Method call results.
|
||||
* @throws Exception\ErrorException When remote call fails.
|
||||
*/
|
||||
public function call($method, $params = [])
|
||||
{
|
||||
$request = $this->createRequest($method, $params);
|
||||
|
||||
$response = $this->doRequest($request);
|
||||
|
||||
if ($response->isError()) {
|
||||
$error = $response->getError();
|
||||
throw new Exception\ErrorException(
|
||||
$error->getMessage(),
|
||||
$error->getCode()
|
||||
);
|
||||
}
|
||||
|
||||
return $response->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create request object.
|
||||
*
|
||||
* @param string $method Method to call.
|
||||
* @param array $params List of arguments.
|
||||
* @return Request Created request.
|
||||
*/
|
||||
protected function createRequest($method, array $params)
|
||||
{
|
||||
$request = new Request();
|
||||
$request->setMethod($method)
|
||||
->setParams($params)
|
||||
->setId(++$this->id);
|
||||
return $request;
|
||||
}
|
||||
}
|
||||
173
vendor/zendframework/zend-json/src/Server/Error.php
vendored
Normal file
173
vendor/zendframework/zend-json/src/Server/Error.php
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
class Error
|
||||
{
|
||||
const ERROR_PARSE = -32700;
|
||||
const ERROR_INVALID_REQUEST = -32600;
|
||||
const ERROR_INVALID_METHOD = -32601;
|
||||
const ERROR_INVALID_PARAMS = -32602;
|
||||
const ERROR_INTERNAL = -32603;
|
||||
const ERROR_OTHER = -32000;
|
||||
|
||||
/**
|
||||
* Current code
|
||||
* @var int
|
||||
*/
|
||||
protected $code = self::ERROR_OTHER;
|
||||
|
||||
/**
|
||||
* Error data
|
||||
* @var mixed
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Error message
|
||||
* @var string
|
||||
*/
|
||||
protected $message;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function __construct($message = null, $code = self::ERROR_OTHER, $data = null)
|
||||
{
|
||||
$this->setMessage($message)
|
||||
->setCode($code)
|
||||
->setData($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set error code.
|
||||
*
|
||||
* If the error code is 0, it will be set to -32000 (ERROR_OTHER).
|
||||
*
|
||||
* @param int $code
|
||||
* @return \Zend\Json\Server\Error
|
||||
*/
|
||||
public function setCode($code)
|
||||
{
|
||||
if (!is_scalar($code) || is_bool($code) || is_float($code)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (is_string($code) && !is_numeric($code)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$code = (int) $code;
|
||||
|
||||
if (0 === $code) {
|
||||
$this->code = self::ERROR_OTHER;
|
||||
} else {
|
||||
$this->code = $code;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error code
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set error message
|
||||
*
|
||||
* @param string $message
|
||||
* @return \Zend\Json\Server\Error
|
||||
*/
|
||||
public function setMessage($message)
|
||||
{
|
||||
if (!is_scalar($message)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->message = (string) $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error message
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set error data
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return \Zend\Json\Server\Error
|
||||
*/
|
||||
public function setData($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast error to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'code' => $this->getCode(),
|
||||
'message' => $this->getMessage(),
|
||||
'data' => $this->getData(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast error to JSON
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return \Zend\Json\Json::encode($this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to string (JSON)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
}
|
||||
20
vendor/zendframework/zend-json/src/Server/Exception/ErrorException.php
vendored
Normal file
20
vendor/zendframework/zend-json/src/Server/Exception/ErrorException.php
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Exception;
|
||||
|
||||
use Zend\Json\Exception;
|
||||
|
||||
/**
|
||||
* Thrown by Zend\Json\Server\Client when a JSON-RPC fault response is returned.
|
||||
*/
|
||||
class ErrorException extends Exception\BadMethodCallException implements
|
||||
ExceptionInterface
|
||||
{
|
||||
}
|
||||
16
vendor/zendframework/zend-json/src/Server/Exception/ExceptionInterface.php
vendored
Normal file
16
vendor/zendframework/zend-json/src/Server/Exception/ExceptionInterface.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Exception;
|
||||
|
||||
use Zend\Json\Exception\ExceptionInterface as Exception;
|
||||
|
||||
interface ExceptionInterface extends Exception
|
||||
{
|
||||
}
|
||||
18
vendor/zendframework/zend-json/src/Server/Exception/HttpException.php
vendored
Normal file
18
vendor/zendframework/zend-json/src/Server/Exception/HttpException.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Exception;
|
||||
|
||||
/**
|
||||
* Thrown by Zend\Json\Server\Client when an HTTP error occurs during an
|
||||
* JSON-RPC method call.
|
||||
*/
|
||||
class HttpException extends RuntimeException
|
||||
{
|
||||
}
|
||||
17
vendor/zendframework/zend-json/src/Server/Exception/InvalidArgumentException.php
vendored
Normal file
17
vendor/zendframework/zend-json/src/Server/Exception/InvalidArgumentException.php
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Exception;
|
||||
|
||||
use Zend\Json\Exception;
|
||||
|
||||
class InvalidArgumentException extends Exception\InvalidArgumentException implements
|
||||
ExceptionInterface
|
||||
{
|
||||
}
|
||||
17
vendor/zendframework/zend-json/src/Server/Exception/RuntimeException.php
vendored
Normal file
17
vendor/zendframework/zend-json/src/Server/Exception/RuntimeException.php
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Exception;
|
||||
|
||||
use Zend\Json\Exception;
|
||||
|
||||
class RuntimeException extends Exception\RuntimeException implements
|
||||
ExceptionInterface
|
||||
{
|
||||
}
|
||||
294
vendor/zendframework/zend-json/src/Server/Request.php
vendored
Normal file
294
vendor/zendframework/zend-json/src/Server/Request.php
vendored
Normal file
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
use Zend\Json;
|
||||
|
||||
/**
|
||||
* @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
/**
|
||||
* Request ID
|
||||
* @var mixed
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Flag
|
||||
* @var bool
|
||||
*/
|
||||
protected $isMethodError = false;
|
||||
|
||||
/**
|
||||
* Flag
|
||||
* @var bool
|
||||
*/
|
||||
protected $isParseError = false;
|
||||
|
||||
/**
|
||||
* Requested method
|
||||
* @var string
|
||||
*/
|
||||
protected $method;
|
||||
|
||||
/**
|
||||
* Regex for method
|
||||
* @var string
|
||||
*/
|
||||
protected $methodRegex = '/^[a-z][a-z0-9\\\\_.]*$/i';
|
||||
|
||||
/**
|
||||
* Request parameters
|
||||
* @var array
|
||||
*/
|
||||
protected $params = [];
|
||||
|
||||
/**
|
||||
* JSON-RPC version of request
|
||||
* @var string
|
||||
*/
|
||||
protected $version = '1.0';
|
||||
|
||||
/**
|
||||
* Set request state
|
||||
*
|
||||
* @param array $options
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$methods = get_class_methods($this);
|
||||
foreach ($options as $key => $value) {
|
||||
$method = 'set' . ucfirst($key);
|
||||
if (in_array($method, $methods)) {
|
||||
$this->$method($value);
|
||||
} elseif ($key == 'jsonrpc') {
|
||||
$this->setVersion($value);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a parameter to the request
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param string $key
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function addParam($value, $key = null)
|
||||
{
|
||||
if ((null === $key) || !is_string($key)) {
|
||||
$index = count($this->params);
|
||||
$this->params[$index] = $value;
|
||||
} else {
|
||||
$this->params[$key] = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add many params
|
||||
*
|
||||
* @param array $params
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function addParams(array $params)
|
||||
{
|
||||
foreach ($params as $key => $value) {
|
||||
$this->addParam($value, $key);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite params
|
||||
*
|
||||
* @param array $params
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function setParams(array $params)
|
||||
{
|
||||
$this->params = [];
|
||||
return $this->addParams($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve param by index or key
|
||||
*
|
||||
* @param int|string $index
|
||||
* @return mixed|null Null when not found
|
||||
*/
|
||||
public function getParam($index)
|
||||
{
|
||||
if (array_key_exists($index, $this->params)) {
|
||||
return $this->params[$index];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request method
|
||||
*
|
||||
* @param string $name
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function setMethod($name)
|
||||
{
|
||||
if (!preg_match($this->methodRegex, $name)) {
|
||||
$this->isMethodError = true;
|
||||
} else {
|
||||
$this->method = $name;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request method name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Was a bad method provided?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMethodError()
|
||||
{
|
||||
return $this->isMethodError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Was a malformed JSON provided?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isParseError()
|
||||
{
|
||||
return $this->isParseError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request identifier
|
||||
*
|
||||
* @param mixed $name
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function setId($name)
|
||||
{
|
||||
$this->id = (string) $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve request identifier
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set JSON-RPC version
|
||||
*
|
||||
* @param string $version
|
||||
* @return \Zend\Json\Server\Request
|
||||
*/
|
||||
public function setVersion($version)
|
||||
{
|
||||
if ('2.0' == $version) {
|
||||
$this->version = '2.0';
|
||||
} else {
|
||||
$this->version = '1.0';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON-RPC version
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request state based on JSON
|
||||
*
|
||||
* @param string $json
|
||||
* @return void
|
||||
*/
|
||||
public function loadJson($json)
|
||||
{
|
||||
try {
|
||||
$options = Json\Json::decode($json, Json\Json::TYPE_ARRAY);
|
||||
$this->setOptions($options);
|
||||
} catch (\Exception $e) {
|
||||
$this->isParseError = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast request to JSON
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
$jsonArray = [
|
||||
'method' => $this->getMethod()
|
||||
];
|
||||
if (null !== ($id = $this->getId())) {
|
||||
$jsonArray['id'] = $id;
|
||||
}
|
||||
$params = $this->getParams();
|
||||
if (!empty($params)) {
|
||||
$jsonArray['params'] = $params;
|
||||
}
|
||||
if ('2.0' == $this->getVersion()) {
|
||||
$jsonArray['jsonrpc'] = '2.0';
|
||||
}
|
||||
|
||||
return Json\Json::encode($jsonArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast request to string (JSON)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
}
|
||||
46
vendor/zendframework/zend-json/src/Server/Request/Http.php
vendored
Normal file
46
vendor/zendframework/zend-json/src/Server/Request/Http.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Request;
|
||||
|
||||
use Zend\Json\Server\Request as JsonRequest;
|
||||
|
||||
class Http extends JsonRequest
|
||||
{
|
||||
/**
|
||||
* Raw JSON pulled from POST body
|
||||
* @var string
|
||||
*/
|
||||
protected $rawJson;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Pull JSON request from raw POST body and use to populate request.
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$json = file_get_contents('php://input');
|
||||
$this->rawJson = $json;
|
||||
if (!empty($json)) {
|
||||
$this->loadJson($json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get JSON from raw POST body
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRawJson()
|
||||
{
|
||||
return $this->rawJson;
|
||||
}
|
||||
}
|
||||
279
vendor/zendframework/zend-json/src/Server/Response.php
vendored
Normal file
279
vendor/zendframework/zend-json/src/Server/Response.php
vendored
Normal file
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
use Zend\Json\Json;
|
||||
|
||||
class Response
|
||||
{
|
||||
/**
|
||||
* Response error
|
||||
* @var null|Error
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* Request ID
|
||||
* @var mixed
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Result
|
||||
* @var mixed
|
||||
*/
|
||||
protected $result;
|
||||
|
||||
/**
|
||||
* Service map
|
||||
* @var Smd
|
||||
*/
|
||||
protected $serviceMap;
|
||||
|
||||
/**
|
||||
* JSON-RPC version
|
||||
* @var string
|
||||
*/
|
||||
protected $version;
|
||||
|
||||
/**
|
||||
* @var $args
|
||||
*/
|
||||
protected $args;
|
||||
|
||||
/**
|
||||
* Set response state
|
||||
*
|
||||
* @param array $options
|
||||
* @return Response
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
// re-produce error state
|
||||
if (isset($options['error']) && is_array($options['error'])) {
|
||||
$error = $options['error'];
|
||||
$options['error'] = new Error($error['message'], $error['code'], $error['data']);
|
||||
}
|
||||
|
||||
$methods = get_class_methods($this);
|
||||
foreach ($options as $key => $value) {
|
||||
$method = 'set' . ucfirst($key);
|
||||
if (in_array($method, $methods)) {
|
||||
$this->$method($value);
|
||||
} elseif ($key == 'jsonrpc') {
|
||||
$this->setVersion($value);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set response state based on JSON
|
||||
*
|
||||
* @param string $json
|
||||
* @return void
|
||||
* @throws Exception\RuntimeException
|
||||
*/
|
||||
public function loadJson($json)
|
||||
{
|
||||
$options = Json::decode($json, Json::TYPE_ARRAY);
|
||||
|
||||
if (!is_array($options)) {
|
||||
throw new Exception\RuntimeException('json is not a valid response; array expected');
|
||||
}
|
||||
|
||||
$this->setOptions($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set result
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return Response
|
||||
*/
|
||||
public function setResult($value)
|
||||
{
|
||||
$this->result = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get result
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getResult()
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
// RPC error, if response results in fault
|
||||
/**
|
||||
* Set result error
|
||||
*
|
||||
* @param mixed $error
|
||||
* @return Response
|
||||
*/
|
||||
public function setError(Error $error = null)
|
||||
{
|
||||
$this->error = $error;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response error
|
||||
*
|
||||
* @return null|Error
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the response an error?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isError()
|
||||
{
|
||||
return $this->getError() instanceof Error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request ID
|
||||
*
|
||||
* @param mixed $name
|
||||
* @return Response
|
||||
*/
|
||||
public function setId($name)
|
||||
{
|
||||
$this->id = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request ID
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set JSON-RPC version
|
||||
*
|
||||
* @param string $version
|
||||
* @return Response
|
||||
*/
|
||||
public function setVersion($version)
|
||||
{
|
||||
$version = (string) $version;
|
||||
if ('2.0' == $version) {
|
||||
$this->version = '2.0';
|
||||
} else {
|
||||
$this->version = null;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON-RPC version
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to JSON
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
if ($this->isError()) {
|
||||
$response = [
|
||||
'error' => $this->getError()->toArray(),
|
||||
'id' => $this->getId(),
|
||||
];
|
||||
} else {
|
||||
$response = [
|
||||
'result' => $this->getResult(),
|
||||
'id' => $this->getId(),
|
||||
];
|
||||
}
|
||||
|
||||
if (null !== ($version = $this->getVersion())) {
|
||||
$response['jsonrpc'] = $version;
|
||||
}
|
||||
|
||||
return \Zend\Json\Json::encode($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve args
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getArgs()
|
||||
{
|
||||
return $this->args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set args
|
||||
*
|
||||
* @param mixed $args
|
||||
* @return self
|
||||
*/
|
||||
public function setArgs($args)
|
||||
{
|
||||
$this->args = $args;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service map object
|
||||
*
|
||||
* @param Smd $serviceMap
|
||||
* @return Response
|
||||
*/
|
||||
public function setServiceMap($serviceMap)
|
||||
{
|
||||
$this->serviceMap = $serviceMap;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve service map
|
||||
*
|
||||
* @return Smd|null
|
||||
*/
|
||||
public function getServiceMap()
|
||||
{
|
||||
return $this->serviceMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to string (JSON)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
}
|
||||
62
vendor/zendframework/zend-json/src/Server/Response/Http.php
vendored
Normal file
62
vendor/zendframework/zend-json/src/Server/Response/Http.php
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Response;
|
||||
|
||||
use Zend\Json\Server\Response as JsonResponse;
|
||||
|
||||
class Http extends JsonResponse
|
||||
{
|
||||
/**
|
||||
* Emit JSON
|
||||
*
|
||||
* Send appropriate HTTP headers. If no Id, then return an empty string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
$this->sendHeaders();
|
||||
if (!$this->isError() && null === $this->getId()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return parent::toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send headers
|
||||
*
|
||||
* If headers are already sent, do nothing. If null ID, send HTTP 204
|
||||
* header. Otherwise, send content type header based on content type of
|
||||
* service map.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function sendHeaders()
|
||||
{
|
||||
if (headers_sent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->isError() && (null === $this->getId())) {
|
||||
header('HTTP/1.1 204 No Content');
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === ($smd = $this->getServiceMap())) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contentType = $smd->getContentType();
|
||||
if (!empty($contentType)) {
|
||||
header('Content-Type: ' . $contentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
564
vendor/zendframework/zend-json/src/Server/Server.php
vendored
Normal file
564
vendor/zendframework/zend-json/src/Server/Server.php
vendored
Normal file
@@ -0,0 +1,564 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
use ReflectionFunction;
|
||||
use ReflectionMethod;
|
||||
use Zend\Server\AbstractServer;
|
||||
use Zend\Server\Definition;
|
||||
use Zend\Server\Method;
|
||||
use Zend\Server\Reflection;
|
||||
|
||||
class Server extends AbstractServer
|
||||
{
|
||||
/**#@+
|
||||
* Version Constants
|
||||
*/
|
||||
const VERSION_1 = '1.0';
|
||||
const VERSION_2 = '2.0';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Flag: whether or not to auto-emit the response
|
||||
* @var bool
|
||||
*/
|
||||
protected $returnResponse = false;
|
||||
|
||||
/**
|
||||
* Inherited from Zend\Server\AbstractServer
|
||||
*
|
||||
* @var bool Flag; allow overwriting existing methods when creating server definition
|
||||
*/
|
||||
protected $overwriteExistingMethods = true;
|
||||
|
||||
/**
|
||||
* Request object
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Response object
|
||||
* @var Response
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* SMD object
|
||||
* @var Smd
|
||||
*/
|
||||
protected $serviceMap;
|
||||
|
||||
/**
|
||||
* SMD class accessors
|
||||
* @var array
|
||||
*/
|
||||
protected $smdMethods;
|
||||
|
||||
/**
|
||||
* Attach a function or callback to the server
|
||||
*
|
||||
* @param string|array|callable $function Valid PHP callback
|
||||
* @param string $namespace Ignored
|
||||
* @throws Exception\InvalidArgumentException if function invalid or not callable
|
||||
* @return Server
|
||||
*/
|
||||
public function addFunction($function, $namespace = '')
|
||||
{
|
||||
if (!is_string($function) && (!is_array($function) || (2 > count($function)))) {
|
||||
throw new Exception\InvalidArgumentException('Unable to attach function; invalid');
|
||||
}
|
||||
|
||||
if (!is_callable($function)) {
|
||||
throw new Exception\InvalidArgumentException('Unable to attach function; does not exist');
|
||||
}
|
||||
|
||||
$argv = null;
|
||||
if (2 < func_num_args()) {
|
||||
$argv = func_get_args();
|
||||
$argv = array_slice($argv, 2);
|
||||
}
|
||||
|
||||
$class = null;
|
||||
if (is_string($function)) {
|
||||
$method = Reflection::reflectFunction($function, $argv, $namespace);
|
||||
} else {
|
||||
$class = array_shift($function);
|
||||
$action = array_shift($function);
|
||||
$reflection = Reflection::reflectClass($class, $argv, $namespace);
|
||||
$methods = $reflection->getMethods();
|
||||
$found = false;
|
||||
foreach ($methods as $method) {
|
||||
if ($action == $method->getName()) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$this->fault('Method not found', Error::ERROR_INVALID_METHOD);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
$definition = $this->_buildSignature($method, $class);
|
||||
$this->_addMethodServiceMap($definition);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a class with the server
|
||||
*
|
||||
* @param string $class
|
||||
* @param string $namespace Ignored
|
||||
* @param mixed $argv Ignored
|
||||
* @return Server
|
||||
*/
|
||||
public function setClass($class, $namespace = '', $argv = null)
|
||||
{
|
||||
if (2 < func_num_args()) {
|
||||
$argv = func_get_args();
|
||||
$argv = array_slice($argv, 2);
|
||||
}
|
||||
|
||||
$reflection = Reflection::reflectClass($class, $argv, $namespace);
|
||||
|
||||
foreach ($reflection->getMethods() as $method) {
|
||||
$definition = $this->_buildSignature($method, $class);
|
||||
$this->_addMethodServiceMap($definition);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate fault response
|
||||
*
|
||||
* @param string $fault
|
||||
* @param int $code
|
||||
* @param mixed $data
|
||||
* @return Error
|
||||
*/
|
||||
public function fault($fault = null, $code = 404, $data = null)
|
||||
{
|
||||
$error = new Error($fault, $code, $data);
|
||||
$this->getResponse()->setError($error);
|
||||
return $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle request
|
||||
*
|
||||
* @param Request $request
|
||||
* @return null|Response
|
||||
* @throws Exception\InvalidArgumentException
|
||||
*/
|
||||
public function handle($request = false)
|
||||
{
|
||||
if ((false !== $request) && (!$request instanceof Request)) {
|
||||
throw new Exception\InvalidArgumentException('Invalid request type provided; cannot handle');
|
||||
} elseif ($request) {
|
||||
$this->setRequest($request);
|
||||
}
|
||||
|
||||
// Handle request
|
||||
$this->_handle();
|
||||
|
||||
// Get response
|
||||
$response = $this->_getReadyResponse();
|
||||
|
||||
// Emit response?
|
||||
if (!$this->returnResponse) {
|
||||
echo $response;
|
||||
return;
|
||||
}
|
||||
|
||||
// or return it?
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load function definitions
|
||||
*
|
||||
* @param array|Definition $definition
|
||||
* @throws Exception\InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
public function loadFunctions($definition)
|
||||
{
|
||||
if (!is_array($definition) && (!$definition instanceof Definition)) {
|
||||
throw new Exception\InvalidArgumentException('Invalid definition provided to loadFunctions()');
|
||||
}
|
||||
|
||||
foreach ($definition as $key => $method) {
|
||||
$this->table->addMethod($method, $key);
|
||||
$this->_addMethodServiceMap($method);
|
||||
}
|
||||
}
|
||||
|
||||
public function setPersistence($mode)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Set request object
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Server
|
||||
*/
|
||||
public function setRequest(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get JSON-RPC request object
|
||||
*
|
||||
* @return Request
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
if (null === ($request = $this->request)) {
|
||||
$this->setRequest(new Request\Http());
|
||||
}
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set response object
|
||||
*
|
||||
* @param Response $response
|
||||
* @return Server
|
||||
*/
|
||||
public function setResponse(Response $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response object
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
if (null === ($response = $this->response)) {
|
||||
$this->setResponse(new Response\Http());
|
||||
}
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set return response flag
|
||||
*
|
||||
* If true, {@link handle()} will return the response instead of
|
||||
* automatically sending it back to the requesting client.
|
||||
*
|
||||
* The response is always available via {@link getResponse()}.
|
||||
*
|
||||
* @param bool $flag
|
||||
* @return Server
|
||||
*/
|
||||
public function setReturnResponse($flag = true)
|
||||
{
|
||||
$this->returnResponse = (bool) $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve return response flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getReturnResponse()
|
||||
{
|
||||
return $this->returnResponse;
|
||||
}
|
||||
|
||||
// overloading for SMD metadata
|
||||
/**
|
||||
* Overload to accessors of SMD object
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $args)
|
||||
{
|
||||
if (preg_match('/^(set|get)/', $method, $matches)) {
|
||||
if (in_array($method, $this->_getSmdMethods())) {
|
||||
if ('set' == $matches[1]) {
|
||||
$value = array_shift($args);
|
||||
$this->getServiceMap()->$method($value);
|
||||
return $this;
|
||||
} else {
|
||||
return $this->getServiceMap()->$method();
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve SMD object
|
||||
*
|
||||
* @return Smd
|
||||
*/
|
||||
public function getServiceMap()
|
||||
{
|
||||
if (null === $this->serviceMap) {
|
||||
$this->serviceMap = new Smd();
|
||||
}
|
||||
return $this->serviceMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add service method to service map
|
||||
*
|
||||
* @param Method\Definition $method
|
||||
* @return void
|
||||
*/
|
||||
protected function _addMethodServiceMap(Method\Definition $method)
|
||||
{
|
||||
$serviceInfo = [
|
||||
'name' => $method->getName(),
|
||||
'return' => $this->_getReturnType($method),
|
||||
];
|
||||
$params = $this->_getParams($method);
|
||||
$serviceInfo['params'] = $params;
|
||||
$serviceMap = $this->getServiceMap();
|
||||
if (false !== $serviceMap->getService($serviceInfo['name'])) {
|
||||
$serviceMap->removeService($serviceInfo['name']);
|
||||
}
|
||||
$serviceMap->addService($serviceInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate PHP type to JSON type
|
||||
*
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
protected function _fixType($type)
|
||||
{
|
||||
return $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default params from signature
|
||||
*
|
||||
* @param array $args
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function _getDefaultParams(array $args, array $params)
|
||||
{
|
||||
if (false === $this->isAssociative($args)) {
|
||||
$params = array_slice($params, count($args));
|
||||
}
|
||||
|
||||
foreach ($params as $param) {
|
||||
if (isset($args[$param['name']]) || !array_key_exists('default', $param)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$args[$param['name']] = $param['default'];
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* check whether array is associative or not
|
||||
*
|
||||
* @param array $array
|
||||
* @return bool
|
||||
*/
|
||||
private function isAssociative(array $array)
|
||||
{
|
||||
$keys = array_keys($array);
|
||||
|
||||
return ($keys != array_keys($keys));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get method param type
|
||||
*
|
||||
* @param Method\Definition $method
|
||||
* @return string|array
|
||||
*/
|
||||
protected function _getParams(Method\Definition $method)
|
||||
{
|
||||
$params = [];
|
||||
foreach ($method->getPrototypes() as $prototype) {
|
||||
foreach ($prototype->getParameterObjects() as $key => $parameter) {
|
||||
if (!isset($params[$key])) {
|
||||
$params[$key] = [
|
||||
'type' => $parameter->getType(),
|
||||
'name' => $parameter->getName(),
|
||||
'optional' => $parameter->isOptional(),
|
||||
];
|
||||
if (null !== ($default = $parameter->getDefaultValue())) {
|
||||
$params[$key]['default'] = $default;
|
||||
}
|
||||
$description = $parameter->getDescription();
|
||||
if (!empty($description)) {
|
||||
$params[$key]['description'] = $description;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$newType = $parameter->getType();
|
||||
if (!is_array($params[$key]['type'])) {
|
||||
if ($params[$key]['type'] == $newType) {
|
||||
continue;
|
||||
}
|
||||
$params[$key]['type'] = (array) $params[$key]['type'];
|
||||
} elseif (in_array($newType, $params[$key]['type'])) {
|
||||
continue;
|
||||
}
|
||||
array_push($params[$key]['type'], $parameter->getType());
|
||||
}
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set response state
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function _getReadyResponse()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$response = $this->getResponse();
|
||||
|
||||
$response->setServiceMap($this->getServiceMap());
|
||||
if (null !== ($id = $request->getId())) {
|
||||
$response->setId($id);
|
||||
}
|
||||
if (null !== ($version = $request->getVersion())) {
|
||||
$response->setVersion($version);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get method return type
|
||||
*
|
||||
* @param Method\Definition $method
|
||||
* @return string|array
|
||||
*/
|
||||
protected function _getReturnType(Method\Definition $method)
|
||||
{
|
||||
$return = [];
|
||||
foreach ($method->getPrototypes() as $prototype) {
|
||||
$return[] = $prototype->getReturnType();
|
||||
}
|
||||
if (1 == count($return)) {
|
||||
return $return[0];
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve list of allowed SMD methods for proxying
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function _getSmdMethods()
|
||||
{
|
||||
if (null === $this->smdMethods) {
|
||||
$this->smdMethods = [];
|
||||
$methods = get_class_methods('Zend\\Json\\Server\\Smd');
|
||||
foreach ($methods as $method) {
|
||||
if (!preg_match('/^(set|get)/', $method)) {
|
||||
continue;
|
||||
}
|
||||
if (strstr($method, 'Service')) {
|
||||
continue;
|
||||
}
|
||||
$this->smdMethods[] = $method;
|
||||
}
|
||||
}
|
||||
return $this->smdMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method for handling request
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _handle()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
|
||||
if ($request->isParseError()) {
|
||||
return $this->fault('Parse error', Error::ERROR_PARSE);
|
||||
}
|
||||
|
||||
if (!$request->isMethodError() && (null === $request->getMethod())) {
|
||||
return $this->fault('Invalid Request', Error::ERROR_INVALID_REQUEST);
|
||||
}
|
||||
|
||||
if ($request->isMethodError()) {
|
||||
return $this->fault('Invalid Request', Error::ERROR_INVALID_REQUEST);
|
||||
}
|
||||
|
||||
$method = $request->getMethod();
|
||||
if (!$this->table->hasMethod($method)) {
|
||||
return $this->fault('Method not found', Error::ERROR_INVALID_METHOD);
|
||||
}
|
||||
|
||||
$params = $request->getParams();
|
||||
$invokable = $this->table->getMethod($method);
|
||||
$serviceMap = $this->getServiceMap();
|
||||
$service = $serviceMap->getService($method);
|
||||
$serviceParams = $service->getParams();
|
||||
|
||||
if (count($params) < count($serviceParams)) {
|
||||
$params = $this->_getDefaultParams($params, $serviceParams);
|
||||
}
|
||||
|
||||
//Make sure named parameters are passed in correct order
|
||||
if (is_string(key($params))) {
|
||||
$callback = $invokable->getCallback();
|
||||
if ('function' == $callback->getType()) {
|
||||
$reflection = new ReflectionFunction($callback->getFunction());
|
||||
} else {
|
||||
$reflection = new ReflectionMethod(
|
||||
$callback->getClass(),
|
||||
$callback->getMethod()
|
||||
);
|
||||
}
|
||||
|
||||
$orderedParams = [];
|
||||
foreach ($reflection->getParameters() as $refParam) {
|
||||
if (array_key_exists($refParam->getName(), $params)) {
|
||||
$orderedParams[$refParam->getName()] = $params[$refParam->getName()];
|
||||
} elseif ($refParam->isOptional()) {
|
||||
$orderedParams[$refParam->getName()] = null;
|
||||
} else {
|
||||
return $this->fault('Invalid params', Error::ERROR_INVALID_PARAMS);
|
||||
}
|
||||
}
|
||||
$params = $orderedParams;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->_dispatch($invokable, $params);
|
||||
} catch (\Exception $e) {
|
||||
return $this->fault($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
$this->getResponse()->setResult($result);
|
||||
}
|
||||
}
|
||||
461
vendor/zendframework/zend-json/src/Server/Smd.php
vendored
Normal file
461
vendor/zendframework/zend-json/src/Server/Smd.php
vendored
Normal file
@@ -0,0 +1,461 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server;
|
||||
|
||||
use Zend\Json\Server\Exception\InvalidArgumentException;
|
||||
use Zend\Json\Server\Exception\RuntimeException;
|
||||
|
||||
class Smd
|
||||
{
|
||||
const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
|
||||
const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
|
||||
const SMD_VERSION = '2.0';
|
||||
|
||||
/**
|
||||
* Content type
|
||||
* @var string
|
||||
*/
|
||||
protected $contentType = 'application/json';
|
||||
|
||||
/**
|
||||
* Content type regex
|
||||
* @var string
|
||||
*/
|
||||
protected $contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
|
||||
|
||||
/**
|
||||
* Service description
|
||||
* @var string
|
||||
*/
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* Generate Dojo-compatible SMD
|
||||
* @var bool
|
||||
*/
|
||||
protected $dojoCompatible = false;
|
||||
|
||||
/**
|
||||
* Current envelope
|
||||
* @var string
|
||||
*/
|
||||
protected $envelope = self::ENV_JSONRPC_1;
|
||||
|
||||
/**
|
||||
* Allowed envelope types
|
||||
* @var array
|
||||
*/
|
||||
protected $envelopeTypes = [
|
||||
self::ENV_JSONRPC_1,
|
||||
self::ENV_JSONRPC_2,
|
||||
];
|
||||
|
||||
/**
|
||||
* Service id
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Services offered
|
||||
* @var array
|
||||
*/
|
||||
protected $services = [];
|
||||
|
||||
/**
|
||||
* Service target
|
||||
* @var string
|
||||
*/
|
||||
protected $target;
|
||||
|
||||
/**
|
||||
* Global transport
|
||||
* @var string
|
||||
*/
|
||||
protected $transport = 'POST';
|
||||
|
||||
/**
|
||||
* Allowed transport types
|
||||
* @var array
|
||||
*/
|
||||
protected $transportTypes = ['POST'];
|
||||
|
||||
/**
|
||||
* Set object state via options
|
||||
*
|
||||
* @param array $options
|
||||
* @return Smd
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
$method = 'set' . ucfirst($key);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set transport
|
||||
*
|
||||
* @param string $transport
|
||||
* @throws Exception\InvalidArgumentException
|
||||
* @return \Zend\Json\Server\Smd
|
||||
*/
|
||||
public function setTransport($transport)
|
||||
{
|
||||
if (!in_array($transport, $this->transportTypes)) {
|
||||
throw new InvalidArgumentException("Invalid transport '{$transport}' specified");
|
||||
}
|
||||
$this->transport = $transport;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transport
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTransport()
|
||||
{
|
||||
return $this->transport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set envelope
|
||||
*
|
||||
* @param string $envelopeType
|
||||
* @throws Exception\InvalidArgumentException
|
||||
* @return Smd
|
||||
*/
|
||||
public function setEnvelope($envelopeType)
|
||||
{
|
||||
if (!in_array($envelopeType, $this->envelopeTypes)) {
|
||||
throw new InvalidArgumentException("Invalid envelope type '{$envelopeType}'");
|
||||
}
|
||||
$this->envelope = $envelopeType;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve envelope
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvelope()
|
||||
{
|
||||
return $this->envelope;
|
||||
}
|
||||
|
||||
// Content-Type of response; default to application/json
|
||||
/**
|
||||
* Set content type
|
||||
*
|
||||
* @param string $type
|
||||
* @throws Exception\InvalidArgumentException
|
||||
* @return \Zend\Json\Server\Smd
|
||||
*/
|
||||
public function setContentType($type)
|
||||
{
|
||||
if (!preg_match($this->contentTypeRegex, $type)) {
|
||||
throw new InvalidArgumentException("Invalid content type '{$type}' specified");
|
||||
}
|
||||
$this->contentType = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve content type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType()
|
||||
{
|
||||
return $this->contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service target
|
||||
*
|
||||
* @param string $target
|
||||
* @return Smd
|
||||
*/
|
||||
public function setTarget($target)
|
||||
{
|
||||
$this->target = (string) $target;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve service target
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTarget()
|
||||
{
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service ID
|
||||
*
|
||||
* @param string $id
|
||||
* @return Smd
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = (string) $id;
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service description
|
||||
*
|
||||
* @param string $description
|
||||
* @return Smd
|
||||
*/
|
||||
public function setDescription($description)
|
||||
{
|
||||
$this->description = (string) $description;
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate whether or not to generate Dojo-compatible SMD
|
||||
*
|
||||
* @param bool $flag
|
||||
* @return Smd
|
||||
*/
|
||||
public function setDojoCompatible($flag)
|
||||
{
|
||||
$this->dojoCompatible = (bool) $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a Dojo compatible SMD?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDojoCompatible()
|
||||
{
|
||||
return $this->dojoCompatible;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Service
|
||||
*
|
||||
* @param Smd\Service|array $service
|
||||
* @throws Exception\RuntimeException
|
||||
* @throws Exception\InvalidArgumentException
|
||||
* @return Smd
|
||||
*/
|
||||
public function addService($service)
|
||||
{
|
||||
if ($service instanceof Smd\Service) {
|
||||
$name = $service->getName();
|
||||
} elseif (is_array($service)) {
|
||||
$service = new Smd\Service($service);
|
||||
$name = $service->getName();
|
||||
} else {
|
||||
throw new InvalidArgumentException('Invalid service passed to addService()');
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $this->services)) {
|
||||
throw new RuntimeException('Attempt to register a service already registered detected');
|
||||
}
|
||||
$this->services[$name] = $service;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add many services
|
||||
*
|
||||
* @param array $services
|
||||
* @return Smd
|
||||
*/
|
||||
public function addServices(array $services)
|
||||
{
|
||||
foreach ($services as $service) {
|
||||
$this->addService($service);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite existing services with new ones
|
||||
*
|
||||
* @param array $services
|
||||
* @return Smd
|
||||
*/
|
||||
public function setServices(array $services)
|
||||
{
|
||||
$this->services = [];
|
||||
return $this->addServices($services);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service object
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool|Smd\Service
|
||||
*/
|
||||
public function getService($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->services)) {
|
||||
return $this->services[$name];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return services
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getServices()
|
||||
{
|
||||
return $this->services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove service
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function removeService($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->services)) {
|
||||
unset($this->services[$name]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
if ($this->isDojoCompatible()) {
|
||||
return $this->toDojoArray();
|
||||
}
|
||||
|
||||
$description = $this->getDescription();
|
||||
$transport = $this->getTransport();
|
||||
$envelope = $this->getEnvelope();
|
||||
$contentType = $this->getContentType();
|
||||
$SMDVersion = static::SMD_VERSION;
|
||||
$service = compact('transport', 'envelope', 'contentType', 'SMDVersion', 'description');
|
||||
|
||||
if (null !== ($target = $this->getTarget())) {
|
||||
$service['target'] = $target;
|
||||
}
|
||||
if (null !== ($id = $this->getId())) {
|
||||
$service['id'] = $id;
|
||||
}
|
||||
|
||||
$services = $this->getServices();
|
||||
if (!empty($services)) {
|
||||
$service['services'] = [];
|
||||
foreach ($services as $name => $svc) {
|
||||
$svc->setEnvelope($envelope);
|
||||
$service['services'][$name] = $svc->toArray();
|
||||
}
|
||||
$service['methods'] = $service['services'];
|
||||
}
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export to DOJO-compatible SMD array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toDojoArray()
|
||||
{
|
||||
$SMDVersion = '.1';
|
||||
$serviceType = 'JSON-RPC';
|
||||
$service = compact('SMDVersion', 'serviceType');
|
||||
|
||||
$target = $this->getTarget();
|
||||
|
||||
$services = $this->getServices();
|
||||
if (!empty($services)) {
|
||||
$service['methods'] = [];
|
||||
foreach ($services as $name => $svc) {
|
||||
$method = [
|
||||
'name' => $name,
|
||||
'serviceURL' => $target,
|
||||
];
|
||||
$params = [];
|
||||
foreach ($svc->getParams() as $param) {
|
||||
$paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
|
||||
$params[] = [
|
||||
'name' => $paramName,
|
||||
'type' => $param['type'],
|
||||
];
|
||||
}
|
||||
if (!empty($params)) {
|
||||
$method['parameters'] = $params;
|
||||
}
|
||||
$service['methods'][] = $method;
|
||||
}
|
||||
}
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to JSON
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return \Zend\Json\Json::encode($this->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to string (JSON)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
}
|
||||
465
vendor/zendframework/zend-json/src/Server/Smd/Service.php
vendored
Normal file
465
vendor/zendframework/zend-json/src/Server/Smd/Service.php
vendored
Normal file
@@ -0,0 +1,465 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework (http://framework.zend.com/)
|
||||
*
|
||||
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
||||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
namespace Zend\Json\Server\Smd;
|
||||
|
||||
use Zend\Json\Server\Exception\InvalidArgumentException;
|
||||
use Zend\Json\Server\Smd;
|
||||
|
||||
/**
|
||||
* Create Service Mapping Description for a method
|
||||
*
|
||||
* @todo Revised method regex to allow NS; however, should SMD be revised to strip PHP NS instead when attaching functions?
|
||||
*/
|
||||
class Service
|
||||
{
|
||||
/**#@+
|
||||
* Service metadata
|
||||
* @var string
|
||||
*/
|
||||
protected $envelope = Smd::ENV_JSONRPC_1;
|
||||
protected $name;
|
||||
protected $return;
|
||||
protected $target;
|
||||
protected $transport = 'POST';
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Allowed envelope types
|
||||
* @var array
|
||||
*/
|
||||
protected $envelopeTypes = [
|
||||
Smd::ENV_JSONRPC_1,
|
||||
Smd::ENV_JSONRPC_2,
|
||||
];
|
||||
|
||||
/**
|
||||
* Regex for names
|
||||
* @var string
|
||||
*
|
||||
* @link http://php.net/manual/en/language.oop5.basic.php
|
||||
* @link http://www.jsonrpc.org/specification#request_object
|
||||
*/
|
||||
protected $nameRegex = '/^(?!^rpc\.)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\.\\\]*$/';
|
||||
|
||||
/**
|
||||
* Parameter option types
|
||||
* @var array
|
||||
*/
|
||||
protected $paramOptionTypes = [
|
||||
'name' => 'is_string',
|
||||
'optional' => 'is_bool',
|
||||
'default' => null,
|
||||
'description' => 'is_string',
|
||||
];
|
||||
|
||||
/**
|
||||
* Service params
|
||||
* @var array
|
||||
*/
|
||||
protected $params = [];
|
||||
|
||||
/**
|
||||
* Mapping of parameter types to JSON-RPC types
|
||||
* @var array
|
||||
*/
|
||||
protected $paramMap = [
|
||||
'any' => 'any',
|
||||
'arr' => 'array',
|
||||
'array' => 'array',
|
||||
'assoc' => 'object',
|
||||
'bool' => 'boolean',
|
||||
'boolean' => 'boolean',
|
||||
'dbl' => 'float',
|
||||
'double' => 'float',
|
||||
'false' => 'boolean',
|
||||
'float' => 'float',
|
||||
'hash' => 'object',
|
||||
'integer' => 'integer',
|
||||
'int' => 'integer',
|
||||
'mixed' => 'any',
|
||||
'nil' => 'null',
|
||||
'null' => 'null',
|
||||
'object' => 'object',
|
||||
'string' => 'string',
|
||||
'str' => 'string',
|
||||
'struct' => 'object',
|
||||
'true' => 'boolean',
|
||||
'void' => 'null',
|
||||
];
|
||||
|
||||
/**
|
||||
* Allowed transport types
|
||||
* @var array
|
||||
*/
|
||||
protected $transportTypes = [
|
||||
'POST',
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string|array $spec
|
||||
* @throws InvalidArgumentException if no name provided
|
||||
*/
|
||||
public function __construct($spec)
|
||||
{
|
||||
if (is_string($spec)) {
|
||||
$this->setName($spec);
|
||||
} elseif (is_array($spec)) {
|
||||
$this->setOptions($spec);
|
||||
}
|
||||
|
||||
if (null == $this->getName()) {
|
||||
throw new InvalidArgumentException('SMD service description requires a name; none provided');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object state
|
||||
*
|
||||
* @param array $options
|
||||
* @return Service
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$methods = get_class_methods($this);
|
||||
foreach ($options as $key => $value) {
|
||||
if ('options' == strtolower($key)) {
|
||||
continue;
|
||||
}
|
||||
$method = 'set' . ucfirst($key);
|
||||
if (in_array($method, $methods)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service name
|
||||
*
|
||||
* @param string $name
|
||||
* @return Service
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$name = (string) $name;
|
||||
if (!preg_match($this->nameRegex, $name)) {
|
||||
throw new InvalidArgumentException("Invalid name '{$name} provided for service; must follow PHP method naming conventions");
|
||||
}
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Transport
|
||||
*
|
||||
* Currently limited to POST
|
||||
*
|
||||
* @param string $transport
|
||||
* @throws InvalidArgumentException
|
||||
* @return Service
|
||||
*/
|
||||
public function setTransport($transport)
|
||||
{
|
||||
if (!in_array($transport, $this->transportTypes)) {
|
||||
throw new InvalidArgumentException("Invalid transport '{$transport}'; please select one of (" . implode(', ', $this->transportTypes) . ')');
|
||||
}
|
||||
|
||||
$this->transport = $transport;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transport
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTransport()
|
||||
{
|
||||
return $this->transport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set service target
|
||||
*
|
||||
* @param string $target
|
||||
* @return Service
|
||||
*/
|
||||
public function setTarget($target)
|
||||
{
|
||||
$this->target = (string) $target;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service target
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTarget()
|
||||
{
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set envelope type
|
||||
*
|
||||
* @param string $envelopeType
|
||||
* @throws InvalidArgumentException
|
||||
* @return Service
|
||||
*/
|
||||
public function setEnvelope($envelopeType)
|
||||
{
|
||||
if (!in_array($envelopeType, $this->envelopeTypes)) {
|
||||
throw new InvalidArgumentException("Invalid envelope type '{$envelopeType}'; please specify one of (" . implode(', ', $this->envelopeTypes) . ')');
|
||||
}
|
||||
|
||||
$this->envelope = $envelopeType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get envelope type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvelope()
|
||||
{
|
||||
return $this->envelope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a parameter to the service
|
||||
*
|
||||
* @param string|array $type
|
||||
* @param array $options
|
||||
* @param int|null $order
|
||||
* @throws InvalidArgumentException
|
||||
* @return Service
|
||||
*/
|
||||
public function addParam($type, array $options = [], $order = null)
|
||||
{
|
||||
if (is_string($type)) {
|
||||
$type = $this->_validateParamType($type);
|
||||
} elseif (is_array($type)) {
|
||||
foreach ($type as $key => $paramType) {
|
||||
$type[$key] = $this->_validateParamType($paramType);
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException('Invalid param type provided');
|
||||
}
|
||||
|
||||
$paramOptions = [
|
||||
'type' => $type,
|
||||
];
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array($key, array_keys($this->paramOptionTypes))) {
|
||||
if (null !== ($callback = $this->paramOptionTypes[$key])) {
|
||||
if (!$callback($value)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$paramOptions[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->params[] = [
|
||||
'param' => $paramOptions,
|
||||
'order' => $order,
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add params
|
||||
*
|
||||
* Each param should be an array, and should include the key 'type'.
|
||||
*
|
||||
* @param array $params
|
||||
* @return Service
|
||||
*/
|
||||
public function addParams(array $params)
|
||||
{
|
||||
ksort($params);
|
||||
foreach ($params as $options) {
|
||||
if (!is_array($options)) {
|
||||
continue;
|
||||
}
|
||||
if (!array_key_exists('type', $options)) {
|
||||
continue;
|
||||
}
|
||||
$type = $options['type'];
|
||||
$order = (array_key_exists('order', $options)) ? $options['order'] : null;
|
||||
$this->addParam($type, $options, $order);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite all parameters
|
||||
*
|
||||
* @param array $params
|
||||
* @return Service
|
||||
*/
|
||||
public function setParams(array $params)
|
||||
{
|
||||
$this->params = [];
|
||||
|
||||
return $this->addParams($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all parameters
|
||||
*
|
||||
* Returns all params in specified order.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
$params = [];
|
||||
$index = 0;
|
||||
foreach ($this->params as $param) {
|
||||
if (null === $param['order']) {
|
||||
if (array_search($index, array_keys($params), true)) {
|
||||
++$index;
|
||||
}
|
||||
$params[$index] = $param['param'];
|
||||
++$index;
|
||||
} else {
|
||||
$params[$param['order']] = $param['param'];
|
||||
}
|
||||
}
|
||||
ksort($params);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set return type
|
||||
*
|
||||
* @param string|array $type
|
||||
* @throws InvalidArgumentException
|
||||
* @return Service
|
||||
*/
|
||||
public function setReturn($type)
|
||||
{
|
||||
if (is_string($type)) {
|
||||
$type = $this->_validateParamType($type, true);
|
||||
} elseif (is_array($type)) {
|
||||
foreach ($type as $key => $returnType) {
|
||||
$type[$key] = $this->_validateParamType($returnType, true);
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException("Invalid param type provided ('" . gettype($type) . "')");
|
||||
}
|
||||
$this->return = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get return type
|
||||
*
|
||||
* @return string|array
|
||||
*/
|
||||
public function getReturn()
|
||||
{
|
||||
return $this->return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast service description to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$envelope = $this->getEnvelope();
|
||||
$target = $this->getTarget();
|
||||
$transport = $this->getTransport();
|
||||
$parameters = $this->getParams();
|
||||
$returns = $this->getReturn();
|
||||
$name = $this->getName();
|
||||
|
||||
if (empty($target)) {
|
||||
return compact('envelope', 'transport', 'name', 'parameters', 'returns');
|
||||
}
|
||||
|
||||
return compact('envelope', 'target', 'transport', 'name', 'parameters', 'returns');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return JSON encoding of service
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
$service = [$this->getName() => $this->toArray()];
|
||||
|
||||
return \Zend\Json\Json::encode($service);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameter type
|
||||
*
|
||||
* @param string $type
|
||||
* @param bool $isReturn
|
||||
* @return string
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function _validateParamType($type, $isReturn = false)
|
||||
{
|
||||
if (!is_string($type)) {
|
||||
throw new InvalidArgumentException("Invalid param type provided ('{$type}')");
|
||||
}
|
||||
|
||||
if (!array_key_exists($type, $this->paramMap)) {
|
||||
$type = 'object';
|
||||
}
|
||||
|
||||
$paramType = $this->paramMap[$type];
|
||||
if (!$isReturn && ('null' == $paramType)) {
|
||||
throw new InvalidArgumentException("Invalid param type provided ('{$type}')");
|
||||
}
|
||||
|
||||
return $paramType;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user