diff options
Diffstat (limited to 'vendor/guzzle/guzzle/src/Guzzle/Service/Resource')
9 files changed, 718 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/AbstractResourceIteratorFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/AbstractResourceIteratorFactory.php new file mode 100644 index 0000000..21140e7 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/AbstractResourceIteratorFactory.php @@ -0,0 +1,37 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Common\Exception\InvalidArgumentException; +use Guzzle\Service\Command\CommandInterface; + +/** + * Abstract resource iterator factory implementation + */ +abstract class AbstractResourceIteratorFactory implements ResourceIteratorFactoryInterface +{ + public function build(CommandInterface $command, array $options = array()) + { + if (!$this->canBuild($command)) { + throw new InvalidArgumentException('Iterator was not found for ' . $command->getName()); + } + + $className = $this->getClassName($command); + + return new $className($command, $options); + } + + public function canBuild(CommandInterface $command) + { + return (bool) $this->getClassName($command); + } + + /** + * Get the name of the class to instantiate for the command + * + * @param CommandInterface $command Command that is associated with the iterator + * + * @return string + */ + abstract protected function getClassName(CommandInterface $command); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/CompositeResourceIteratorFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/CompositeResourceIteratorFactory.php new file mode 100644 index 0000000..2efc133 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/CompositeResourceIteratorFactory.php @@ -0,0 +1,67 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Common\Exception\InvalidArgumentException; +use Guzzle\Service\Command\CommandInterface; + +/** + * Factory that utilizes multiple factories for creating iterators + */ +class CompositeResourceIteratorFactory implements ResourceIteratorFactoryInterface +{ + /** @var array Array of factories */ + protected $factories; + + /** @param array $factories Array of factories used to instantiate iterators */ + public function __construct(array $factories) + { + $this->factories = $factories; + } + + public function build(CommandInterface $command, array $options = array()) + { + if (!($factory = $this->getFactory($command))) { + throw new InvalidArgumentException('Iterator was not found for ' . $command->getName()); + } + + return $factory->build($command, $options); + } + + public function canBuild(CommandInterface $command) + { + return $this->getFactory($command) !== false; + } + + /** + * Add a factory to the composite factory + * + * @param ResourceIteratorFactoryInterface $factory Factory to add + * + * @return self + */ + public function addFactory(ResourceIteratorFactoryInterface $factory) + { + $this->factories[] = $factory; + + return $this; + } + + /** + * Get the factory that matches the command object + * + * @param CommandInterface $command Command retrieving the iterator for + * + * @return ResourceIteratorFactoryInterface|bool + */ + protected function getFactory(CommandInterface $command) + { + foreach ($this->factories as $factory) { + if ($factory->canBuild($command)) { + return $factory; + } + } + + return false; + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/MapResourceIteratorFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/MapResourceIteratorFactory.php new file mode 100644 index 0000000..c71ca9d --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/MapResourceIteratorFactory.php @@ -0,0 +1,34 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Service\Command\CommandInterface; + +/** + * Resource iterator factory used when explicitly mapping strings to iterator classes + */ +class MapResourceIteratorFactory extends AbstractResourceIteratorFactory +{ + /** @var array Associative array mapping iterator names to class names */ + protected $map; + + /** @param array $map Associative array mapping iterator names to class names */ + public function __construct(array $map) + { + $this->map = $map; + } + + public function getClassName(CommandInterface $command) + { + $className = $command->getName(); + + if (isset($this->map[$className])) { + return $this->map[$className]; + } elseif (isset($this->map['*'])) { + // If a wildcard was added, then always use that + return $this->map['*']; + } + + return null; + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/Model.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/Model.php new file mode 100644 index 0000000..2322434 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/Model.php @@ -0,0 +1,64 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Common\Collection; +use Guzzle\Service\Description\Parameter; + +/** + * Default model created when commands create service description model responses + */ +class Model extends Collection +{ + /** @var Parameter Structure of the model */ + protected $structure; + + /** + * @param array $data Data contained by the model + * @param Parameter $structure The structure of the model + */ + public function __construct(array $data = array(), Parameter $structure = null) + { + $this->data = $data; + $this->structure = $structure; + } + + /** + * Get the structure of the model + * + * @return Parameter + */ + public function getStructure() + { + return $this->structure ?: new Parameter(); + } + + /** + * Provides debug information about the model object + * + * @return string + */ + public function __toString() + { + $output = 'Debug output of '; + if ($this->structure) { + $output .= $this->structure->getName() . ' '; + } + $output .= 'model'; + $output = str_repeat('=', strlen($output)) . "\n" . $output . "\n" . str_repeat('=', strlen($output)) . "\n\n"; + $output .= "Model data\n-----------\n\n"; + $output .= "This data can be retrieved from the model object using the get() method of the model " + . "(e.g. \$model->get(\$key)) or accessing the model like an associative array (e.g. \$model['key']).\n\n"; + $lines = array_slice(explode("\n", trim(print_r($this->toArray(), true))), 2, -1); + $output .= implode("\n", $lines); + + if ($this->structure) { + $output .= "\n\nModel structure\n---------------\n\n"; + $output .= "The following JSON document defines how the model was parsed from an HTTP response into the " + . "associative array structure you see above.\n\n"; + $output .= ' ' . json_encode($this->structure->toArray()) . "\n\n"; + } + + return $output . "\n"; + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIterator.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIterator.php new file mode 100644 index 0000000..e141524 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIterator.php @@ -0,0 +1,254 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Common\AbstractHasDispatcher; +use Guzzle\Service\Command\CommandInterface; + +abstract class ResourceIterator extends AbstractHasDispatcher implements ResourceIteratorInterface +{ + /** @var CommandInterface Command used to send requests */ + protected $command; + + /** @var CommandInterface First sent command */ + protected $originalCommand; + + /** @var array Currently loaded resources */ + protected $resources; + + /** @var int Total number of resources that have been retrieved */ + protected $retrievedCount = 0; + + /** @var int Total number of resources that have been iterated */ + protected $iteratedCount = 0; + + /** @var string NextToken/Marker for a subsequent request */ + protected $nextToken = false; + + /** @var int Maximum number of resources to fetch per request */ + protected $pageSize; + + /** @var int Maximum number of resources to retrieve in total */ + protected $limit; + + /** @var int Number of requests sent */ + protected $requestCount = 0; + + /** @var array Initial data passed to the constructor */ + protected $data = array(); + + /** @var bool Whether or not the current value is known to be invalid */ + protected $invalid; + + public static function getAllEvents() + { + return array( + // About to issue another command to get more results + 'resource_iterator.before_send', + // Issued another command to get more results + 'resource_iterator.after_send' + ); + } + + /** + * @param CommandInterface $command Initial command used for iteration + * @param array $data Associative array of additional parameters. You may specify any number of custom + * options for an iterator. Among these options, you may also specify the following values: + * - limit: Attempt to limit the maximum number of resources to this amount + * - page_size: Attempt to retrieve this number of resources per request + */ + public function __construct(CommandInterface $command, array $data = array()) + { + // Clone the command to keep track of the originating command for rewind + $this->originalCommand = $command; + + // Parse options from the array of options + $this->data = $data; + $this->limit = array_key_exists('limit', $data) ? $data['limit'] : 0; + $this->pageSize = array_key_exists('page_size', $data) ? $data['page_size'] : false; + } + + /** + * Get all of the resources as an array (Warning: this could issue a large number of requests) + * + * @return array + */ + public function toArray() + { + return iterator_to_array($this, false); + } + + public function setLimit($limit) + { + $this->limit = $limit; + $this->resetState(); + + return $this; + } + + public function setPageSize($pageSize) + { + $this->pageSize = $pageSize; + $this->resetState(); + + return $this; + } + + /** + * Get an option from the iterator + * + * @param string $key Key of the option to retrieve + * + * @return mixed|null Returns NULL if not set or the value if set + */ + public function get($key) + { + return array_key_exists($key, $this->data) ? $this->data[$key] : null; + } + + /** + * Set an option on the iterator + * + * @param string $key Key of the option to set + * @param mixed $value Value to set for the option + * + * @return ResourceIterator + */ + public function set($key, $value) + { + $this->data[$key] = $value; + + return $this; + } + + public function current() + { + return $this->resources ? current($this->resources) : false; + } + + public function key() + { + return max(0, $this->iteratedCount - 1); + } + + public function count() + { + return $this->retrievedCount; + } + + /** + * Get the total number of requests sent + * + * @return int + */ + public function getRequestCount() + { + return $this->requestCount; + } + + /** + * Rewind the Iterator to the first element and send the original command + */ + public function rewind() + { + // Use the original command + $this->command = clone $this->originalCommand; + $this->resetState(); + $this->next(); + } + + public function valid() + { + return !$this->invalid && (!$this->resources || $this->current() || $this->nextToken) + && (!$this->limit || $this->iteratedCount < $this->limit + 1); + } + + public function next() + { + $this->iteratedCount++; + + // Check if a new set of resources needs to be retrieved + $sendRequest = false; + if (!$this->resources) { + $sendRequest = true; + } else { + // iterate over the internal array + $current = next($this->resources); + $sendRequest = $current === false && $this->nextToken && (!$this->limit || $this->iteratedCount < $this->limit + 1); + } + + if ($sendRequest) { + + $this->dispatch('resource_iterator.before_send', array( + 'iterator' => $this, + 'resources' => $this->resources + )); + + // Get a new command object from the original command + $this->command = clone $this->originalCommand; + // Send a request and retrieve the newly loaded resources + $this->resources = $this->sendRequest(); + $this->requestCount++; + + // If no resources were found, then the last request was not needed + // and iteration must stop + if (empty($this->resources)) { + $this->invalid = true; + } else { + // Add to the number of retrieved resources + $this->retrievedCount += count($this->resources); + // Ensure that we rewind to the beginning of the array + reset($this->resources); + } + + $this->dispatch('resource_iterator.after_send', array( + 'iterator' => $this, + 'resources' => $this->resources + )); + } + } + + /** + * Retrieve the NextToken that can be used in other iterators. + * + * @return string Returns a NextToken + */ + public function getNextToken() + { + return $this->nextToken; + } + + /** + * Returns the value that should be specified for the page size for a request that will maintain any hard limits, + * but still honor the specified pageSize if the number of items retrieved + pageSize < hard limit + * + * @return int Returns the page size of the next request. + */ + protected function calculatePageSize() + { + if ($this->limit && $this->iteratedCount + $this->pageSize > $this->limit) { + return 1 + ($this->limit - $this->iteratedCount); + } + + return (int) $this->pageSize; + } + + /** + * Reset the internal state of the iterator without triggering a rewind() + */ + protected function resetState() + { + $this->iteratedCount = 0; + $this->retrievedCount = 0; + $this->nextToken = false; + $this->resources = null; + $this->invalid = false; + } + + /** + * Send a request to retrieve the next page of results. Hook for subclasses to implement. + * + * @return array Returns the newly loaded resources + */ + abstract protected function sendRequest(); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php new file mode 100644 index 0000000..6aa3615 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorApplyBatched.php @@ -0,0 +1,111 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Common\AbstractHasDispatcher; +use Guzzle\Batch\BatchBuilder; +use Guzzle\Batch\BatchSizeDivisor; +use Guzzle\Batch\BatchClosureTransfer; +use Guzzle\Common\Version; + +/** + * Apply a callback to the contents of a {@see ResourceIteratorInterface} + * @deprecated Will be removed in a future version and is no longer maintained. Use the Batch\ abstractions instead. + * @codeCoverageIgnore + */ +class ResourceIteratorApplyBatched extends AbstractHasDispatcher +{ + /** @var callable|array */ + protected $callback; + + /** @var ResourceIteratorInterface */ + protected $iterator; + + /** @var integer Total number of sent batches */ + protected $batches = 0; + + /** @var int Total number of iterated resources */ + protected $iterated = 0; + + public static function getAllEvents() + { + return array( + // About to send a batch of requests to the callback + 'iterator_batch.before_batch', + // Finished sending a batch of requests to the callback + 'iterator_batch.after_batch', + // Created the batch object + 'iterator_batch.created_batch' + ); + } + + /** + * @param ResourceIteratorInterface $iterator Resource iterator to apply a callback to + * @param array|callable $callback Callback method accepting the resource iterator + * and an array of the iterator's current resources + */ + public function __construct(ResourceIteratorInterface $iterator, $callback) + { + $this->iterator = $iterator; + $this->callback = $callback; + Version::warn(__CLASS__ . ' is deprecated'); + } + + /** + * Apply the callback to the contents of the resource iterator + * + * @param int $perBatch The number of records to group per batch transfer + * + * @return int Returns the number of iterated resources + */ + public function apply($perBatch = 50) + { + $this->iterated = $this->batches = $batches = 0; + $that = $this; + $it = $this->iterator; + $callback = $this->callback; + + $batch = BatchBuilder::factory() + ->createBatchesWith(new BatchSizeDivisor($perBatch)) + ->transferWith(new BatchClosureTransfer(function (array $batch) use ($that, $callback, &$batches, $it) { + $batches++; + $that->dispatch('iterator_batch.before_batch', array('iterator' => $it, 'batch' => $batch)); + call_user_func_array($callback, array($it, $batch)); + $that->dispatch('iterator_batch.after_batch', array('iterator' => $it, 'batch' => $batch)); + })) + ->autoFlushAt($perBatch) + ->build(); + + $this->dispatch('iterator_batch.created_batch', array('batch' => $batch)); + + foreach ($this->iterator as $resource) { + $this->iterated++; + $batch->add($resource); + } + + $batch->flush(); + $this->batches = $batches; + + return $this->iterated; + } + + /** + * Get the total number of batches sent + * + * @return int + */ + public function getBatchCount() + { + return $this->batches; + } + + /** + * Get the total number of iterated resources + * + * @return int + */ + public function getIteratedCount() + { + return $this->iterated; + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorClassFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorClassFactory.php new file mode 100644 index 0000000..2fd9980 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorClassFactory.php @@ -0,0 +1,60 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Inflection\InflectorInterface; +use Guzzle\Inflection\Inflector; +use Guzzle\Service\Command\CommandInterface; + +/** + * Factory for creating {@see ResourceIteratorInterface} objects using a convention of storing iterator classes under a + * root namespace using the name of a {@see CommandInterface} object as a convention for determining the name of an + * iterator class. The command name is converted to CamelCase and Iterator is appended (e.g. abc_foo => AbcFoo). + */ +class ResourceIteratorClassFactory extends AbstractResourceIteratorFactory +{ + /** @var array List of namespaces used to look for classes */ + protected $namespaces; + + /** @var InflectorInterface Inflector used to determine class names */ + protected $inflector; + + /** + * @param string|array $namespaces List of namespaces for iterator objects + * @param InflectorInterface $inflector Inflector used to resolve class names + */ + public function __construct($namespaces = array(), InflectorInterface $inflector = null) + { + $this->namespaces = (array) $namespaces; + $this->inflector = $inflector ?: Inflector::getDefault(); + } + + /** + * Registers a namespace to check for Iterators + * + * @param string $namespace Namespace which contains Iterator classes + * + * @return self + */ + public function registerNamespace($namespace) + { + array_unshift($this->namespaces, $namespace); + + return $this; + } + + protected function getClassName(CommandInterface $command) + { + $iteratorName = $this->inflector->camel($command->getName()) . 'Iterator'; + + // Determine the name of the class to load + foreach ($this->namespaces as $namespace) { + $potentialClassName = $namespace . '\\' . $iteratorName; + if (class_exists($potentialClassName)) { + return $potentialClassName; + } + } + + return false; + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorFactoryInterface.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorFactoryInterface.php new file mode 100644 index 0000000..8b4e8db --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorFactoryInterface.php @@ -0,0 +1,30 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Service\Command\CommandInterface; + +/** + * Factory for creating {@see ResourceIteratorInterface} objects + */ +interface ResourceIteratorFactoryInterface +{ + /** + * Create a resource iterator + * + * @param CommandInterface $command Command to create an iterator for + * @param array $options Iterator options that are exposed as data. + * + * @return ResourceIteratorInterface + */ + public function build(CommandInterface $command, array $options = array()); + + /** + * Check if the factory can create an iterator + * + * @param CommandInterface $command Command to create an iterator for + * + * @return bool + */ + public function canBuild(CommandInterface $command); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorInterface.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorInterface.php new file mode 100644 index 0000000..dbaafde --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Resource/ResourceIteratorInterface.php @@ -0,0 +1,61 @@ +<?php + +namespace Guzzle\Service\Resource; + +use Guzzle\Common\HasDispatcherInterface; +use Guzzle\Common\ToArrayInterface; + +/** + * Iterates over a paginated resource using subsequent requests in order to retrieve the entire matching result set + */ +interface ResourceIteratorInterface extends ToArrayInterface, HasDispatcherInterface, \Iterator, \Countable +{ + /** + * Retrieve the NextToken that can be used in other iterators. + * + * @return string Returns a NextToken + */ + public function getNextToken(); + + /** + * Attempt to limit the total number of resources returned by the iterator. + * + * You may still receive more items than you specify. Set to 0 to specify no limit. + * + * @param int $limit Limit amount + * + * @return ResourceIteratorInterface + */ + public function setLimit($limit); + + /** + * Attempt to limit the total number of resources retrieved per request by the iterator. + * + * The iterator may return more than you specify in the page size argument depending on the service and underlying + * command implementation. Set to 0 to specify no page size limitation. + * + * @param int $pageSize Limit amount + * + * @return ResourceIteratorInterface + */ + public function setPageSize($pageSize); + + /** + * Get a data option from the iterator + * + * @param string $key Key of the option to retrieve + * + * @return mixed|null Returns NULL if not set or the value if set + */ + public function get($key); + + /** + * Set a data option on the iterator + * + * @param string $key Key of the option to set + * @param mixed $value Value to set for the option + * + * @return ResourceIteratorInterface + */ + public function set($key, $value); +} |