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->addClientSideMonitoring($args); $this->addEndpointParameterMiddleware($args); $this->addEndpointDiscoveryMiddleware($config, $args); 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 protected 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) { $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 addClientSideMonitoring($args) { $options = ConfigurationProvider::defaultProvider($args); $this->handlerList->appendBuild( ApiCallMonitoringMiddleware::wrap( $this->credentialProvider, $options, $this->region, $this->getApi()->getServiceId() ), 'ApiCallMonitoringMiddleware' ); $callAttemptMiddleware = ApiCallAttemptMonitoringMiddleware::wrap( $this->credentialProvider, $options, $this->region, $this->getApi()->getServiceId() ); $this->handlerList->appendAttempt ( $callAttemptMiddleware, 'ApiCallAttemptMonitoringMiddleware' ); } /** * 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) { return [ new Service($api, ApiProvider::defaultProvider()), new DocModel($docs) ]; } /** * @deprecated * @return static */ public static function factory(array $config = []) { return new static($config); } }