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->tokenProvider = $config['token']; $this->region = isset($config['region']) ? $config['region'] : null; $this->config = $config['config']; $this->setClientBuiltIns($args); $this->clientContextParams = $this->setClientContextParams($args); $this->defaultRequestOptions = $config['http']; $this->endpointProvider = $config['endpoint_provider']; $this->serializer = $config['serializer']; $this->addSignatureMiddleware(); $this->addInvocationId(); $this->addEndpointParameterMiddleware($args); $this->addEndpointDiscoveryMiddleware($config, $args); $this->addRequestCompressionMiddleware($config); $this->loadAliases(); $this->addStreamRequestPayload(); $this->addRecursionDetection(); $this->addRequestBuilder(); 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 getEndpointProvider() { return $this->endpointProvider; } /** * Provides the set of service context parameter * key-value pairs used for endpoint resolution. * * @return array */ public function getClientContextParams() { return $this->clientContextParams; } /** * Provides the set of built-in keys and values * used for endpoint resolution * * @return array */ public function getClientBuiltIns() { return $this->clientBuiltIns; } 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 ['', AwsException::class]; } $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; case 'bearer': $version = 'bearer'; break; } if (isset($c['@context']['signature_version'])) { if ($c['@context']['signature_version'] == 'v4a') { $version = 'v4a'; } } if (!empty($endpointAuthSchemes = $c->getAuthSchemes())) { $version = $endpointAuthSchemes['version']; $name = isset($endpointAuthSchemes['name']) ? $endpointAuthSchemes['name'] : $name; $region = isset($endpointAuthSchemes['region']) ? $endpointAuthSchemes['region'] : $region; } return SignatureProvider::resolve($provider, $version, $name, $region); }; $this->handlerList->appendSign( Middleware::signer($this->credentialProvider, $resolver, $this->tokenProvider), 'signer' ); } private function addRequestCompressionMiddleware($config) { if (empty($config['disable_request_compression'])) { $list = $this->getHandlerList(); $list->appendBuild( RequestCompressionMiddleware::wrap($config), 'request-compression' ); } } 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' ); } private function addRecursionDetection() { // Add recursion detection header to requests // originating in supported Lambda runtimes $this->handlerList->appendBuild( Middleware::recursionDetection(), 'recursion-detection' ); } /** * Adds the `builder` middleware such that a client's endpoint * provider and endpoint resolution arguments can be passed. */ private function addRequestBuilder() { $handlerList = $this->getHandlerList(); $serializer = $this->serializer; $endpointProvider = $this->endpointProvider; $endpointArgs = $this->getEndpointProviderArgs(); $handlerList->prependBuild( Middleware::requestBuilder( $serializer, $endpointProvider, $endpointArgs ), 'builderV2' ); } /** * Retrieves client context param definition from service model, * creates mapping of client context param names with client-provided * values. * * @return array */ private function setClientContextParams($args) { $api = $this->getApi(); $resolvedParams = []; if (!empty($paramDefinitions = $api->getClientContextParams())) { foreach($paramDefinitions as $paramName => $paramValue) { if (isset($args[$paramName])) { $result[$paramName] = $args[$paramName]; } } } return $resolvedParams; } /** * Retrieves and sets default values used for endpoint resolution. */ private function setClientBuiltIns($args) { $builtIns = []; $config = $this->getConfig(); $service = $args['service']; $builtIns['SDK::Endpoint'] = isset($args['endpoint']) ? $args['endpoint'] : null; $builtIns['AWS::Region'] = $this->getRegion(); $builtIns['AWS::UseFIPS'] = $config['use_fips_endpoint']->isUseFipsEndpoint(); $builtIns['AWS::UseDualStack'] = $config['use_dual_stack_endpoint']->isUseDualstackEndpoint(); if ($service === 's3' || $service === 's3control'){ $builtIns['AWS::S3::UseArnRegion'] = $config['use_arn_region']->isUseArnRegion(); } if ($service === 's3') { $builtIns['AWS::S3::UseArnRegion'] = $config['use_arn_region']->isUseArnRegion(); $builtIns['AWS::S3::Accelerate'] = $config['use_accelerate_endpoint']; $builtIns['AWS::S3::ForcePathStyle'] = $config['use_path_style_endpoint']; $builtIns['AWS::S3::DisableMultiRegionAccessPoints'] = $config['disable_multiregion_access_points']; } $this->clientBuiltIns += $builtIns; } /** * Retrieves arguments to be used in endpoint resolution. * * @return array */ public function getEndpointProviderArgs() { return $this->normalizeEndpointProviderArgs(); } /** * Combines built-in and client context parameter values in * order of specificity. Client context parameter values supersede * built-in values. * * @return array */ private function normalizeEndpointProviderArgs() { $normalizedBuiltIns = []; foreach($this->clientBuiltIns as $name => $value) { $normalizedName = explode('::', $name); $normalizedName = $normalizedName[count($normalizedName) - 1]; $normalizedBuiltIns[$normalizedName] = $value; } return array_merge($normalizedBuiltIns, $this->getClientContextParams()); } protected function isUseEndpointV2() { return $this->endpointProvider instanceof EndpointProviderV2; } public static function emitDeprecationWarning() { $phpVersion = PHP_VERSION_ID; if ($phpVersion < 70205) { $phpVersionString = phpversion(); @trigger_error( "This installation of the SDK is using PHP version" . " {$phpVersionString}, which will be deprecated on August" . " 15th, 2023. Please upgrade your PHP version to a minimum of" . " 7.2.5 before then to continue receiving updates to the AWS" . " SDK for PHP. To disable this warning, set" . " suppress_php_deprecation_warning to true on the client constructor" . " or set the environment variable AWS_SUPPRESS_PHP_DEPRECATION_WARNING" . " to true.", E_USER_DEPRECATED ); } } /** * 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); } }