parseClass(); if (!isset($args['service'])) { $args['service'] = manifest($service)['endpoint']; } if (!isset($args['exception_class'])) { $args['exception_class'] = $exceptionClass; } $this->handlerList = new HandlerList(); $resolver = new ClientResolver(static::getArguments()); $config = $resolver->resolve($args, $this->handlerList); $this->api = $config['api']; $this->signatureProvider = $config['signature_provider']; $this->endpoint = new Uri($config['endpoint']); $this->credentialProvider = $config['credentials']; $this->region = isset($config['region']) ? $config['region'] : null; $this->config = $config['config']; $this->defaultRequestOptions = $config['http']; $this->addSignatureMiddleware(); $this->addInvocationId(); $this->addEndpointParameterMiddleware($args); $this->addEndpointDiscoveryMiddleware($config, $args); $this->loadAliases(); $this->addStreamRequestPayload(); if (isset($args['with_resolved'])) { $args['with_resolved']($config); } } public function getHandlerList() { return $this->handlerList; } public function getConfig($option = null) { return $option === null ? $this->config : (isset($this->config[$option]) ? $this->config[$option] : null); } public function getCredentials() { $fn = $this->credentialProvider; return $fn(); } public function getEndpoint() { return $this->endpoint; } public function getRegion() { return $this->region; } public function getApi() { return $this->api; } public function getCommand($name, array $args = []) { // Fail fast if the command cannot be found in the description. if (!isset($this->getApi()['operations'][$name])) { $name = ucfirst($name); if (!isset($this->getApi()['operations'][$name])) { throw new \InvalidArgumentException("Operation not found: $name"); } } if (!isset($args['@http'])) { $args['@http'] = $this->defaultRequestOptions; } else { $args['@http'] += $this->defaultRequestOptions; } return new Command($name, $args, clone $this->getHandlerList()); } public function __sleep() { throw new \RuntimeException('Instances of ' . static::class . ' cannot be serialized'); } /** * Get the signature_provider function of the client. * * @return callable */ final public function getSignatureProvider() { return $this->signatureProvider; } /** * Parse the class name and setup the custom exception class of the client * and return the "service" name of the client and "exception_class". * * @return array */ private function parseClass() { $klass = get_class($this); if ($klass === __CLASS__) { return ['', 'Aws\Exception\AwsException']; } $service = substr($klass, strrpos($klass, '\\') + 1, -6); return [ strtolower($service), "Aws\\{$service}\\Exception\\{$service}Exception" ]; } private function addEndpointParameterMiddleware($args) { if (empty($args['disable_host_prefix_injection'])) { $list = $this->getHandlerList(); $list->appendBuild( EndpointParameterMiddleware::wrap( $this->api ), 'endpoint_parameter' ); } } private function addEndpointDiscoveryMiddleware($config, $args) { $list = $this->getHandlerList(); if (!isset($args['endpoint'])) { $list->appendBuild( EndpointDiscoveryMiddleware::wrap( $this, $args, $config['endpoint_discovery'] ), 'EndpointDiscoveryMiddleware' ); } } private function addSignatureMiddleware() { $api = $this->getApi(); $provider = $this->signatureProvider; $version = $this->config['signature_version']; $name = $this->config['signing_name']; $region = $this->config['signing_region']; $resolver = static function ( CommandInterface $c ) use ($api, $provider, $name, $region, $version) { if (!empty($c['@context']['signing_region'])) { $region = $c['@context']['signing_region']; } if (!empty($c['@context']['signing_service'])) { $name = $c['@context']['signing_service']; } $authType = $api->getOperation($c->getName())['authtype']; switch ($authType){ case 'none': $version = 'anonymous'; break; case 'v4-unsigned-body': $version = 'v4-unsigned-body'; break; } return SignatureProvider::resolve($provider, $version, $name, $region); }; $this->handlerList->appendSign( Middleware::signer($this->credentialProvider, $resolver), 'signer' ); } private function addInvocationId() { // Add invocation id to each request $this->handlerList->prependSign(Middleware::invocationId(), 'invocation-id'); } private function loadAliases($file = null) { if (!isset($this->aliases)) { if (is_null($file)) { $file = __DIR__ . '/data/aliases.json'; } $aliases = \Aws\load_compiled_json($file); $serviceId = $this->api->getServiceId(); $version = $this->getApi()->getApiVersion(); if (!empty($aliases['operations'][$serviceId][$version])) { $this->aliases = array_flip($aliases['operations'][$serviceId][$version]); } } } private function addStreamRequestPayload() { $streamRequestPayloadMiddleware = StreamRequestPayloadMiddleware::wrap( $this->api ); $this->handlerList->prependSign( $streamRequestPayloadMiddleware, 'StreamRequestPayloadMiddleware' ); } /** * Returns a service model and doc model with any necessary changes * applied. * * @param array $api Array of service data being documented. * @param array $docs Array of doc model data. * * @return array Tuple containing a [Service, DocModel] * * @internal This should only used to document the service API. * @codeCoverageIgnore */ public static function applyDocFilters(array $api, array $docs) { $aliases = \Aws\load_compiled_json(__DIR__ . '/data/aliases.json'); $serviceId = $api['metadata']['serviceId']; $version = $api['metadata']['apiVersion']; // Replace names for any operations with SDK aliases if (!empty($aliases['operations'][$serviceId][$version])) { foreach ($aliases['operations'][$serviceId][$version] as $op => $alias) { $api['operations'][$alias] = $api['operations'][$op]; $docs['operations'][$alias] = $docs['operations'][$op]; unset($api['operations'][$op], $docs['operations'][$op]); } } ksort($api['operations']); return [ new Service($api, ApiProvider::defaultProvider()), new DocModel($docs) ]; } /** * @deprecated * @return static */ public static function factory(array $config = []) { return new static($config); } }