public function onInit(InitEvent $event)
 {
     $command = $event->getCommand();
     $errors = [];
     $operation = $this->description->getOperation($command->getName());
     foreach ($operation->getParams() as $name => $schema) {
         $value = $command[$name];
         if (!$this->validator->validate($schema, $value)) {
             $errors = array_merge($errors, $this->validator->getErrors());
         } elseif ($value !== $command[$name]) {
             // Update the config value if it changed and no validation
             // errors were encountered
             $command[$name] = $value;
         }
     }
     if ($params = $operation->getAdditionalParameters()) {
         foreach ($command->toArray() as $name => $value) {
             // It's only additional if it isn't defined in the schema
             if (!$operation->hasParam($name)) {
                 // Always set the name so that error messages are useful
                 $params->setName($name);
                 if (!$this->validator->validate($params, $value)) {
                     $errors = array_merge($errors, $this->validator->getErrors());
                 } elseif ($value !== $command[$name]) {
                     $command[$name] = $value;
                 }
             }
         }
     }
     if ($errors) {
         throw new CommandException('Validation errors: ' . implode("\n", $errors), $event->getTransaction());
     }
 }
示例#2
0
 /**
  * @param PreparedEvent $event
  * @return array
  */
 protected function getRequestOptions(PreparedEvent $event)
 {
     $command = $event->getCommand();
     $operation = $this->description->getOperation($command->getName());
     $requestOptions = $operation->getData('requestOptions');
     return is_array($requestOptions) ? $requestOptions : array();
 }
 public function onProcess(ProcessEvent $event)
 {
     // Only add a result object if no exception was encountered.
     if ($event->getException()) {
         return;
     }
     $command = $event->getCommand();
     $operation = $this->description->getOperation($command->getName());
     $rawConfig = $operation->toArray();
     if (empty($rawConfig['responseParser'])) {
         return;
     }
     $parsedResponse = $this->parseResponse($event, $rawConfig['responseParser']);
     $event->setResult($parsedResponse);
 }
 /**
  * Creates a callable function used to create command objects from a
  * service description.
  *
  * @param DescriptionInterface $description Service description
  *
  * @return callable Returns a command factory
  */
 public static function defaultCommandFactory(DescriptionInterface $description)
 {
     return function ($name, array $args = [], GuzzleClientInterface $client) use($description) {
         $operation = null;
         if ($description->hasOperation($name)) {
             $operation = $description->getOperation($name);
         } else {
             $name = ucfirst($name);
             if ($description->hasOperation($name)) {
                 $operation = $description->getOperation($name);
             }
         }
         if (!$operation) {
             return null;
         }
         return new Command($operation, $args, clone $client->getEmitter());
     };
 }
 /**
  * Stops propagation of ProcessEvents when using Batching
  *
  * @param  ProcessEvent $event The Process Event
  *
  * @return bool
  */
 public function onProcess(ProcessEvent $event)
 {
     $command = $event->getCommand();
     $operation = $this->description->getOperation($command->getName());
     if (!$operation->getData('batching')) {
         return false;
     }
     $event->stopPropagation();
     return true;
 }
 /**
  * @param ProcessEvent $event
  * @return ResponseInterface|null
  */
 protected function createResult(ProcessEvent $event)
 {
     // Only add a result object if no exception was encountered.
     if ($event->getException()) {
         return null;
     }
     $command = $event->getCommand();
     // Do not overwrite a previous result
     if ($event->getResult()) {
         return null;
     }
     $operation = $this->description->getOperation($command->getName());
     $responseClass = $operation->getResponseModel();
     if ($responseClass === null) {
         throw new Exception\RuntimeException(sprintf('No response class configured for operation "%s"', $command->getName()));
     }
     if (!class_exists($responseClass)) {
         throw new Exception\RuntimeException(sprintf('Response class "%s" of operation "%s" does not exist', $responseClass, $command->getName()));
     }
     /** @todo We could check if the response class implements ResponseInterface */
     /** @var ResponseInterface $responseClass */
     return $responseClass::fromOperation($operation, $event);
 }
 public function onProcess(ProcessEvent $event)
 {
     // Only add a result object if no exception was encountered.
     if ($event->getException()) {
         return;
     }
     $command = $event->getCommand();
     // Do not overwrite a previous result
     if ($event->getResult()) {
         return;
     }
     $operation = $this->description->getOperation($command->getName());
     // Add a default Model as the result if no matching schema was found.
     if (!($modelName = $operation->getResponseModel())) {
         $event->setResult([]);
         return;
     }
     $model = $operation->getServiceDescription()->getModel($modelName);
     if (!$model) {
         throw new \RuntimeException("Unknown model: {$modelName}");
     }
     $event->setResult($this->visit($model, $event));
 }
示例#8
0
 /**
  * Create a request for an operation with a uri merged onto a base URI
  */
 private function createCommandWithUri(Operation $operation, CommandInterface $command, ServiceClientInterface $client)
 {
     // Get the path values and use the client config settings
     $variables = [];
     foreach ($operation->getParams() as $name => $arg) {
         /* @var Parameter $arg */
         if ($arg->getLocation() == 'uri') {
             if (isset($command[$name])) {
                 $variables[$name] = $arg->filter($command[$name]);
                 if (!is_array($variables[$name])) {
                     $variables[$name] = (string) $variables[$name];
                 }
             }
         }
     }
     // Expand the URI template.
     $uri = Utils::uriTemplate($operation->getUri(), $variables);
     return $client->getHttpClient()->createRequest($operation->getHttpMethod(), $this->description->getBaseUrl()->combine($uri), $command['request_options'] ?: []);
 }