installed plugin Infinite Uploads version 2.0.8

This commit is contained in:
2025-05-02 12:03:21 +00:00
committed by Gitium
parent 7ca941b591
commit 8fefb19ab4
1179 changed files with 99739 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Aws\ResultInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
/**
* @internal
*/
abstract class AbstractParser
{
/** @var \Aws\Api\Service Representation of the service API*/
protected $api;
/** @var callable */
protected $parser;
/**
* @param Service $api Service description.
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api)
{
$this->api = $api;
}
/**
* @param CommandInterface $command Command that was executed.
* @param ResponseInterface $response Response that was received.
*
* @return ResultInterface
*/
public abstract function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response);
public abstract function parseMemberFromStream(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, $response);
}

View File

@ -0,0 +1,140 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Result;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
/**
* @internal
*/
abstract class AbstractRestParser extends \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractParser
{
use PayloadParserTrait;
/**
* Parses a payload from a response.
*
* @param ResponseInterface $response Response to parse.
* @param StructureShape $member Member to parse
* @param array $result Result value
*
* @return mixed
*/
protected abstract function payload(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, array &$result);
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response)
{
$output = $this->api->getOperation($command->getName())->getOutput();
$result = [];
if ($payload = $output['payload']) {
$this->extractPayload($payload, $output, $response, $result);
}
foreach ($output->getMembers() as $name => $member) {
switch ($member['location']) {
case 'header':
$this->extractHeader($name, $member, $response, $result);
break;
case 'headers':
$this->extractHeaders($name, $member, $response, $result);
break;
case 'statusCode':
$this->extractStatus($name, $response, $result);
break;
}
}
if (!$payload && $response->getBody()->getSize() > 0 && count($output->getMembers()) > 0) {
// if no payload was found, then parse the contents of the body
$this->payload($response, $output, $result);
}
return new \UglyRobot\Infinite_Uploads\Aws\Result($result);
}
private function extractPayload($payload, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $output, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, array &$result)
{
$member = $output->getMember($payload);
if (!empty($member['eventstream'])) {
$result[$payload] = new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\EventParsingIterator($response->getBody(), $member, $this);
} else {
if ($member instanceof StructureShape) {
// Structure members parse top-level data into a specific key.
$result[$payload] = [];
$this->payload($response, $member, $result[$payload]);
} else {
// Streaming data is just the stream from the response body.
$result[$payload] = $response->getBody();
}
}
}
/**
* Extract a single header from the response into the result.
*/
private function extractHeader($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, &$result)
{
$value = $response->getHeaderLine($shape['locationName'] ?: $name);
switch ($shape->getType()) {
case 'float':
case 'double':
$value = (double) $value;
break;
case 'long':
$value = (int) $value;
break;
case 'boolean':
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
case 'blob':
$value = base64_decode($value);
break;
case 'timestamp':
try {
$value = \UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult::fromTimestamp($value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null);
break;
} catch (\Exception $e) {
// If the value cannot be parsed, then do not add it to the
// output structure.
return;
}
case 'string':
try {
if ($shape['jsonvalue']) {
$value = $this->parseJson(base64_decode($value), $response);
}
// If value is not set, do not add to output structure.
if (!isset($value)) {
return;
}
break;
} catch (\Exception $e) {
//If the value cannot be parsed, then do not add it to the
//output structure.
return;
}
}
$result[$name] = $value;
}
/**
* Extract a map of headers with an optional prefix from the response.
*/
private function extractHeaders($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, &$result)
{
// Check if the headers are prefixed by a location name
$result[$name] = [];
$prefix = $shape['locationName'];
$prefixLen = strlen($prefix);
foreach ($response->getHeaders() as $k => $values) {
if (!$prefixLen) {
$result[$name][$k] = implode(', ', $values);
} elseif (stripos($k, $prefix) === 0) {
$result[$name][substr($k, $prefixLen)] = implode(', ', $values);
}
}
}
/**
* Places the status code of the response into the result array.
*/
private function extractStatus($name, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, array &$result)
{
$result[$name] = (int) $response->getStatusCode();
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Aws\Exception\AwsException;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7;
/**
* @internal Decorates a parser and validates the x-amz-crc32 header.
*/
class Crc32ValidatingParser extends \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractParser
{
/**
* @param callable $parser Parser to wrap.
*/
public function __construct(callable $parser)
{
$this->parser = $parser;
}
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response)
{
if ($expected = $response->getHeaderLine('x-amz-crc32')) {
$hash = hexdec(\UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\hash($response->getBody(), 'crc32b'));
if ($expected != $hash) {
throw new \UglyRobot\Infinite_Uploads\Aws\Exception\AwsException("crc32 mismatch. Expected {$expected}, found {$hash}.", $command, ['code' => 'ClientChecksumMismatch', 'connection_error' => true, 'response' => $response]);
}
}
$fn = $this->parser;
return $fn($command, $response);
}
public function parseMemberFromStream(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, $response)
{
return $this->parser->parseMemberFromStream($stream, $member, $response);
}
}

