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,54 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Base class that is used by most API shapes
*/
abstract class AbstractModel implements \ArrayAccess
{
/** @var array */
protected $definition;
/** @var ShapeMap */
protected $shapeMap;
/**
* @param array $definition Service description
* @param ShapeMap $shapeMap Shapemap used for creating shapes
*/
public function __construct(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
$this->definition = $definition;
$this->shapeMap = $shapeMap;
}
public function toArray()
{
return $this->definition;
}
public function offsetGet($offset)
{
return isset($this->definition[$offset]) ? $this->definition[$offset] : null;
}
public function offsetSet($offset, $value)
{
$this->definition[$offset] = $value;
}
public function offsetExists($offset)
{
return isset($this->definition[$offset]);
}
public function offsetUnset($offset)
{
unset($this->definition[$offset]);
}
protected function shapeAt($key)
{
if (!isset($this->definition[$key])) {
throw new \InvalidArgumentException('Expected shape definition at ' . $key);
}
return $this->shapeFor($this->definition[$key]);
}
protected function shapeFor(array $definition)
{
return isset($definition['shape']) ? $this->shapeMap->resolve($definition) : \UglyRobot\Infinite_Uploads\Aws\Api\Shape::create($definition, $this->shapeMap);
}
}

View File

@ -0,0 +1,212 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
use UglyRobot\Infinite_Uploads\Aws\Exception\UnresolvedApiException;
/**
* API providers.
*
* An API provider is a function that accepts a type, service, and version and
* returns an array of API data on success or NULL if no API data can be created
* for the provided arguments.
*
* You can wrap your calls to an API provider with the
* {@see ApiProvider::resolve} method to ensure that API data is created. If the
* API data is not created, then the resolve() method will throw a
* {@see Aws\Exception\UnresolvedApiException}.
*
* use Aws\Api\ApiProvider;
* $provider = ApiProvider::defaultProvider();
* // Returns an array or NULL.
* $data = $provider('api', 's3', '2006-03-01');
* // Returns an array or throws.
* $data = ApiProvider::resolve($provider, 'api', 'elasticfood', '2020-01-01');
*
* You can compose multiple providers into a single provider using
* {@see Aws\or_chain}. This method accepts providers as arguments and
* returns a new function that will invoke each provider until a non-null value
* is returned.
*
* $a = ApiProvider::filesystem(sys_get_temp_dir() . '/aws-beta-models');
* $b = ApiProvider::manifest();
*
* $c = \Aws\or_chain($a, $b);
* $data = $c('api', 'betaservice', '2015-08-08'); // $a handles this.
* $data = $c('api', 's3', '2006-03-01'); // $b handles this.
* $data = $c('api', 'invalid', '2014-12-15'); // Neither handles this.
*/
class ApiProvider
{
/** @var array A map of public API type names to their file suffix. */
private static $typeMap = ['api' => 'api-2', 'paginator' => 'paginators-1', 'waiter' => 'waiters-2', 'docs' => 'docs-2'];
/** @var array API manifest */
private $manifest;
/** @var string The directory containing service models. */
private $modelsDir;
/**
* Resolves an API provider and ensures a non-null return value.
*
* @param callable $provider Provider function to invoke.
* @param string $type Type of data ('api', 'waiter', 'paginator').
* @param string $service Service name.
* @param string $version API version.
*
* @return array
* @throws UnresolvedApiException
*/
public static function resolve(callable $provider, $type, $service, $version)
{
// Execute the provider and return the result, if there is one.
$result = $provider($type, $service, $version);
if (is_array($result)) {
if (!isset($result['metadata']['serviceIdentifier'])) {
$result['metadata']['serviceIdentifier'] = $service;
}
return $result;
}
// Throw an exception with a message depending on the inputs.
if (!isset(self::$typeMap[$type])) {
$msg = "The type must be one of: " . implode(', ', self::$typeMap);
} elseif ($service) {
$msg = "The {$service} service does not have version: {$version}.";
} else {
$msg = "You must specify a service name to retrieve its API data.";
}
throw new \UglyRobot\Infinite_Uploads\Aws\Exception\UnresolvedApiException($msg);
}
/**
* Default SDK API provider.
*
* This provider loads pre-built manifest data from the `data` directory.
*
* @return self
*/
public static function defaultProvider()
{
return new self(__DIR__ . '/../data', \UglyRobot\Infinite_Uploads\Aws\manifest());
}
/**
* Loads API data after resolving the version to the latest, compatible,
* available version based on the provided manifest data.
*
* Manifest data is essentially an associative array of service names to
* associative arrays of API version aliases.
*
* [
* ...
* 'ec2' => [
* 'latest' => '2014-10-01',
* '2014-10-01' => '2014-10-01',
* '2014-09-01' => '2014-10-01',
* '2014-06-15' => '2014-10-01',
* ...
* ],
* 'ecs' => [...],
* 'elasticache' => [...],
* ...
* ]
*
* @param string $dir Directory containing service models.
* @param array $manifest The API version manifest data.
*
* @return self
*/
public static function manifest($dir, array $manifest)
{
return new self($dir, $manifest);
}
/**
* Loads API data from the specified directory.
*
* If "latest" is specified as the version, this provider must glob the
* directory to find which is the latest available version.
*
* @param string $dir Directory containing service models.
*
* @return self
* @throws \InvalidArgumentException if the provided `$dir` is invalid.
*/
public static function filesystem($dir)
{
return new self($dir);
}
/**
* Retrieves a list of valid versions for the specified service.
*
* @param string $service Service name
*
* @return array
*/
public function getVersions($service)
{
if (!isset($this->manifest)) {
$this->buildVersionsList($service);
}
if (!isset($this->manifest[$service]['versions'])) {
return [];
}
return array_values(array_unique($this->manifest[$service]['versions']));
}
/**
* Execute the provider.
*
* @param string $type Type of data ('api', 'waiter', 'paginator').
* @param string $service Service name.
* @param string $version API version.
*
* @return array|null
*/
public function __invoke($type, $service, $version)
{
// Resolve the type or return null.
if (isset(self::$typeMap[$type])) {
$type = self::$typeMap[$type];
} else {
return null;
}
// Resolve the version or return null.
if (!isset($this->manifest)) {
$this->buildVersionsList($service);
}
if (!isset($this->manifest[$service]['versions'][$version])) {
return null;
}
$version = $this->manifest[$service]['versions'][$version];
$path = "{$this->modelsDir}/{$service}/{$version}/{$type}.json";
try {
return \UglyRobot\Infinite_Uploads\Aws\load_compiled_json($path);
} catch (\InvalidArgumentException $e) {
return null;
}
}
/**
* @param string $modelsDir Directory containing service models.
* @param array $manifest The API version manifest data.
*/
private function __construct($modelsDir, array $manifest = null)
{
$this->manifest = $manifest;
$this->modelsDir = rtrim($modelsDir, '/');
if (!is_dir($this->modelsDir)) {
throw new \InvalidArgumentException("The specified models directory, {$modelsDir}, was not found.");
}
}
/**
* Build the versions list for the specified service by globbing the dir.
*/
private function buildVersionsList($service)
{
$dir = "{$this->modelsDir}/{$service}/";
if (!is_dir($dir)) {
return;
}
// Get versions, remove . and .., and sort in descending order.
$results = array_diff(scandir($dir, SCANDIR_SORT_DESCENDING), ['..', '.']);
if (!$results) {
$this->manifest[$service] = ['versions' => []];
} else {
$this->manifest[$service] = ['versions' => ['latest' => $results[0]]];
$this->manifest[$service]['versions'] += array_combine($results, $results);
}
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException;
use Exception;
/**
* DateTime overrides that make DateTime work more seamlessly as a string,
* with JSON documents, and with JMESPath.
*/
class DateTimeResult extends \DateTime implements \JsonSerializable
{
/**
* Create a new DateTimeResult from a unix timestamp.
* The Unix epoch (or Unix time or POSIX time or Unix
* timestamp) is the number of seconds that have elapsed since
* January 1, 1970 (midnight UTC/GMT).
* @param $unixTimestamp
*
* @return DateTimeResult
* @throws Exception
*/
public static function fromEpoch($unixTimestamp)
{
return new self(gmdate('c', $unixTimestamp));
}
/**
* @param $iso8601Timestamp
*
* @return DateTimeResult
*/
public static function fromISO8601($iso8601Timestamp)
{
if (is_numeric($iso8601Timestamp) || !is_string($iso8601Timestamp)) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Invalid timestamp value passed to DateTimeResult::fromISO8601');
}
return new \UglyRobot\Infinite_Uploads\Aws\Api\DateTimeResult($iso8601Timestamp);
}
/**
* Create a new DateTimeResult from an unknown timestamp.
*
* @param $timestamp
*
* @return DateTimeResult
* @throws ParserException|Exception
*/
public static function fromTimestamp($timestamp, $expectedFormat = null)
{
if (empty($timestamp)) {
return self::fromEpoch(0);
}
if (!(is_string($timestamp) || is_numeric($timestamp))) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
}
try {
if ($expectedFormat == 'iso8601') {
try {
return self::fromISO8601($timestamp);
} catch (Exception $exception) {
return self::fromEpoch($timestamp);
}
} else {
if ($expectedFormat == 'unixTimestamp') {
try {
return self::fromEpoch($timestamp);
} catch (Exception $exception) {
return self::fromISO8601($timestamp);
}
} else {
if (\UglyRobot\Infinite_Uploads\Aws\is_valid_epoch($timestamp)) {
return self::fromEpoch($timestamp);
}
}
}
return self::fromISO8601($timestamp);
} catch (Exception $exception) {
throw new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\Exception\ParserException('Invalid timestamp value passed to DateTimeResult::fromTimestamp');
}
}
/**
* Serialize the DateTimeResult as an ISO 8601 date string.
*
* @return string
*/
public function __toString()
{
return $this->format('c');
}
/**
* Serialize the date as an ISO 8601 date when serializing as JSON.
*
* @return mixed|string
*/
public function jsonSerialize()
{
return (string) $this;
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Encapsulates the documentation strings for a given service-version and
* provides methods for extracting the desired parts related to a service,
* operation, error, or shape (i.e., parameter).
*/
class DocModel
{
/** @var array */
private $docs;
/**
* @param array $docs
*
* @throws \RuntimeException
*/
public function __construct(array $docs)
{
if (!extension_loaded('tidy')) {
throw new \RuntimeException('The "tidy" PHP extension is required.');
}
$this->docs = $docs;
}
/**
* Convert the doc model to an array.
*
* @return array
*/
public function toArray()
{
return $this->docs;
}
/**
* Retrieves documentation about the service.
*
* @return null|string
*/
public function getServiceDocs()
{
return isset($this->docs['service']) ? $this->docs['service'] : null;
}
/**
* Retrieves documentation about an operation.
*
* @param string $operation Name of the operation
*
* @return null|string
*/
public function getOperationDocs($operation)
{
return isset($this->docs['operations'][$operation]) ? $this->docs['operations'][$operation] : null;
}
/**
* Retrieves documentation about an error.
*
* @param string $error Name of the error
*
* @return null|string
*/
public function getErrorDocs($error)
{
return isset($this->docs['shapes'][$error]['base']) ? $this->docs['shapes'][$error]['base'] : null;
}
/**
* Retrieves documentation about a shape, specific to the context.
*
* @param string $shapeName Name of the shape.
* @param string $parentName Name of the parent/context shape.
* @param string $ref Name used by the context to reference the shape.
*
* @return null|string
*/
public function getShapeDocs($shapeName, $parentName, $ref)
{
if (!isset($this->docs['shapes'][$shapeName])) {
return '';
}
$result = '';
$d = $this->docs['shapes'][$shapeName];
if (isset($d['refs']["{$parentName}\${$ref}"])) {
$result = $d['refs']["{$parentName}\${$ref}"];
} elseif (isset($d['base'])) {
$result = $d['base'];
}
if (isset($d['append'])) {
$result .= $d['append'];
}
return $this->clean($result);
}
private function clean($content)
{
if (!$content) {
return '';
}
$tidy = new \tidy();
$tidy->parseString($content, ['indent' => true, 'doctype' => 'omit', 'output-html' => true, 'show-body-only' => true, 'drop-empty-paras' => true, 'drop-font-tags' => true, 'drop-proprietary-attributes' => true, 'hide-comments' => true, 'logical-emphasis' => true]);
$tidy->cleanRepair();
return (string) $content;
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\MetadataParserTrait;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\PayloadParserTrait;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
abstract class AbstractErrorParser
{
use MetadataParserTrait;
use PayloadParserTrait;
/**
* @var Service
*/
protected $api;
/**
* @param Service $api
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api = null)
{
$this->api = $api;
}
protected abstract function payload(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member);
protected function extractPayload(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response)
{
if ($member instanceof StructureShape) {
// Structure members parse top-level data into a specific key.
return $this->payload($response, $member);
} else {
// Streaming data is just the stream from the response body.
return $response->getBody();
}
}
protected function populateShape(array &$data, \UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\CommandInterface $command = null)
{
$data['body'] = [];
if (!empty($command) && !empty($this->api)) {
// If modeled error code is indicated, check for known error shape
if (!empty($data['code'])) {
$errors = $this->api->getOperation($command->getName())->getErrors();
foreach ($errors as $key => $error) {
// If error code matches a known error shape, populate the body
if ($data['code'] == $error['name'] && $error instanceof StructureShape) {
$modeledError = $error;
$data['body'] = $this->extractPayload($modeledError, $response);
$data['error_shape'] = $modeledError;
foreach ($error->getMembers() as $name => $member) {
switch ($member['location']) {
case 'header':
$this->extractHeader($name, $member, $response, $data['body']);
break;
case 'headers':
$this->extractHeaders($name, $member, $response, $data['body']);
break;
case 'statusCode':
$this->extractStatus($name, $response, $data['body']);
break;
}
}
break;
}
}
}
}
return $data;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\PayloadParserTrait;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
/**
* Provides basic JSON error parsing functionality.
*/
trait JsonParserTrait
{
use PayloadParserTrait;
private function genericHandler(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response)
{
$code = (string) $response->getStatusCode();
return ['request_id' => (string) $response->getHeaderLine('x-amzn-requestid'), 'code' => null, 'message' => null, 'type' => $code[0] == '4' ? 'client' : 'server', 'parsed' => $this->parseJson($response->getBody(), $response)];
}
protected function payload(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member)
{
$jsonBody = $this->parseJson($response->getBody(), $response);
if ($jsonBody) {
return $this->parser->parse($member, $jsonBody);
}
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\JsonParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
/**
* Parsers JSON-RPC errors.
*/
class JsonRpcErrorParser extends \UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser\AbstractErrorParser
{
use JsonParserTrait;
private $parser;
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api = null, \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\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\CommandInterface $command = null)
{
$data = $this->genericHandler($response);
// Make the casing consistent across services.
if ($data['parsed']) {
$data['parsed'] = array_change_key_case($data['parsed']);
}
if (isset($data['parsed']['__type'])) {
$parts = explode('#', $data['parsed']['__type']);
$data['code'] = isset($parts[1]) ? $parts[1] : $parts[0];
$data['message'] = isset($data['parsed']['message']) ? $data['parsed']['message'] : null;
}
$this->populateShape($data, $response, $command);
return $data;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\JsonParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
/**
* Parses JSON-REST errors.
*/
class RestJsonErrorParser extends \UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser\AbstractErrorParser
{
use JsonParserTrait;
private $parser;
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api = null, \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\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\CommandInterface $command = null)
{
$data = $this->genericHandler($response);
// Merge in error data from the JSON body
if ($json = $data['parsed']) {
$data = array_replace($data, $json);
}
// Correct error type from services like Amazon Glacier
if (!empty($data['type'])) {
$data['type'] = strtolower($data['type']);
}
// Retrieve the error code from services like Amazon Elastic Transcoder
if ($code = $response->getHeaderLine('x-amzn-errortype')) {
$colon = strpos($code, ':');
$data['code'] = $colon ? substr($code, 0, $colon) : $code;
}
// Retrieve error message directly
$data['message'] = isset($data['parsed']['message']) ? $data['parsed']['message'] : (isset($data['parsed']['Message']) ? $data['parsed']['Message'] : null);
$this->populateShape($data, $response, $command);
return $data;
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\PayloadParserTrait;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface;
/**
* Parses XML errors.
*/
class XmlErrorParser extends \UglyRobot\Infinite_Uploads\Aws\Api\ErrorParser\AbstractErrorParser
{
use PayloadParserTrait;
protected $parser;
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api = null, \UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser $parser = null)
{
parent::__construct($api);
$this->parser = $parser ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\XmlParser();
}
public function __invoke(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\CommandInterface $command = null)
{
$code = (string) $response->getStatusCode();
$data = ['type' => $code[0] == '4' ? 'client' : 'server', 'request_id' => null, 'code' => null, 'message' => null, 'parsed' => null];
$body = $response->getBody();
if ($body->getSize() > 0) {
$this->parseBody($this->parseXml($body, $response), $data);
} else {
$this->parseHeaders($response, $data);
}
$this->populateShape($data, $response, $command);
return $data;
}
private function parseHeaders(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, array &$data)
{
if ($response->getStatusCode() == '404') {
$data['code'] = 'NotFound';
}
$data['message'] = $response->getStatusCode() . ' ' . $response->getReasonPhrase();
if ($requestId = $response->getHeaderLine('x-amz-request-id')) {
$data['request_id'] = $requestId;
$data['message'] .= " (Request-ID: {$requestId})";
}
}
private function parseBody(\SimpleXMLElement $body, array &$data)
{
$data['parsed'] = $body;
$prefix = $this->registerNamespacePrefix($body);
if ($tempXml = $body->xpath("//{$prefix}Code[1]")) {
$data['code'] = (string) $tempXml[0];
}
if ($tempXml = $body->xpath("//{$prefix}Message[1]")) {
$data['message'] = (string) $tempXml[0];
}
$tempXml = $body->xpath("//{$prefix}RequestId[1]");
if (isset($tempXml[0])) {
$data['request_id'] = (string) $tempXml[0];
}
}
protected function registerNamespacePrefix(\SimpleXMLElement $element)
{
$namespaces = $element->getDocNamespaces();
if (!isset($namespaces[''])) {
return '';
}
// Account for the default namespace being defined and PHP not
// being able to handle it :(.
$element->registerXPathNamespace('ns', $namespaces['']);
return 'ns:';
}
protected function payload(\UglyRobot\Infinite_Uploads\Psr\Http\Message\ResponseInterface $response, \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member)
{
$xmlBody = $this->parseXml($response->getBody(), $response);
$prefix = $this->registerNamespacePrefix($xmlBody);
$errorBody = $xmlBody->xpath("//{$prefix}Error");
if (is_array($errorBody) && !empty($errorBody[0])) {
return $this->parser->parse($member, $errorBody[0]);
}
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Represents a list shape.
*/
class ListShape extends \UglyRobot\Infinite_Uploads\Aws\Api\Shape
{
private $member;
public function __construct(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
$definition['type'] = 'list';
parent::__construct($definition, $shapeMap);
}
/**
* @return Shape
* @throws \RuntimeException if no member is specified
*/
public function getMember()
{
if (!$this->member) {
if (!isset($this->definition['member'])) {
throw new \RuntimeException('No member attribute specified');
}
$this->member = \UglyRobot\Infinite_Uploads\Aws\Api\Shape::create($this->definition['member'], $this->shapeMap);
}
return $this->member;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Represents a map shape.
*/
class MapShape extends \UglyRobot\Infinite_Uploads\Aws\Api\Shape
{
/** @var Shape */
private $value;
/** @var Shape */
private $key;
public function __construct(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
$definition['type'] = 'map';
parent::__construct($definition, $shapeMap);
}
/**
* @return Shape
* @throws \RuntimeException if no value is specified
*/
public function getValue()
{
if (!$this->value) {
if (!isset($this->definition['value'])) {
throw new \RuntimeException('No value specified');
}
$this->value = \UglyRobot\Infinite_Uploads\Aws\Api\Shape::create($this->definition['value'], $this->shapeMap);
}
return $this->value;
}
/**
* @return Shape
*/
public function getKey()
{
if (!$this->key) {
$this->key = isset($this->definition['key']) ? \UglyRobot\Infinite_Uploads\Aws\Api\Shape::create($this->definition['key'], $this->shapeMap) : new \UglyRobot\Infinite_Uploads\Aws\Api\Shape(['type' => 'string'], $this->shapeMap);
}
return $this->key;
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Represents an API operation.
*/
class Operation extends \UglyRobot\Infinite_Uploads\Aws\Api\AbstractModel
{
private $input;
private $output;
private $errors;
public function __construct(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
$definition['type'] = 'structure';
if (!isset($definition['http']['method'])) {
$definition['http']['method'] = 'POST';
}
if (!isset($definition['http']['requestUri'])) {
$definition['http']['requestUri'] = '/';
}
parent::__construct($definition, $shapeMap);
}
/**
* Returns an associative array of the HTTP attribute of the operation:
*
* - method: HTTP method of the operation
* - requestUri: URI of the request (can include URI template placeholders)
*
* @return array
*/
public function getHttp()
{
return $this->definition['http'];
}
/**
* Get the input shape of the operation.
*
* @return StructureShape
*/
public function getInput()
{
if (!$this->input) {
if ($input = $this['input']) {
$this->input = $this->shapeFor($input);
} else {
$this->input = new \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape([], $this->shapeMap);
}
}
return $this->input;
}
/**
* Get the output shape of the operation.
*
* @return StructureShape
*/
public function getOutput()
{
if (!$this->output) {
if ($output = $this['output']) {
$this->output = $this->shapeFor($output);
} else {
$this->output = new \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape([], $this->shapeMap);
}
}
return $this->output;
}
/**
* Get an array of operation error shapes.
*
* @return Shape[]
*/
public function getErrors()
{
if ($this->errors === null) {
if ($errors = $this['errors']) {
foreach ($errors as $key => $error) {
$errors[$key] = $this->shapeFor($error);
}
$this->errors = $errors;
} else {
$this->errors = [];
}
}
return $this->errors;
}
}

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;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\ListShape;
/**
* @internal
*/
class Ec2ParamBuilder extends \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\QueryParamBuilder
{
protected function queryName(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $default = null)
{
return $shape['queryName'] ?: ucfirst($shape['locationName']) ?: $default;
}
protected function isFlat(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape)
{
return false;
}
protected function format_list(\UglyRobot\Infinite_Uploads\Aws\Api\ListShape $shape, array $value, $prefix, &$query)
{
// Handle empty list serialization
if (!$value) {
$query[$prefix] = false;
} else {
$items = $shape->getMember();
foreach ($value as $k => $v) {
$this->format($items, $v, $prefix . '.' . ($k + 1), $query);
}
}
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape;
/**
* Formats the JSON body of a JSON-REST or JSON-RPC operation.
* @internal
*/
class JsonBody
{
private $api;
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api)
{
$this->api = $api;
}
/**
* Gets the JSON Content-Type header for a service API
*
* @param Service $service
*
* @return string
*/
public static function getContentType(\UglyRobot\Infinite_Uploads\Aws\Api\Service $service)
{
return 'application/x-amz-json-' . number_format($service->getMetadata('jsonVersion'), 1);
}
/**
* Builds the JSON body based on an array of arguments.
*
* @param Shape $shape Operation being constructed
* @param array $args Associative array of arguments
*
* @return string
*/
public function build(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, array $args)
{
$result = json_encode($this->format($shape, $args));
return $result == '[]' ? '{}' : $result;
}
private function format(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
switch ($shape['type']) {
case 'structure':
$data = [];
foreach ($value as $k => $v) {
if ($v !== null && $shape->hasMember($k)) {
$valueShape = $shape->getMember($k);
$data[$valueShape['locationName'] ?: $k] = $this->format($valueShape, $v);
}
}
if (empty($data)) {
return new \stdClass();
}
return $data;
case 'list':
$items = $shape->getMember();
foreach ($value as $k => $v) {
$value[$k] = $this->format($items, $v);
}
return $value;
case 'map':
if (empty($value)) {
return new \stdClass();
}
$values = $shape->getValue();
foreach ($value as $k => $v) {
$value[$k] = $this->format($values, $v);
}
return $value;
case 'blob':
return base64_encode($value);
case 'timestamp':
$timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'unixTimestamp';
return \UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape::format($value, $timestampFormat);
default:
return $value;
}
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Request;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\RequestInterface;
/**
* Prepares a JSON-RPC request for transfer.
* @internal
*/
class JsonRpcSerializer
{
/** @var JsonBody */
private $jsonFormatter;
/** @var string */
private $endpoint;
/** @var Service */
private $api;
/** @var string */
private $contentType;
/**
* @param Service $api Service description
* @param string $endpoint Endpoint to connect to
* @param JsonBody $jsonFormatter Optional JSON formatter to use
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, $endpoint, \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\JsonBody $jsonFormatter = null)
{
$this->endpoint = $endpoint;
$this->api = $api;
$this->jsonFormatter = $jsonFormatter ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\JsonBody($this->api);
$this->contentType = \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\JsonBody::getContentType($api);
}
/**
* When invoked with an AWS command, returns a serialization array
* containing "method", "uri", "headers", and "body" key value pairs.
*
* @param CommandInterface $command
*
* @return RequestInterface
*/
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command)
{
$name = $command->getName();
$operation = $this->api->getOperation($name);
return new \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Request($operation['http']['method'], $this->endpoint, ['X-Amz-Target' => $this->api->getMetadata('targetPrefix') . '.' . $name, 'Content-Type' => $this->contentType], $this->jsonFormatter->build($operation->getInput(), $command->toArray()));
}
}

View File

@ -0,0 +1,112 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Api\ListShape;
use UglyRobot\Infinite_Uploads\Aws\Api\MapShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape;
/**
* @internal
*/
class QueryParamBuilder
{
private $methods;
protected function queryName(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $default = null)
{
if (null !== $shape['queryName']) {
return $shape['queryName'];
}
if (null !== $shape['locationName']) {
return $shape['locationName'];
}
if ($this->isFlat($shape) && !empty($shape['member']['locationName'])) {
return $shape['member']['locationName'];
}
return $default;
}
protected function isFlat(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape)
{
return $shape['flattened'] === true;
}
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, array $params)
{
if (!$this->methods) {
$this->methods = array_fill_keys(get_class_methods($this), true);
}
$query = [];
$this->format_structure($shape, $params, '', $query);
return $query;
}
protected function format(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value, $prefix, array &$query)
{
$type = 'format_' . $shape['type'];
if (isset($this->methods[$type])) {
$this->{$type}($shape, $value, $prefix, $query);
} else {
$query[$prefix] = (string) $value;
}
}
protected function format_structure(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, array $value, $prefix, &$query)
{
if ($prefix) {
$prefix .= '.';
}
foreach ($value as $k => $v) {
if ($shape->hasMember($k)) {
$member = $shape->getMember($k);
$this->format($member, $v, $prefix . $this->queryName($member, $k), $query);
}
}
}
protected function format_list(\UglyRobot\Infinite_Uploads\Aws\Api\ListShape $shape, array $value, $prefix, &$query)
{
// Handle empty list serialization
if (!$value) {
$query[$prefix] = '';
return;
}
$items = $shape->getMember();
if (!$this->isFlat($shape)) {
$locationName = $shape->getMember()['locationName'] ?: 'member';
$prefix .= ".{$locationName}";
} elseif ($name = $this->queryName($items)) {
$parts = explode('.', $prefix);
$parts[count($parts) - 1] = $name;
$prefix = implode('.', $parts);
}
foreach ($value as $k => $v) {
$this->format($items, $v, $prefix . '.' . ($k + 1), $query);
}
}
protected function format_map(\UglyRobot\Infinite_Uploads\Aws\Api\MapShape $shape, array $value, $prefix, array &$query)
{
$vals = $shape->getValue();
$keys = $shape->getKey();
if (!$this->isFlat($shape)) {
$prefix .= '.entry';
}
$i = 0;
$keyName = '%s.%d.' . $this->queryName($keys, 'key');
$valueName = '%s.%s.' . $this->queryName($vals, 'value');
foreach ($value as $k => $v) {
$i++;
$this->format($keys, $k, sprintf($keyName, $prefix, $i), $query);
$this->format($vals, $v, sprintf($valueName, $prefix, $i), $query);
}
}
protected function format_blob(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value, $prefix, array &$query)
{
$query[$prefix] = base64_encode($value);
}
protected function format_timestamp(\UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape $shape, $value, $prefix, array &$query)
{
$timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'iso8601';
$query[$prefix] = \UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape::format($value, $timestampFormat);
}
protected function format_boolean(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value, $prefix, array &$query)
{
$query[$prefix] = $value ? 'true' : 'false';
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Request;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\RequestInterface;
/**
* Serializes a query protocol request.
* @internal
*/
class QuerySerializer
{
private $endpoint;
private $api;
private $paramBuilder;
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, $endpoint, callable $paramBuilder = null)
{
$this->api = $api;
$this->endpoint = $endpoint;
$this->paramBuilder = $paramBuilder ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\QueryParamBuilder();
}
/**
* When invoked with an AWS command, returns a serialization array
* containing "method", "uri", "headers", and "body" key value pairs.
*
* @param CommandInterface $command
*
* @return RequestInterface
*/
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command)
{
$operation = $this->api->getOperation($command->getName());
$body = ['Action' => $command->getName(), 'Version' => $this->api->getMetadata('apiVersion')];
$params = $command->toArray();
// Only build up the parameters when there are parameters to build
if ($params) {
$body += call_user_func($this->paramBuilder, $operation->getInput(), $params);
}
$body = http_build_query($body, null, '&', PHP_QUERY_RFC3986);
return new \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Request('POST', $this->endpoint, ['Content-Length' => strlen($body), 'Content-Type' => 'application/x-www-form-urlencoded'], $body);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
/**
* Serializes requests for the REST-JSON protocol.
* @internal
*/
class RestJsonSerializer extends \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\RestSerializer
{
/** @var JsonBody */
private $jsonFormatter;
/** @var string */
private $contentType;
/**
* @param Service $api Service API description
* @param string $endpoint Endpoint to connect to
* @param JsonBody $jsonFormatter Optional JSON formatter to use
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, $endpoint, \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\JsonBody $jsonFormatter = null)
{
parent::__construct($api, $endpoint);
$this->contentType = 'application/json';
$this->jsonFormatter = $jsonFormatter ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\JsonBody($api);
}
protected function payload(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, array $value, array &$opts)
{
$opts['headers']['Content-Type'] = $this->contentType;
$opts['body'] = (string) $this->jsonFormatter->build($member, $value);
}
}

View File

@ -0,0 +1,172 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\MapShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\Operation;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape;
use UglyRobot\Infinite_Uploads\Aws\CommandInterface;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Uri;
use UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\UriResolver;
use UglyRobot\Infinite_Uploads\Psr\Http\Message\RequestInterface;
/**
* Serializes HTTP locations like header, uri, payload, etc...
* @internal
*/
abstract class RestSerializer
{
/** @var Service */
private $api;
/** @var Psr7\Uri */
private $endpoint;
/**
* @param Service $api Service API description
* @param string $endpoint Endpoint to connect to
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, $endpoint)
{
$this->api = $api;
$this->endpoint = \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\uri_for($endpoint);
}
/**
* @param CommandInterface $command Command to serialized
*
* @return RequestInterface
*/
public function __invoke(\UglyRobot\Infinite_Uploads\Aws\CommandInterface $command)
{
$operation = $this->api->getOperation($command->getName());
$args = $command->toArray();
$opts = $this->serialize($operation, $args);
$uri = $this->buildEndpoint($operation, $args, $opts);
return new \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Request($operation['http']['method'], $uri, isset($opts['headers']) ? $opts['headers'] : [], isset($opts['body']) ? $opts['body'] : null);
}
/**
* Modifies a hash of request options for a payload body.
*
* @param StructureShape $member Member to serialize
* @param array $value Value to serialize
* @param array $opts Request options to modify.
*/
protected abstract function payload(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, array $value, array &$opts);
private function serialize(\UglyRobot\Infinite_Uploads\Aws\Api\Operation $operation, array $args)
{
$opts = [];
$input = $operation->getInput();
// Apply the payload trait if present
if ($payload = $input['payload']) {
$this->applyPayload($input, $payload, $args, $opts);
}
foreach ($args as $name => $value) {
if ($input->hasMember($name)) {
$member = $input->getMember($name);
$location = $member['location'];
if (!$payload && !$location) {
$bodyMembers[$name] = $value;
} elseif ($location == 'header') {
$this->applyHeader($name, $member, $value, $opts);
} elseif ($location == 'querystring') {
$this->applyQuery($name, $member, $value, $opts);
} elseif ($location == 'headers') {
$this->applyHeaderMap($name, $member, $value, $opts);
}
}
}
if (isset($bodyMembers)) {
$this->payload($operation->getInput(), $bodyMembers, $opts);
}
return $opts;
}
private function applyPayload(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $input, $name, array $args, array &$opts)
{
if (!isset($args[$name])) {
return;
}
$m = $input->getMember($name);
if ($m['streaming'] || ($m['type'] == 'string' || $m['type'] == 'blob')) {
// Streaming bodies or payloads that are strings are
// always just a stream of data.
$opts['body'] = \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\stream_for($args[$name]);
return;
}
$this->payload($m, $args[$name], $opts);
}
private function applyHeader($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $member, $value, array &$opts)
{
if ($member->getType() === 'timestamp') {
$timestampFormat = !empty($member['timestampFormat']) ? $member['timestampFormat'] : 'rfc822';
$value = \UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape::format($value, $timestampFormat);
}
if ($member['jsonvalue']) {
$value = json_encode($value);
if (empty($value) && JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException('Unable to encode the provided value' . ' with \'json_encode\'. ' . json_last_error_msg());
}
$value = base64_encode($value);
}
$opts['headers'][$member['locationName'] ?: $name] = $value;
}
/**
* Note: This is currently only present in the Amazon S3 model.
*/
private function applyHeaderMap($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $member, array $value, array &$opts)
{
$prefix = $member['locationName'];
foreach ($value as $k => $v) {
$opts['headers'][$prefix . $k] = $v;
}
}
private function applyQuery($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $member, $value, array &$opts)
{
if ($member instanceof MapShape) {
$opts['query'] = isset($opts['query']) && is_array($opts['query']) ? $opts['query'] + $value : $value;
} elseif ($value !== null) {
$type = $member->getType();
if ($type === 'boolean') {
$value = $value ? 'true' : 'false';
} elseif ($type === 'timestamp') {
$timestampFormat = !empty($member['timestampFormat']) ? $member['timestampFormat'] : 'iso8601';
$value = \UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape::format($value, $timestampFormat);
}
$opts['query'][$member['locationName'] ?: $name] = $value;
}
}
private function buildEndpoint(\UglyRobot\Infinite_Uploads\Aws\Api\Operation $operation, array $args, array $opts)
{
$varspecs = [];
// Create an associative array of varspecs used in expansions
foreach ($operation->getInput()->getMembers() as $name => $member) {
if ($member['location'] == 'uri') {
$varspecs[$member['locationName'] ?: $name] = isset($args[$name]) ? $args[$name] : null;
}
}
$relative = preg_replace_callback('/\\{([^\\}]+)\\}/', function (array $matches) use($varspecs) {
$isGreedy = substr($matches[1], -1, 1) == '+';
$k = $isGreedy ? substr($matches[1], 0, -1) : $matches[1];
if (!isset($varspecs[$k])) {
return '';
}
if ($isGreedy) {
return str_replace('%2F', '/', rawurlencode($varspecs[$k]));
}
return rawurlencode($varspecs[$k]);
}, $operation['http']['requestUri']);
// Add the query string variables or appending to one if needed.
if (!empty($opts['query'])) {
$append = \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\build_query($opts['query']);
$relative .= strpos($relative, '?') ? "&{$append}" : "?{$append}";
}
// If endpoint has path, remove leading '/' to preserve URI resolution.
$path = $this->endpoint->getPath();
if ($path && $relative[0] === '/') {
$relative = substr($relative, 1);
}
// Expand path place holders using Amazon's slightly different URI
// template syntax.
return \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\UriResolver::resolve($this->endpoint, new \UglyRobot\Infinite_Uploads\GuzzleHttp\Psr7\Uri($relative));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
/**
* @internal
*/
class RestXmlSerializer extends \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\RestSerializer
{
/** @var XmlBody */
private $xmlBody;
/**
* @param Service $api Service API description
* @param string $endpoint Endpoint to connect to
* @param XmlBody $xmlBody Optional XML formatter to use
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, $endpoint, \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\XmlBody $xmlBody = null)
{
parent::__construct($api, $endpoint);
$this->xmlBody = $xmlBody ?: new \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\XmlBody($api);
}
protected function payload(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $member, array $value, array &$opts)
{
$opts['headers']['Content-Type'] = 'application/xml';
$opts['body'] = (string) $this->xmlBody->build($member, $value);
}
}

View File

@ -0,0 +1,148 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api\Serializer;
use UglyRobot\Infinite_Uploads\Aws\Api\MapShape;
use UglyRobot\Infinite_Uploads\Aws\Api\Service;
use UglyRobot\Infinite_Uploads\Aws\Api\Shape;
use UglyRobot\Infinite_Uploads\Aws\Api\StructureShape;
use UglyRobot\Infinite_Uploads\Aws\Api\ListShape;
use UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape;
use XMLWriter;
/**
* @internal Formats the XML body of a REST-XML services.
*/
class XmlBody
{
/** @var \Aws\Api\Service */
private $api;
/**
* @param Service $api API being used to create the XML body.
*/
public function __construct(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api)
{
$this->api = $api;
}
/**
* Builds the XML body based on an array of arguments.
*
* @param Shape $shape Operation being constructed
* @param array $args Associative array of arguments
*
* @return string
*/
public function build(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, array $args)
{
$xml = new \XMLWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$this->format($shape, $shape['locationName'] ?: $shape['name'], $args, $xml);
$xml->endDocument();
return $xml->outputMemory();
}
private function startElement(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name, \XMLWriter $xml)
{
$xml->startElement($name);
if ($ns = $shape['xmlNamespace']) {
$xml->writeAttribute(isset($ns['prefix']) ? "xmlns:{$ns['prefix']}" : 'xmlns', $shape['xmlNamespace']['uri']);
}
}
private function format(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name, $value, \XMLWriter $xml)
{
// Any method mentioned here has a custom serialization handler.
static $methods = ['add_structure' => true, 'add_list' => true, 'add_blob' => true, 'add_timestamp' => true, 'add_boolean' => true, 'add_map' => true, 'add_string' => true];
$type = 'add_' . $shape['type'];
if (isset($methods[$type])) {
$this->{$type}($shape, $name, $value, $xml);
} else {
$this->defaultShape($shape, $name, $value, $xml);
}
}
private function defaultShape(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name, $value, \XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
$xml->text($value);
$xml->endElement();
}
private function add_structure(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, $name, array $value, \XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
foreach ($this->getStructureMembers($shape, $value) as $k => $definition) {
$this->format($definition['member'], $definition['member']['locationName'] ?: $k, $definition['value'], $xml);
}
$xml->endElement();
}
private function getStructureMembers(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, array $value)
{
$members = [];
foreach ($value as $k => $v) {
if ($v !== null && $shape->hasMember($k)) {
$definition = ['member' => $shape->getMember($k), 'value' => $v];
if ($definition['member']['xmlAttribute']) {
// array_unshift_associative
$members = [$k => $definition] + $members;
} else {
$members[$k] = $definition;
}
}
}
return $members;
}
private function add_list(\UglyRobot\Infinite_Uploads\Aws\Api\ListShape $shape, $name, array $value, \XMLWriter $xml)
{
$items = $shape->getMember();
if ($shape['flattened']) {
$elementName = $name;
} else {
$this->startElement($shape, $name, $xml);
$elementName = $items['locationName'] ?: 'member';
}
foreach ($value as $v) {
$this->format($items, $elementName, $v, $xml);
}
if (!$shape['flattened']) {
$xml->endElement();
}
}
private function add_map(\UglyRobot\Infinite_Uploads\Aws\Api\MapShape $shape, $name, array $value, \XMLWriter $xml)
{
$xmlEntry = $shape['flattened'] ? $shape['locationName'] : 'entry';
$xmlKey = $shape->getKey()['locationName'] ?: 'key';
$xmlValue = $shape->getValue()['locationName'] ?: 'value';
$this->startElement($shape, $name, $xml);
foreach ($value as $key => $v) {
$this->startElement($shape, $xmlEntry, $xml);
$this->format($shape->getKey(), $xmlKey, $key, $xml);
$this->format($shape->getValue(), $xmlValue, $v, $xml);
$xml->endElement();
}
$xml->endElement();
}
private function add_blob(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name, $value, \XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
$xml->writeRaw(base64_encode($value));
$xml->endElement();
}
private function add_timestamp(\UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape $shape, $name, $value, \XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
$timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'iso8601';
$xml->writeRaw(\UglyRobot\Infinite_Uploads\Aws\Api\TimestampShape::format($value, $timestampFormat));
$xml->endElement();
}
private function add_boolean(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name, $value, \XMLWriter $xml)
{
$this->startElement($shape, $name, $xml);
$xml->writeRaw($value ? 'true' : 'false');
$xml->endElement();
}
private function add_string(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $name, $value, \XMLWriter $xml)
{
if ($shape['xmlAttribute']) {
$xml->writeAttribute($shape['locationName'] ?: $name, $value);
} else {
$this->defaultShape($shape, $name, $value, $xml);
}
}
}

View File

@ -0,0 +1,357 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
use UglyRobot\Infinite_Uploads\Aws\Api\Serializer\QuerySerializer;
use UglyRobot\Infinite_Uploads\Aws\Api\Serializer\Ec2ParamBuilder;
use UglyRobot\Infinite_Uploads\Aws\Api\Parser\QueryParser;
/**
* Represents a web service API model.
*/
class Service extends \UglyRobot\Infinite_Uploads\Aws\Api\AbstractModel
{
/** @var callable */
private $apiProvider;
/** @var string */
private $serviceName;
/** @var string */
private $apiVersion;
/** @var Operation[] */
private $operations = [];
/** @var array */
private $paginators = null;
/** @var array */
private $waiters = null;
/**
* @param array $definition
* @param callable $provider
*
* @internal param array $definition Service description
*/
public function __construct(array $definition, callable $provider)
{
static $defaults = ['operations' => [], 'shapes' => [], 'metadata' => []], $defaultMeta = ['apiVersion' => null, 'serviceFullName' => null, 'serviceId' => null, 'endpointPrefix' => null, 'signingName' => null, 'signatureVersion' => null, 'protocol' => null, 'uid' => null];
$definition += $defaults;
$definition['metadata'] += $defaultMeta;
$this->definition = $definition;
$this->apiProvider = $provider;
parent::__construct($definition, new \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap($definition['shapes']));
if (isset($definition['metadata']['serviceIdentifier'])) {
$this->serviceName = $this->getServiceName();
} else {
$this->serviceName = $this->getEndpointPrefix();
}
$this->apiVersion = $this->getApiVersion();
}
/**
* Creates a request serializer for the provided API object.
*
* @param Service $api API that contains a protocol.
* @param string $endpoint Endpoint to send requests to.
*
* @return callable
* @throws \UnexpectedValueException
*/
public static function createSerializer(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api, $endpoint)
{
static $mapping = ['json' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Serializer\\JsonRpcSerializer', 'query' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Serializer\\QuerySerializer', 'rest-json' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Serializer\\RestJsonSerializer', 'rest-xml' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Serializer\\RestXmlSerializer'];
$proto = $api->getProtocol();
if (isset($mapping[$proto])) {
return new $mapping[$proto]($api, $endpoint);
}
if ($proto == 'ec2') {
return new \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\QuerySerializer($api, $endpoint, new \UglyRobot\Infinite_Uploads\Aws\Api\Serializer\Ec2ParamBuilder());
}
throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol());
}
/**
* Creates an error parser for the given protocol.
*
* Redundant method signature to preserve backwards compatibility.
*
* @param string $protocol Protocol to parse (e.g., query, json, etc.)
*
* @return callable
* @throws \UnexpectedValueException
*/
public static function createErrorParser($protocol, \UglyRobot\Infinite_Uploads\Aws\Api\Service $api = null)
{
static $mapping = ['json' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\ErrorParser\\JsonRpcErrorParser', 'query' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\ErrorParser\\XmlErrorParser', 'rest-json' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\ErrorParser\\RestJsonErrorParser', 'rest-xml' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\ErrorParser\\XmlErrorParser', 'ec2' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\ErrorParser\\XmlErrorParser'];
if (isset($mapping[$protocol])) {
return new $mapping[$protocol]($api);
}
throw new \UnexpectedValueException("Unknown protocol: {$protocol}");
}
/**
* Applies the listeners needed to parse client models.
*
* @param Service $api API to create a parser for
* @return callable
* @throws \UnexpectedValueException
*/
public static function createParser(\UglyRobot\Infinite_Uploads\Aws\Api\Service $api)
{
static $mapping = ['json' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Parser\\JsonRpcParser', 'query' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Parser\\QueryParser', 'rest-json' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Parser\\RestJsonParser', 'rest-xml' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Parser\\RestXmlParser'];
$proto = $api->getProtocol();
if (isset($mapping[$proto])) {
return new $mapping[$proto]($api);
}
if ($proto == 'ec2') {
return new \UglyRobot\Infinite_Uploads\Aws\Api\Parser\QueryParser($api, null, false);
}
throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol());
}
/**
* Get the full name of the service
*
* @return string
*/
public function getServiceFullName()
{
return $this->definition['metadata']['serviceFullName'];
}
/**
* Get the service id
*
* @return string
*/
public function getServiceId()
{
return $this->definition['metadata']['serviceId'];
}
/**
* Get the API version of the service
*
* @return string
*/
public function getApiVersion()
{
return $this->definition['metadata']['apiVersion'];
}
/**
* Get the API version of the service
*
* @return string
*/
public function getEndpointPrefix()
{
return $this->definition['metadata']['endpointPrefix'];
}
/**
* Get the signing name used by the service.
*
* @return string
*/
public function getSigningName()
{
return $this->definition['metadata']['signingName'] ?: $this->definition['metadata']['endpointPrefix'];
}
/**
* Get the service name.
*
* @return string
*/
public function getServiceName()
{
return $this->definition['metadata']['serviceIdentifier'];
}
/**
* Get the default signature version of the service.
*
* Note: this method assumes "v4" when not specified in the model.
*
* @return string
*/
public function getSignatureVersion()
{
return $this->definition['metadata']['signatureVersion'] ?: 'v4';
}
/**
* Get the protocol used by the service.
*
* @return string
*/
public function getProtocol()
{
return $this->definition['metadata']['protocol'];
}
/**
* Get the uid string used by the service
*
* @return string
*/
public function getUid()
{
return $this->definition['metadata']['uid'];
}
/**
* Check if the description has a specific operation by name.
*
* @param string $name Operation to check by name
*
* @return bool
*/
public function hasOperation($name)
{
return isset($this['operations'][$name]);
}
/**
* Get an operation by name.
*
* @param string $name Operation to retrieve by name
*
* @return Operation
* @throws \InvalidArgumentException If the operation is not found
*/
public function getOperation($name)
{
if (!isset($this->operations[$name])) {
if (!isset($this->definition['operations'][$name])) {
throw new \InvalidArgumentException("Unknown operation: {$name}");
}
$this->operations[$name] = new \UglyRobot\Infinite_Uploads\Aws\Api\Operation($this->definition['operations'][$name], $this->shapeMap);
}
return $this->operations[$name];
}
/**
* Get all of the operations of the description.
*
* @return Operation[]
*/
public function getOperations()
{
$result = [];
foreach ($this->definition['operations'] as $name => $definition) {
$result[$name] = $this->getOperation($name);
}
return $result;
}
/**
* Get all of the error shapes of the service
*
* @return array
*/
public function getErrorShapes()
{
$result = [];
foreach ($this->definition['shapes'] as $name => $definition) {
if (!empty($definition['exception'])) {
$definition['name'] = $name;
$result[] = new \UglyRobot\Infinite_Uploads\Aws\Api\StructureShape($definition, $this->getShapeMap());
}
}
return $result;
}
/**
* Get all of the service metadata or a specific metadata key value.
*
* @param string|null $key Key to retrieve or null to retrieve all metadata
*
* @return mixed Returns the result or null if the key is not found
*/
public function getMetadata($key = null)
{
if (!$key) {
return $this['metadata'];
}
if (isset($this->definition['metadata'][$key])) {
return $this->definition['metadata'][$key];
}
return null;
}
/**
* Gets an associative array of available paginator configurations where
* the key is the name of the paginator, and the value is the paginator
* configuration.
*
* @return array
* @unstable The configuration format of paginators may change in the future
*/
public function getPaginators()
{
if (!isset($this->paginators)) {
$res = call_user_func($this->apiProvider, 'paginator', $this->serviceName, $this->apiVersion);
$this->paginators = isset($res['pagination']) ? $res['pagination'] : [];
}
return $this->paginators;
}
/**
* Determines if the service has a paginator by name.
*
* @param string $name Name of the paginator.
*
* @return bool
*/
public function hasPaginator($name)
{
return isset($this->getPaginators()[$name]);
}
/**
* Retrieve a paginator by name.
*
* @param string $name Paginator to retrieve by name. This argument is
* typically the operation name.
* @return array
* @throws \UnexpectedValueException if the paginator does not exist.
* @unstable The configuration format of paginators may change in the future
*/
public function getPaginatorConfig($name)
{
static $defaults = ['input_token' => null, 'output_token' => null, 'limit_key' => null, 'result_key' => null, 'more_results' => null];
if ($this->hasPaginator($name)) {
return $this->paginators[$name] + $defaults;
}
throw new \UnexpectedValueException("There is no {$name} " . "paginator defined for the {$this->serviceName} service.");
}
/**
* Gets an associative array of available waiter configurations where the
* key is the name of the waiter, and the value is the waiter
* configuration.
*
* @return array
*/
public function getWaiters()
{
if (!isset($this->waiters)) {
$res = call_user_func($this->apiProvider, 'waiter', $this->serviceName, $this->apiVersion);
$this->waiters = isset($res['waiters']) ? $res['waiters'] : [];
}
return $this->waiters;
}
/**
* Determines if the service has a waiter by name.
*
* @param string $name Name of the waiter.
*
* @return bool
*/
public function hasWaiter($name)
{
return isset($this->getWaiters()[$name]);
}
/**
* Get a waiter configuration by name.
*
* @param string $name Name of the waiter by name.
*
* @return array
* @throws \UnexpectedValueException if the waiter does not exist.
*/
public function getWaiterConfig($name)
{
// Error if the waiter is not defined
if ($this->hasWaiter($name)) {
return $this->waiters[$name];
}
throw new \UnexpectedValueException("There is no {$name} waiter " . "defined for the {$this->serviceName} service.");
}
/**
* Get the shape map used by the API.
*
* @return ShapeMap
*/
public function getShapeMap()
{
return $this->shapeMap;
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Base class representing a modeled shape.
*/
class Shape extends \UglyRobot\Infinite_Uploads\Aws\Api\AbstractModel
{
/**
* Get a concrete shape for the given definition.
*
* @param array $definition
* @param ShapeMap $shapeMap
*
* @return mixed
* @throws \RuntimeException if the type is invalid
*/
public static function create(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
static $map = ['structure' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\StructureShape', 'map' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\MapShape', 'list' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\ListShape', 'timestamp' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\TimestampShape', 'integer' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'double' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'float' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'long' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'string' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'byte' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'character' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'blob' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape', 'boolean' => 'UglyRobot\\Infinite_Uploads\\Aws\\Api\\Shape'];
if (isset($definition['shape'])) {
return $shapeMap->resolve($definition);
}
if (!isset($map[$definition['type']])) {
throw new \RuntimeException('Invalid type: ' . print_r($definition, true));
}
$type = $map[$definition['type']];
return new $type($definition, $shapeMap);
}
/**
* Get the type of the shape
*
* @return string
*/
public function getType()
{
return $this->definition['type'];
}
/**
* Get the name of the shape
*
* @return string
*/
public function getName()
{
return $this->definition['name'];
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Builds shape based on shape references.
*/
class ShapeMap
{
/** @var array */
private $definitions;
/** @var Shape[] */
private $simple;
/**
* @param array $shapeModels Associative array of shape definitions.
*/
public function __construct(array $shapeModels)
{
$this->definitions = $shapeModels;
}
/**
* Get an array of shape names.
*
* @return array
*/
public function getShapeNames()
{
return array_keys($this->definitions);
}
/**
* Resolve a shape reference
*
* @param array $shapeRef Shape reference shape
*
* @return Shape
* @throws \InvalidArgumentException
*/
public function resolve(array $shapeRef)
{
$shape = $shapeRef['shape'];
if (!isset($this->definitions[$shape])) {
throw new \InvalidArgumentException('Shape not found: ' . $shape);
}
$isSimple = count($shapeRef) == 1;
if ($isSimple && isset($this->simple[$shape])) {
return $this->simple[$shape];
}
$definition = $shapeRef + $this->definitions[$shape];
$definition['name'] = $definition['shape'];
if (isset($definition['shape'])) {
unset($definition['shape']);
}
$result = \UglyRobot\Infinite_Uploads\Aws\Api\Shape::create($definition, $this);
if ($isSimple) {
$this->simple[$shape] = $result;
}
return $result;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Represents a structure shape and resolve member shape references.
*/
class StructureShape extends \UglyRobot\Infinite_Uploads\Aws\Api\Shape
{
/**
* @var Shape[]
*/
private $members;
public function __construct(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
$definition['type'] = 'structure';
if (!isset($definition['members'])) {
$definition['members'] = [];
}
parent::__construct($definition, $shapeMap);
}
/**
* Gets a list of all members
*
* @return Shape[]
*/
public function getMembers()
{
if (empty($this->members)) {
$this->generateMembersHash();
}
return $this->members;
}
/**
* Check if a specific member exists by name.
*
* @param string $name Name of the member to check
*
* @return bool
*/
public function hasMember($name)
{
return isset($this->definition['members'][$name]);
}
/**
* Retrieve a member by name.
*
* @param string $name Name of the member to retrieve
*
* @return Shape
* @throws \InvalidArgumentException if the member is not found.
*/
public function getMember($name)
{
$members = $this->getMembers();
if (!isset($members[$name])) {
throw new \InvalidArgumentException('Unknown member ' . $name);
}
return $members[$name];
}
private function generateMembersHash()
{
$this->members = [];
foreach ($this->definition['members'] as $name => $definition) {
$this->members[$name] = $this->shapeFor($definition);
}
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
/**
* Represents a timestamp shape.
*/
class TimestampShape extends \UglyRobot\Infinite_Uploads\Aws\Api\Shape
{
public function __construct(array $definition, \UglyRobot\Infinite_Uploads\Aws\Api\ShapeMap $shapeMap)
{
$definition['type'] = 'timestamp';
parent::__construct($definition, $shapeMap);
}
/**
* Formats a timestamp value for a service.
*
* @param mixed $value Value to format
* @param string $format Format used to serialize the value
*
* @return int|string
* @throws \UnexpectedValueException if the format is unknown.
* @throws \InvalidArgumentException if the value is an unsupported type.
*/
public static function format($value, $format)
{
if ($value instanceof \DateTime) {
$value = $value->getTimestamp();
} elseif (is_string($value)) {
$value = strtotime($value);
} elseif (!is_int($value)) {
throw new \InvalidArgumentException('Unable to handle the provided' . ' timestamp type: ' . gettype($value));
}
switch ($format) {
case 'iso8601':
return gmdate('Y-m-d\\TH:i:s\\Z', $value);
case 'rfc822':
return gmdate('D, d M Y H:i:s \\G\\M\\T', $value);
case 'unixTimestamp':
return $value;
default:
throw new \UnexpectedValueException('Unknown timestamp format: ' . $format);
}
}
}

View File

@ -0,0 +1,193 @@
<?php
namespace UglyRobot\Infinite_Uploads\Aws\Api;
use Aws;
/**
* Validates a schema against a hash of input.
*/
class Validator
{
private $path = [];
private $errors = [];
private $constraints = [];
private static $defaultConstraints = ['required' => true, 'min' => true, 'max' => false, 'pattern' => false];
/**
* @param array $constraints Associative array of constraints to enforce.
* Accepts the following keys: "required", "min",
* "max", and "pattern". If a key is not
* provided, the constraint will assume false.
*/
public function __construct(array $constraints = null)
{
static $assumedFalseValues = ['required' => false, 'min' => false, 'max' => false, 'pattern' => false];
$this->constraints = empty($constraints) ? self::$defaultConstraints : $constraints + $assumedFalseValues;
}
/**
* Validates the given input against the schema.
*
* @param string $name Operation name
* @param Shape $shape Shape to validate
* @param array $input Input to validate
*
* @throws \InvalidArgumentException if the input is invalid.
*/
public function validate($name, \UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, array $input)
{
$this->dispatch($shape, $input);
if ($this->errors) {
$message = sprintf("Found %d error%s while validating the input provided for the " . "%s operation:\n%s", count($this->errors), count($this->errors) > 1 ? 's' : '', $name, implode("\n", $this->errors));
$this->errors = [];
throw new \InvalidArgumentException($message);
}
}
private function dispatch(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
static $methods = ['structure' => 'check_structure', 'list' => 'check_list', 'map' => 'check_map', 'blob' => 'check_blob', 'boolean' => 'check_boolean', 'integer' => 'check_numeric', 'float' => 'check_numeric', 'long' => 'check_numeric', 'string' => 'check_string', 'byte' => 'check_string', 'char' => 'check_string'];
$type = $shape->getType();
if (isset($methods[$type])) {
$this->{$methods[$type]}($shape, $value);
}
}
private function check_structure(\UglyRobot\Infinite_Uploads\Aws\Api\StructureShape $shape, $value)
{
if (!$this->checkAssociativeArray($value)) {
return;
}
if ($this->constraints['required'] && $shape['required']) {
foreach ($shape['required'] as $req) {
if (!isset($value[$req])) {
$this->path[] = $req;
$this->addError('is missing and is a required parameter');
array_pop($this->path);
}
}
}
foreach ($value as $name => $v) {
if ($shape->hasMember($name)) {
$this->path[] = $name;
$this->dispatch($shape->getMember($name), isset($value[$name]) ? $value[$name] : null);
array_pop($this->path);
}
}
}
private function check_list(\UglyRobot\Infinite_Uploads\Aws\Api\ListShape $shape, $value)
{
if (!is_array($value)) {
$this->addError('must be an array. Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
return;
}
$this->validateRange($shape, count($value), "list element count");
$items = $shape->getMember();
foreach ($value as $index => $v) {
$this->path[] = $index;
$this->dispatch($items, $v);
array_pop($this->path);
}
}
private function check_map(\UglyRobot\Infinite_Uploads\Aws\Api\MapShape $shape, $value)
{
if (!$this->checkAssociativeArray($value)) {
return;
}
$values = $shape->getValue();
foreach ($value as $key => $v) {
$this->path[] = $key;
$this->dispatch($values, $v);
array_pop($this->path);
}
}
private function check_blob(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
static $valid = ['string' => true, 'integer' => true, 'double' => true, 'resource' => true];
$type = gettype($value);
if (!isset($valid[$type])) {
if ($type != 'object' || !method_exists($value, '__toString')) {
$this->addError('must be an fopen resource, a ' . 'UglyRobot\\Infinite_Uploads\\GuzzleHttp\\Stream\\StreamInterface object, or something ' . 'that can be cast to a string. Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
}
}
}
private function check_numeric(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
if (!is_numeric($value)) {
$this->addError('must be numeric. Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
return;
}
$this->validateRange($shape, $value, "numeric value");
}
private function check_boolean(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
if (!is_bool($value)) {
$this->addError('must be a boolean. Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
}
}
private function check_string(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $value)
{
if ($shape['jsonvalue']) {
if (!self::canJsonEncode($value)) {
$this->addError('must be a value encodable with \'json_encode\'.' . ' Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
}
return;
}
if (!$this->checkCanString($value)) {
$this->addError('must be a string or an object that implements ' . '__toString(). Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
return;
}
$this->validateRange($shape, strlen($value), "string length");
if ($this->constraints['pattern']) {
$pattern = $shape['pattern'];
if ($pattern && !preg_match("/{$pattern}/", $value)) {
$this->addError("Pattern /{$pattern}/ failed to match '{$value}'");
}
}
}
private function validateRange(\UglyRobot\Infinite_Uploads\Aws\Api\Shape $shape, $length, $descriptor)
{
if ($this->constraints['min']) {
$min = $shape['min'];
if ($min && $length < $min) {
$this->addError("expected {$descriptor} to be >= {$min}, but " . "found {$descriptor} of {$length}");
}
}
if ($this->constraints['max']) {
$max = $shape['max'];
if ($max && $length > $max) {
$this->addError("expected {$descriptor} to be <= {$max}, but " . "found {$descriptor} of {$length}");
}
}
}
private function checkCanString($value)
{
static $valid = ['string' => true, 'integer' => true, 'double' => true, 'NULL' => true];
$type = gettype($value);
return isset($valid[$type]) || $type == 'object' && method_exists($value, '__toString');
}
private function checkAssociativeArray($value)
{
$isAssociative = false;
if (is_array($value)) {
$expectedIndex = 0;
$key = key($value);
do {
$isAssociative = $key !== $expectedIndex++;
next($value);
$key = key($value);
} while (!$isAssociative && null !== $key);
}
if (!$isAssociative) {
$this->addError('must be an associative array. Found ' . \UglyRobot\Infinite_Uploads\Aws\describe_type($value));
return false;
}
return true;
}
private function addError($message)
{
$this->errors[] = implode('', array_map(function ($s) {
return "[{$s}]";
}, $this->path)) . ' ' . $message;
}
private function canJsonEncode($data)
{
return !is_resource($data);
}
}