Vendoring deprecated composer libs

This commit is contained in:
Frédéric Guillot
2018-06-21 14:13:41 -07:00
parent c73ac5f1f8
commit a491348d44
515 changed files with 5376 additions and 693 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace JsonRPC\Request;
/**
* Class BatchRequestParser
*
* @package JsonRPC\Request
* @author Frederic Guillot
*/
class BatchRequestParser extends RequestParser
{
/**
* Parse incoming request
*
* @access public
* @return string
*/
public function parse()
{
$responses = array();
foreach ($this->payload as $payload) {
$responses[] = RequestParser::create()
->withPayload($payload)
->withProcedureHandler($this->procedureHandler)
->withMiddlewareHandler($this->middlewareHandler)
->withLocalException($this->localExceptions)
->parse();
}
$responses = array_filter($responses);
return empty($responses) ? '' : '['.implode(',', $responses).']';
}
/**
* Return true if we have a batch request
*
* ex : [
* 0 => '...',
* 1 => '...',
* 2 => '...',
* 3 => '...',
* ]
*
* @static
* @access public
* @param array $payload
* @return bool
*/
public static function isBatchRequest(array $payload)
{
return array_keys($payload) === range(0, count($payload) - 1);
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace JsonRPC\Request;
/**
* Class RequestBuilder
*
* @package JsonRPC\Request
* @author Frederic Guillot
*/
class RequestBuilder
{
/**
* Request ID
*
* @access private
* @var mixed
*/
private $id = null;
/**
* Method name
*
* @access private
* @var string
*/
private $procedure = '';
/**
* Method arguments
*
* @access private
* @var array
*/
private $params = array();
/**
* Additional request attributes
*
* @access private
* @var array
*/
private $reqattrs = array();
/**
* Get new object instance
*
* @static
* @access public
* @return RequestBuilder
*/
public static function create()
{
return new static();
}
/**
* Set id
*
* @access public
* @param null $id
* @return RequestBuilder
*/
public function withId($id)
{
$this->id = $id;
return $this;
}
/**
* Set method
*
* @access public
* @param string $procedure
* @return RequestBuilder
*/
public function withProcedure($procedure)
{
$this->procedure = $procedure;
return $this;
}
/**
* Set parameters
*
* @access public
* @param array $params
* @return RequestBuilder
*/
public function withParams(array $params)
{
$this->params = $params;
return $this;
}
/**
* Set additional request attributes
*
* @access public
* @param array $reqattrs
* @return RequestBuilder
*/
public function withRequestAttributes(array $reqattrs)
{
$this->reqattrs = $reqattrs;
return $this;
}
/**
* Build the payload
*
* @access public
* @return string
*/
public function build()
{
$payload = array_merge_recursive($this->reqattrs, array(
'jsonrpc' => '2.0',
'method' => $this->procedure,
'id' => $this->id ?: mt_rand(),
));
if (! empty($this->params)) {
$payload['params'] = $this->params;
}
return json_encode($payload);
}
}

View File

@@ -0,0 +1,200 @@
<?php
namespace JsonRPC\Request;
use Exception;
use JsonRPC\Exception\AccessDeniedException;
use JsonRPC\Exception\AuthenticationFailureException;
use JsonRPC\Exception\InvalidJsonRpcFormatException;
use JsonRPC\MiddlewareHandler;
use JsonRPC\ProcedureHandler;
use JsonRPC\Response\ResponseBuilder;
use JsonRPC\Validator\JsonFormatValidator;
use JsonRPC\Validator\RpcFormatValidator;
/**
* Class RequestParser
*
* @package JsonRPC
* @author Frederic Guillot
*/
class RequestParser
{
/**
* Request payload
*
* @access protected
* @var mixed
*/
protected $payload;
/**
* List of exceptions that should not be relayed to the client
*
* @access protected
* @var array()
*/
protected $localExceptions = array(
'JsonRPC\Exception\AuthenticationFailureException',
'JsonRPC\Exception\AccessDeniedException',
);
/**
* ProcedureHandler
*
* @access protected
* @var ProcedureHandler
*/
protected $procedureHandler;
/**
* MiddlewareHandler
*
* @access protected
* @var MiddlewareHandler
*/
protected $middlewareHandler;
/**
* Get new object instance
*
* @static
* @access public
* @return RequestParser
*/
public static function create()
{
return new static();
}
/**
* Set payload
*
* @access public
* @param mixed $payload
* @return $this
*/
public function withPayload($payload)
{
$this->payload = $payload;
return $this;
}
/**
* Exception classes that should not be relayed to the client
*
* @access public
* @param mixed $exception
* @return $this
*/
public function withLocalException($exception)
{
if (is_array($exception)) {
$this->localExceptions = array_merge($this->localExceptions, $exception);
} else {
$this->localExceptions[] = $exception;
}
return $this;
}
/**
* Set procedure handler
*
* @access public
* @param ProcedureHandler $procedureHandler
* @return $this
*/
public function withProcedureHandler(ProcedureHandler $procedureHandler)
{
$this->procedureHandler = $procedureHandler;
return $this;
}
/**
* Set middleware handler
*
* @access public
* @param MiddlewareHandler $middlewareHandler
* @return $this
*/
public function withMiddlewareHandler(MiddlewareHandler $middlewareHandler)
{
$this->middlewareHandler = $middlewareHandler;
return $this;
}
/**
* Parse incoming request
*
* @access public
* @return string
* @throws AccessDeniedException
* @throws AuthenticationFailureException
*/
public function parse()
{
try {
JsonFormatValidator::validate($this->payload);
RpcFormatValidator::validate($this->payload);
$this->middlewareHandler
->withProcedure($this->payload['method'])
->execute();
$result = $this->procedureHandler->executeProcedure(
$this->payload['method'],
empty($this->payload['params']) ? array() : $this->payload['params']
);
if (! $this->isNotification()) {
return ResponseBuilder::create()
->withId($this->payload['id'])
->withResult($result)
->build();
}
} catch (Exception $e) {
return $this->handleExceptions($e);
}
return '';
}
/**
* Handle exceptions
*
* @access protected
* @param Exception $e
* @return string
* @throws Exception
*/
protected function handleExceptions(Exception $e)
{
foreach ($this->localExceptions as $exception) {
if ($e instanceof $exception) {
throw $e;
}
}
if ($e instanceof InvalidJsonRpcFormatException || ! $this->isNotification()) {
return ResponseBuilder::create()
->withId(isset($this->payload['id']) ? $this->payload['id'] : null)
->withException($e)
->build();
}
return '';
}
/**
* Return true if the message is a notification
*
* @access protected
* @return bool
*/
protected function isNotification()
{
return is_array($this->payload) && !isset($this->payload['id']);
}
}