View File

@ -0,0 +1,241 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use Iterator;
use UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException;
/**
* @internal Implements a decoder for a binary encoded event stream that will
* decode, validate, and provide individual events from the stream.
*/
class DecodingEventStreamIterator implements \Iterator
{
const HEADERS = 'headers';
const PAYLOAD = 'payload';
const LENGTH_TOTAL = 'total_length';
const LENGTH_HEADERS = 'headers_length';
const CRC_PRELUDE = 'prelude_crc';
const BYTES_PRELUDE = 12;
const BYTES_TRAILING = 4;
private static $preludeFormat = [self::LENGTH_TOTAL => 'decodeUint32', self::LENGTH_HEADERS => 'decodeUint32', self::CRC_PRELUDE => 'decodeUint32'];
private static $lengthFormatMap = [1 => 'decodeUint8', 2 => 'decodeUint16', 4 => 'decodeUint32', 8 => 'decodeUint64'];
private static $headerTypeMap = [0 => 'decodeBooleanTrue', 1 => 'decodeBooleanFalse', 2 => 'decodeInt8', 3 => 'decodeInt16', 4 => 'decodeInt32', 5 => 'decodeInt64', 6 => 'decodeBytes', 7 => 'decodeString', 8 => 'decodeTimestamp', 9 => 'decodeUuid'];
/** @var StreamInterface Stream of eventstream shape to parse. */
private $stream;
/** @var array Currently parsed event. */
private $currentEvent;
/** @var int Current in-order event key. */
private $key;
/** @var resource|\HashContext CRC32 hash context for event validation */
private $hashContext;
/** @var int $currentPosition */
private $currentPosition;
/**
* DecodingEventStreamIterator constructor.
*
* @param StreamInterface $stream
*/
public function __construct(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream)
{
$this->stream = $stream;
$this->rewind();
}
private function parseHeaders($headerBytes)
{
$headers = [];
$bytesRead = 0;
while ($bytesRead < $headerBytes) {
list($key, $numBytes) = $this->decodeString(1);
$bytesRead += $numBytes;
list($type, $numBytes) = $this->decodeUint8();
$bytesRead += $numBytes;
$f = self::$headerTypeMap[$type];
list($value, $numBytes) = $this->{$f}();
$bytesRead += $numBytes;
if (isset($headers[$key])) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Duplicate key in event headers.');
}
$headers[$key] = $value;
}
return [$headers, $bytesRead];
}
private function parsePrelude()
{
$prelude = [];
$bytesRead = 0;
$calculatedCrc = null;
foreach (self::$preludeFormat as $key => $decodeFunction) {
if ($key === self::CRC_PRELUDE) {
$hashCopy = hash_copy($this->hashContext);
$calculatedCrc = hash_final($this->hashContext, true);
$this->hashContext = $hashCopy;
}
list($value, $numBytes) = $this->{$decodeFunction}();
$bytesRead += $numBytes;
$prelude[$key] = $value;
}
if (unpack('N', $calculatedCrc)[1] !== $prelude[self::CRC_PRELUDE]) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Prelude checksum mismatch.');
}
return [$prelude, $bytesRead];
}
private function parseEvent()
{
$event = [];
if ($this->stream->tell() < $this->stream->getSize()) {
$this->hashContext = hash_init('crc32b');
$bytesLeft = $this->stream->getSize() - $this->stream->tell();
list($prelude, $numBytes) = $this->parsePrelude();
if ($prelude[self::LENGTH_TOTAL] > $bytesLeft) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Message length too long.');
}
$bytesLeft -= $numBytes;
if ($prelude[self::LENGTH_HEADERS] > $bytesLeft) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Headers length too long.');
}
list($event[self::HEADERS], $numBytes) = $this->parseHeaders($prelude[self::LENGTH_HEADERS]);
$event[self::PAYLOAD] = \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\stream_for($this->readAndHashBytes($prelude[self::LENGTH_TOTAL] - self::BYTES_PRELUDE - $numBytes - self::BYTES_TRAILING));
$calculatedCrc = hash_final($this->hashContext, true);
$messageCrc = $this->stream->read(4);
if ($calculatedCrc !== $messageCrc) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Message checksum mismatch.');
}
}
return $event;
}
// Iterator Functionality
/**
* @return array
*/
public function current()
{
return $this->currentEvent;
}
/**
* @return int
*/
public function key()
{
return $this->key;
}
public function next()
{
$this->currentPosition = $this->stream->tell();
if ($this->valid()) {
$this->key++;
$this->currentEvent = $this->parseEvent();
}
}
public function rewind()
{
$this->stream->rewind();
$this->key = 0;
$this->currentPosition = 0;
$this->currentEvent = $this->parseEvent();
}
/**
* @return bool
*/
public function valid()
{
return $this->currentPosition < $this->stream->getSize();
}
// Decoding Utilities
private function readAndHashBytes($num)
{
$bytes = $this->stream->read($num);
hash_update($this->hashContext, $bytes);
return $bytes;
}
private function decodeBooleanTrue()
{
return [true, 0];
}
private function decodeBooleanFalse()
{
return [false, 0];
}
private function uintToInt($val, $size)
{
$signedCap = pow(2, $size - 1);
if ($val > $signedCap) {
$val -= 2 * $signedCap;
}
return $val;
}
private function decodeInt8()
{
$val = (int) unpack('C', $this->readAndHashBytes(1))[1];
return [$this->uintToInt($val, 8), 1];
}
private function decodeUint8()
{
return [unpack('C', $this->readAndHashBytes(1))[1], 1];
}
private function decodeInt16()
{
$val = (int) unpack('n', $this->readAndHashBytes(2))[1];
return [$this->uintToInt($val, 16), 2];
}
private function decodeUint16()
{
return [unpack('n', $this->readAndHashBytes(2))[1], 2];
}
private function decodeInt32()
{
$val = (int) unpack('N', $this->readAndHashBytes(4))[1];
return [$this->uintToInt($val, 32), 4];
}
private function decodeUint32()
{
return [unpack('N', $this->readAndHashBytes(4))[1], 4];
}
private function decodeInt64()
{
$val = $this->unpackInt64($this->readAndHashBytes(8))[1];
return [$this->uintToInt($val, 64), 8];
}
private function decodeUint64()
{
return [$this->unpackInt64($this->readAndHashBytes(8))[1], 8];
}
private function unpackInt64($bytes)
{
if (version_compare(PHP_VERSION, '5.6.3', '<')) {
$d = unpack('N2', $bytes);
return [1 => $d[1] << 32 | $d[2]];
}
return unpack('J', $bytes);
}
private function decodeBytes($lengthBytes = 2)
{
if (!isset(self::$lengthFormatMap[$lengthBytes])) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Undefined variable length format.');
}
$f = self::$lengthFormatMap[$lengthBytes];
list($len, $bytes) = $this->{$f}();
return [$this->readAndHashBytes($len), $len + $bytes];
}
private function decodeString($lengthBytes = 2)
{
if (!isset(self::$lengthFormatMap[$lengthBytes])) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Undefined variable length format.');
}
$f = self::$lengthFormatMap[$lengthBytes];
list($len, $bytes) = $this->{$f}();
return [$this->readAndHashBytes($len), $len + $bytes];
}
private function decodeTimestamp()
{
list($val, $bytes) = $this->decodeInt64();
return [\UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult::createFromFormat('U.u', $val / 1000), $bytes];
}
private function decodeUuid()
{
$val = unpack('H32', $this->readAndHashBytes(16))[1];
return [substr($val, 0, 8) . '-' . substr($val, 8, 4) . '-' . substr($val, 12, 4) . '-' . substr($val, 16, 4) . '-' . substr($val, 20, 12), 16];
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use Iterator;
use UglyRobot\Infinite_Uploads\Aws\Exception\EventStreamDataException;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
/**
* @internal Implements a decoder for a binary encoded event stream that will
* decode, validate, and provide individual events from the stream.
*/
class EventParsingIterator implements \Iterator
{
/** @var StreamInterface */
private $decodingIterator;
/** @var StructureShape */
private $shape;
/** @var AbstractParser */
private $parser;
public function __construct(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractParser $parser)
{
$this->decodingIterator = new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\DecodingEventStreamIterator($stream);
$this->shape = $shape;
$this->parser = $parser;
}
public function current()
{
return $this->parseEvent($this->decodingIterator->current());
}
public function key()
{
return $this->decodingIterator->key();
}
public function next()
{
$this->decodingIterator->next();
}
public function rewind()
{
$this->decodingIterator->rewind();
}
public function valid()
{
return $this->decodingIterator->valid();
}
private function parseEvent(array $event)
{
if (!empty($event['headers'][':message-type'])) {
if ($event['headers'][':message-type'] === 'error') {
return $this->parseError($event);
}
if ($event['headers'][':message-type'] !== 'event') {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Failed to parse unknown message type.');
}
}
if (empty($event['headers'][':event-type'])) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Failed to parse without event type.');
}
$eventShape = $this->shape->getMember($event['headers'][':event-type']);
$parsedEvent = [];
foreach ($eventShape['members'] as $shape => $details) {
if (!empty($details['eventpayload'])) {
$payloadShape = $eventShape->getMember($shape);
if ($payloadShape['type'] === 'blob') {
$parsedEvent[$shape] = $event['payload'];
} else {
$parsedEvent[$shape] = $this->parser->parseMemberFromStream($event['payload'], $payloadShape, null);
}
} else {
$parsedEvent[$shape] = $event['headers'][$shape];
}
}
return [$event['headers'][':event-type'] => $parsedEvent];
}
private function parseError(array $event)
{
throw new \UglyRobot\Infinite_Uploads\Aws\Exception\EventStreamDataException($event['headers'][':error-code'], $event['headers'][':error-message']);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception;
use UglyRobot\Infinite_Uploads\Aws\HasMonitoringEventsTrait;
use UglyRobot\Infinite_Uploads\Aws\MonitoringEventsInterface;
use UglyRobot\Infinite_Uploads\Aws\ResponseContainerInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
class ParserException extends \RuntimeException implements \UglyRobot\Infinite_Uploads\Aws\MonitoringEventsInterface, \UglyRobot\Infinite_Uploads\Aws\ResponseContainerInterface
{
use HasMonitoringEventsTrait;
private $errorCode;
private $requestId;
private $response;
public function __construct($message = '', $code = 0, $previous = null, array $context = [])
{
$this->errorCode = isset($context['error_code']) ? $context['error_code'] : null;
$this->requestId = isset($context['request_id']) ? $context['request_id'] : null;
$this->response = isset($context['response']) ? $context['response'] : null;
parent::__construct($message, $code, $previous);
}
/**
* Get the error code, if any.
*
* @return string|null
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* Get the request ID, if any.
*
* @return string|null
*/
public function getRequestId()
{
return $this->requestId;
}
/**
* Get the received HTTP response if any.
*
* @return ResponseInterface|null
*/
public function getResponse()
{
return $this->response;
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
/**
* @internal Implements standard JSON parsing.
*/
class JsonParser
{
public function parse(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
if ($value === null) {
return $value;
}
switch ($shape['type']) {
case 'structure':
$target = [];
foreach ($shape->getMembers() as $name => $member) {
$locationName = $member['locationName'] ?: $name;
if (isset($value[$locationName])) {
$target[$name] = $this->parse($member, $value[$locationName]);
}
}
return $target;
case 'list':
$member = $shape->getMember();
$target = [];
foreach ($value as $v) {
$target[] = $this->parse($member, $v);
}
return $target;
case 'map':
$values = $shape->getValue();
$target = [];
foreach ($value as $k => $v) {
$target[$k] = $this->parse($values, $v);
}
return $target;
case 'timestamp':
return \UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult::fromTimestamp($value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null);
case 'blob':
return base64_decode($value);
default:
return $value;
}
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Result;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
/**
* @internal Implements JSON-RPC parsing (e.g., DynamoDB)
*/
class JsonRpcParser extends \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractParser
{
use PayloadParserTrait;
/**
* @param Service $api Service description
* @param JsonParser $parser JSON body builder
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, \UglyRobot\Infinite_Uploads\Aws\Api\Parser\JsonParser $parser = null)
{
parent::__construct($api);
$this->parser = $parser ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\JsonParser();
}
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response)
{
$operation = $this->api->getOperation($command->getName());
$result = null === $operation['output'] ? null : $this->parseMemberFromStream($response->getBody(), $operation->getOutput(), $response);
return new \UglyRobot\Infinite_Uploads\Aws\Result($result ?: []);
}
public function parseMemberFromStream(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, $response)
{
return $this->parser->parse($member, $this->parseJson($stream, $response));
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
trait MetadataParserTrait
{
/**
* Extract a single header from the response into the result.
*/
protected function extractHeader($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, &$result)
{
$value = $response->getHeaderLine($shape['locationName'] ?: $name);
switch ($shape->getType()) {
case 'float':
case 'double':
$value = (double) $value;
break;
case 'long':
$value = (int) $value;
break;
case 'boolean':
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
break;
case 'blob':
$value = base64_decode($value);
break;
case 'timestamp':
try {
$value = \UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult::fromTimestamp($value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null);
break;
} catch (\Exception $e) {
// If the value cannot be parsed, then do not add it to the
// output structure.
return;
}
case 'string':
if ($shape['jsonvalue']) {
$value = $this->parseJson(base64_decode($value), $response);
}
break;
}
$result[$name] = $value;
}
/**
* Extract a map of headers with an optional prefix from the response.
*/
protected function extractHeaders($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, &$result)
{
// Check if the headers are prefixed by a location name
$result[$name] = [];
$prefix = $shape['locationName'];
$prefixLen = strlen($prefix);
foreach ($response->getHeaders() as $k => $values) {
if (!$prefixLen) {
$result[$name][$k] = implode(', ', $values);
} elseif (stripos($k, $prefix) === 0) {
$result[$name][substr($k, $prefixLen)] = implode(', ', $values);
}
}
}
/**
* Places the status code of the response into the result array.
*/
protected function extractStatus($name, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, array &$result)
{
$result[$name] = (int) $response->getStatusCode();
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
trait PayloadParserTrait
{
/**
* @param string $json
*
* @throws ParserException
*
* @return array
*/
private function parseJson($json, $response)
{
$jsonPayload = json_decode($json, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Error parsing JSON: ' . json_last_error_msg(), 0, null, ['response' => $response]);
}
return $jsonPayload;
}
/**
* @param string $xml
*
* @throws ParserException
*
* @return \SimpleXMLElement
*/
protected function parseXml($xml, $response)
{
$priorSetting = libxml_use_internal_errors(true);
try {
libxml_clear_errors();
$xmlPayload = new \SimpleXMLElement($xml);
if ($error = libxml_get_last_error()) {
throw new \RuntimeException($error->message);
}
} catch (\Exception $e) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException("Error parsing XML: {$e->getMessage()}", 0, $e, ['response' => $response]);
} finally {
libxml_use_internal_errors($priorSetting);
}
return $xmlPayload;
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Result;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
/**
* @internal Parses query (XML) responses (e.g., EC2, SQS, and many others)
*/
class QueryParser extends \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractParser
{
use PayloadParserTrait;
/** @var bool */
private $honorResultWrapper;
/**
* @param Service $api Service description
* @param XmlParser $xmlParser Optional XML parser
* @param bool $honorResultWrapper Set to false to disable the peeling
* back of result wrappers from the
* output structure.
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, \UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser $xmlParser = null, $honorResultWrapper = true)
{
parent::__construct($api);
$this->parser = $xmlParser ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser();
$this->honorResultWrapper = $honorResultWrapper;
}
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response)
{
$output = $this->api->getOperation($command->getName())->getOutput();
$xml = $this->parseXml($response->getBody(), $response);
if ($this->honorResultWrapper && $output['resultWrapper']) {
$xml = $xml->{$output['resultWrapper']};
}
return new \UglyRobot\Infinite_Uploads\Aws\Result($this->parser->parse($output, $xml));
}
public function parseMemberFromStream(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, $response)
{
$xml = $this->parseXml($stream, $response);
return $this->parser->parse($member, $xml);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
/**
* @internal Implements REST-JSON parsing (e.g., Glacier, Elastic Transcoder)
*/
class RestJsonParser extends \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractRestParser
{
use PayloadParserTrait;
/**
* @param Service $api Service description
* @param JsonParser $parser JSON body builder
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, \UglyRobot\Infinite_Uploads\Aws\Api\Parser\JsonParser $parser = null)
{
parent::__construct($api);
$this->parser = $parser ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\JsonParser();
}
protected function payload(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, array &$result)
{
$jsonBody = $this->parseJson($response->getBody(), $response);
if ($jsonBody) {
$result += $this->parser->parse($member, $jsonBody);
}
}
public function parseMemberFromStream(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, $response)
{
$jsonBody = $this->parseJson($stream, $response);
if ($jsonBody) {
return $this->parser->parse($member, $jsonBody);
}
return [];
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface;
/**
* @internal Implements REST-XML parsing (e.g., S3, CloudFront, etc...)
*/
class RestXmlParser extends \UglyRobot\Infinite_Uploads\Aws\Api\Parser\AbstractRestParser
{
use PayloadParserTrait;
/**
* @param Service $api Service description
* @param XmlParser $parser XML body parser
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, \UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser $parser = null)
{
parent::__construct($api);
$this->parser = $parser ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser();
}
protected function payload(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, array &$result)
{
$result += $this->parseMemberFromStream($response->getBody(), $member, $response);
}
public function parseMemberFromStream(\UglyRobot\Infinite_Uploads\Psr\Http\Message\StreamInterface $stream, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, $response)
{
$xml = $this->parseXml($stream, $response);
return $this->parser->parse($member, $xml);
}
}

View File

@ -0,0 +1,119 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Parser;
use UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult;
use UglyRobot\Infinite_Uploads\Aws\Api\ListShape;
use UglyRobot\Infinite_Uploads\Aws\Api\MapShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
/**
* @internal Implements standard XML parsing for REST-XML and Query protocols.
*/
class XmlParser
{
public function parse(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, \SimpleXMLElement $value)
{
return $this->dispatch($shape, $value);
}
private function dispatch($shape, \SimpleXMLElement $value)
{
static $methods = ['structure' => 'parse_structure', 'list' => 'parse_list', 'map' => 'parse_map', 'blob' => 'parse_blob', 'boolean' => 'parse_boolean', 'integer' => 'parse_integer', 'float' => 'parse_float', 'double' => 'parse_float', 'timestamp' => 'parse_timestamp'];
$type = $shape['type'];
if (isset($methods[$type])) {
return $this->{$methods[$type]}($shape, $value);
}
return (string) $value;
}
private function parse_structure(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, \SimpleXMLElement $value)
{
$target = [];
foreach ($shape->getMembers() as $name => $member) {
// Extract the name of the XML node
$node = $this->memberKey($member, $name);
if (isset($value->{$node})) {
$target[$name] = $this->dispatch($member, $value->{$node});
} else {
$memberShape = $shape->getMember($name);
if (!empty($memberShape['xmlAttribute'])) {
$target[$name] = $this->parse_xml_attribute($shape, $memberShape, $value);
}
}
}
return $target;
}
private function memberKey(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name)
{
if (null !== $shape['locationName']) {
return $shape['locationName'];
}
if ($shape instanceof ListShape && $shape['flattened']) {
return $shape->getMember()['locationName'] ?: $name;
}
return $name;
}
private function parse_list(\UglyRobot\Infinite_Uploads\Aws\Api\ListShape $shape, \SimpleXMLElement $value)
{
$target = [];
$member = $shape->getMember();
if (!$shape['flattened']) {
$value = $value->{$member['locationName'] ?: 'member'};
}
foreach ($value as $v) {
$target[] = $this->dispatch($member, $v);
}
return $target;
}
private function parse_map(\UglyRobot\Infinite_Uploads\Aws\Api\MapShape $shape, \SimpleXMLElement $value)
{
$target = [];
if (!$shape['flattened']) {
$value = $value->entry;
}
$mapKey = $shape->getKey();
$mapValue = $shape->getValue();
$keyName = $shape->getKey()['locationName'] ?: 'key';
$valueName = $shape->getValue()['locationName'] ?: 'value';
foreach ($value as $node) {
$key = $this->dispatch($mapKey, $node->{$keyName});
$value = $this->dispatch($mapValue, $node->{$valueName});
$target[$key] = $value;
}
return $target;
}
private function parse_blob(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
return base64_decode((string) $value);
}
private function parse_float(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
return (double) (string) $value;
}
private function parse_integer(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
return (int) (string) $value;
}
private function parse_boolean(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
return $value == 'true';
}
private function parse_timestamp(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
if (is_string($value) || is_int($value) || is_object($value) && method_exists($value, '__toString')) {
return \UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult::fromTimestamp((string) $value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null);
}
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Invalid timestamp value passed to XmlParser::parse_timestamp');
}
private function parse_xml_attribute(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $memberShape, $value)
{
$namespace = $shape['xmlNamespace']['uri'] ? $shape['xmlNamespace']['uri'] : '';
$prefix = $shape['xmlNamespace']['prefix'] ? $shape['xmlNamespace']['prefix'] : '';
if (!empty($prefix)) {
$prefix .= ':';
}
$key = str_replace($prefix, '', $memberShape['locationName']);
$attributes = $value->attributes($namespace);
return isset($attributes[$key]) ? (string) $attributes[$key] : null;
}
}