123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace GuzzleHttp\Command\Guzzle;
- use GuzzleHttp\ClientInterface;
- use GuzzleHttp\Command\CommandInterface;
- use GuzzleHttp\Command\Guzzle\Handler\ValidatedDescriptionHandler;
- use GuzzleHttp\Command\ServiceClient;
- use GuzzleHttp\HandlerStack;
- class GuzzleClient extends ServiceClient
- {
-
- private $config;
-
- private $description;
-
- public function __construct(
- ClientInterface $client,
- DescriptionInterface $description,
- callable $commandToRequestTransformer = null,
- callable $responseToResultTransformer = null,
- HandlerStack $commandHandlerStack = null,
- array $config = []
- ) {
- $this->config = $config;
- $this->description = $description;
- $serializer = $this->getSerializer($commandToRequestTransformer);
- $deserializer = $this->getDeserializer($responseToResultTransformer);
- parent::__construct($client, $serializer, $deserializer, $commandHandlerStack);
- $this->processConfig($config);
- }
-
- public function getCommand($name, array $args = [])
- {
- if (!$this->description->hasOperation($name)) {
- $name = ucfirst($name);
- if (!$this->description->hasOperation($name)) {
- throw new \InvalidArgumentException(
- "No operation found named {$name}"
- );
- }
- }
-
- $args += $this->getConfig('defaults');
- return parent::getCommand($name, $args);
- }
-
- public function getDescription()
- {
- return $this->description;
- }
-
- private function getSerializer($commandToRequestTransformer)
- {
- return $commandToRequestTransformer !== null
- ? $commandToRequestTransformer
- : new Serializer($this->description);
- }
-
- private function getDeserializer($responseToResultTransformer)
- {
- $process = (! isset($this->config['process']) || $this->config['process'] === true);
- return $responseToResultTransformer !== null
- ? $responseToResultTransformer
- : new Deserializer($this->description, $process);
- }
-
- public function getConfig($option = null)
- {
- return $option === null
- ? $this->config
- : (isset($this->config[$option]) ? $this->config[$option] : []);
- }
-
- public function setConfig($option, $value)
- {
- $this->config[$option] = $value;
- }
-
- protected function processConfig(array $config)
- {
-
- if (!isset($config['defaults'])) {
- $config['defaults'] = [];
- }
-
- $stack = $this->getHandlerStack();
- if (!isset($config['validate']) || $config['validate'] === true) {
- $stack->push(new ValidatedDescriptionHandler($this->description), 'validate_description');
- }
- if (!isset($config['process']) || $config['process'] === true) {
-
-
-
- }
- }
- }
|