diff options
Diffstat (limited to 'vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory')
6 files changed, 359 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/AliasFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/AliasFactory.php new file mode 100644 index 0000000..1c5ce07 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/AliasFactory.php @@ -0,0 +1,39 @@ +<?php + +namespace Guzzle\Service\Command\Factory; + +use Guzzle\Common\Exception\InvalidArgumentException; +use Guzzle\Service\ClientInterface; + +/** + * Command factory used when you need to provide aliases to commands + */ +class AliasFactory implements FactoryInterface +{ + /** @var array Associative array mapping command aliases to the aliased command */ + protected $aliases; + + /** @var ClientInterface Client used to retry using aliases */ + protected $client; + + /** + * @param ClientInterface $client Client used to retry with the alias + * @param array $aliases Associative array mapping aliases to the alias + */ + public function __construct(ClientInterface $client, array $aliases) + { + $this->client = $client; + $this->aliases = $aliases; + } + + public function factory($name, array $args = array()) + { + if (isset($this->aliases[$name])) { + try { + return $this->client->getCommand($this->aliases[$name], $args); + } catch (InvalidArgumentException $e) { + return null; + } + } + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/CompositeFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/CompositeFactory.php new file mode 100644 index 0000000..8c46983 --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/CompositeFactory.php @@ -0,0 +1,154 @@ +<?php + +namespace Guzzle\Service\Command\Factory; + +use Guzzle\Service\Command\CommandInterface; +use Guzzle\Service\ClientInterface; + +/** + * Composite factory used by a client object to create command objects utilizing multiple factories + */ +class CompositeFactory implements \IteratorAggregate, \Countable, FactoryInterface +{ + /** @var array Array of command factories */ + protected $factories; + + /** + * Get the default chain to use with clients + * + * @param ClientInterface $client Client to base the chain on + * + * @return self + */ + public static function getDefaultChain(ClientInterface $client) + { + $factories = array(); + if ($description = $client->getDescription()) { + $factories[] = new ServiceDescriptionFactory($description); + } + $factories[] = new ConcreteClassFactory($client); + + return new self($factories); + } + + /** + * @param array $factories Array of command factories + */ + public function __construct(array $factories = array()) + { + $this->factories = $factories; + } + + /** + * Add a command factory to the chain + * + * @param FactoryInterface $factory Factory to add + * @param string|FactoryInterface $before Insert the new command factory before a command factory class or object + * matching a class name. + * @return CompositeFactory + */ + public function add(FactoryInterface $factory, $before = null) + { + $pos = null; + + if ($before) { + foreach ($this->factories as $i => $f) { + if ($before instanceof FactoryInterface) { + if ($f === $before) { + $pos = $i; + break; + } + } elseif (is_string($before)) { + if ($f instanceof $before) { + $pos = $i; + break; + } + } + } + } + + if ($pos === null) { + $this->factories[] = $factory; + } else { + array_splice($this->factories, $i, 0, array($factory)); + } + + return $this; + } + + /** + * Check if the chain contains a specific command factory + * + * @param FactoryInterface|string $factory Factory to check + * + * @return bool + */ + public function has($factory) + { + return (bool) $this->find($factory); + } + + /** + * Remove a specific command factory from the chain + * + * @param string|FactoryInterface $factory Factory to remove by name or instance + * + * @return CompositeFactory + */ + public function remove($factory = null) + { + if (!($factory instanceof FactoryInterface)) { + $factory = $this->find($factory); + } + + $this->factories = array_values(array_filter($this->factories, function($f) use ($factory) { + return $f !== $factory; + })); + + return $this; + } + + /** + * Get a command factory by class name + * + * @param string|FactoryInterface $factory Command factory class or instance + * + * @return null|FactoryInterface + */ + public function find($factory) + { + foreach ($this->factories as $f) { + if ($factory === $f || (is_string($factory) && $f instanceof $factory)) { + return $f; + } + } + } + + /** + * Create a command using the associated command factories + * + * @param string $name Name of the command + * @param array $args Command arguments + * + * @return CommandInterface + */ + public function factory($name, array $args = array()) + { + foreach ($this->factories as $factory) { + $command = $factory->factory($name, $args); + if ($command) { + return $command; + } + } + } + + public function count() + { + return count($this->factories); + } + + public function getIterator() + { + return new \ArrayIterator($this->factories); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/ConcreteClassFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/ConcreteClassFactory.php new file mode 100644 index 0000000..0e93dea --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/ConcreteClassFactory.php @@ -0,0 +1,47 @@ +<?php + +namespace Guzzle\Service\Command\Factory; + +use Guzzle\Inflection\InflectorInterface; +use Guzzle\Inflection\Inflector; +use Guzzle\Service\ClientInterface; + +/** + * Command factory used to create commands referencing concrete command classes + */ +class ConcreteClassFactory implements FactoryInterface +{ + /** @var ClientInterface */ + protected $client; + + /** @var InflectorInterface */ + protected $inflector; + + /** + * @param ClientInterface $client Client that owns the commands + * @param InflectorInterface $inflector Inflector used to resolve class names + */ + public function __construct(ClientInterface $client, InflectorInterface $inflector = null) + { + $this->client = $client; + $this->inflector = $inflector ?: Inflector::getDefault(); + } + + public function factory($name, array $args = array()) + { + // Determine the class to instantiate based on the namespace of the current client and the default directory + $prefix = $this->client->getConfig('command.prefix'); + if (!$prefix) { + // The prefix can be specified in a factory method and is cached + $prefix = implode('\\', array_slice(explode('\\', get_class($this->client)), 0, -1)) . '\\Command\\'; + $this->client->getConfig()->set('command.prefix', $prefix); + } + + $class = $prefix . str_replace(' ', '\\', ucwords(str_replace('.', ' ', $this->inflector->camel($name)))); + + // Create the concrete command if it exists + if (class_exists($class)) { + return new $class($args); + } + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/FactoryInterface.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/FactoryInterface.php new file mode 100644 index 0000000..35c299d --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/FactoryInterface.php @@ -0,0 +1,21 @@ +<?php + +namespace Guzzle\Service\Command\Factory; + +use Guzzle\Service\Command\CommandInterface; + +/** + * Interface for creating commands by name + */ +interface FactoryInterface +{ + /** + * Create a command by name + * + * @param string $name Command to create + * @param array $args Command arguments + * + * @return CommandInterface|null + */ + public function factory($name, array $args = array()); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/MapFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/MapFactory.php new file mode 100644 index 0000000..0ad80bc --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/MapFactory.php @@ -0,0 +1,27 @@ +<?php + +namespace Guzzle\Service\Command\Factory; + +/** + * Command factory used when explicitly mapping strings to command classes + */ +class MapFactory implements FactoryInterface +{ + /** @var array Associative array mapping command names to classes */ + protected $map; + + /** @param array $map Associative array mapping command names to classes */ + public function __construct(array $map) + { + $this->map = $map; + } + + public function factory($name, array $args = array()) + { + if (isset($this->map[$name])) { + $class = $this->map[$name]; + + return new $class($args); + } + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/ServiceDescriptionFactory.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/ServiceDescriptionFactory.php new file mode 100644 index 0000000..b943a5b --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Factory/ServiceDescriptionFactory.php @@ -0,0 +1,71 @@ +<?php + +namespace Guzzle\Service\Command\Factory; + +use Guzzle\Service\Description\ServiceDescriptionInterface; +use Guzzle\Inflection\InflectorInterface; + +/** + * Command factory used to create commands based on service descriptions + */ +class ServiceDescriptionFactory implements FactoryInterface +{ + /** @var ServiceDescriptionInterface */ + protected $description; + + /** @var InflectorInterface */ + protected $inflector; + + /** + * @param ServiceDescriptionInterface $description Service description + * @param InflectorInterface $inflector Optional inflector to use if the command is not at first found + */ + public function __construct(ServiceDescriptionInterface $description, InflectorInterface $inflector = null) + { + $this->setServiceDescription($description); + $this->inflector = $inflector; + } + + /** + * Change the service description used with the factory + * + * @param ServiceDescriptionInterface $description Service description to use + * + * @return FactoryInterface + */ + public function setServiceDescription(ServiceDescriptionInterface $description) + { + $this->description = $description; + + return $this; + } + + /** + * Returns the service description + * + * @return ServiceDescriptionInterface + */ + public function getServiceDescription() + { + return $this->description; + } + + public function factory($name, array $args = array()) + { + $command = $this->description->getOperation($name); + + // If a command wasn't found, then try to uppercase the first letter and try again + if (!$command) { + $command = $this->description->getOperation(ucfirst($name)); + // If an inflector was passed, then attempt to get the command using snake_case inflection + if (!$command && $this->inflector) { + $command = $this->description->getOperation($this->inflector->snake($name)); + } + } + + if ($command) { + $class = $command->getClass(); + return new $class($args, $command, $this->description); + } + } +} |