vendor/aws/aws-sdk-php/src/AwsClient.php line 142

Open in your IDE?
  1. <?php
  2. namespace Aws;
  3. use Aws\Api\ApiProvider;
  4. use Aws\Api\DocModel;
  5. use Aws\Api\Service;
  6. use Aws\Signature\SignatureProvider;
  7. use GuzzleHttp\Psr7\Uri;
  8. /**
  9.  * Default AWS client implementation
  10.  */
  11. class AwsClient implements AwsClientInterface
  12. {
  13.     /** @var string */
  14.     private $region;
  15.     /** @var string */
  16.     private $endpoint;
  17.     /** @var Service */
  18.     private $api;
  19.     /** @var callable */
  20.     private $signatureProvider;
  21.     /** @var callable */
  22.     private $credentialProvider;
  23.     /** @var HandlerList */
  24.     private $handlerList;
  25.     /** @var array*/
  26.     private $defaultRequestOptions;
  27.     /**
  28.      * Get an array of client constructor arguments used by the client.
  29.      *
  30.      * @return array
  31.      */
  32.     public static function getArguments()
  33.     {
  34.         return ClientResolver::getDefaultArguments();
  35.     }
  36.     /**
  37.      * The client constructor accepts the following options:
  38.      *
  39.      * - api_provider: (callable) An optional PHP callable that accepts a
  40.      *   type, service, and version argument, and returns an array of
  41.      *   corresponding configuration data. The type value can be one of api,
  42.      *   waiter, or paginator.
  43.      * - credentials:
  44.      *   (Aws\Credentials\CredentialsInterface|array|bool|callable) Specifies
  45.      *   the credentials used to sign requests. Provide an
  46.      *   Aws\Credentials\CredentialsInterface object, an associative array of
  47.      *   "key", "secret", and an optional "token" key, `false` to use null
  48.      *   credentials, or a callable credentials provider used to create
  49.      *   credentials or return null. See Aws\Credentials\CredentialProvider for
  50.      *   a list of built-in credentials providers. If no credentials are
  51.      *   provided, the SDK will attempt to load them from the environment.
  52.      * - debug: (bool|array) Set to true to display debug information when
  53.      *   sending requests. Alternatively, you can provide an associative array
  54.      *   with the following keys: logfn: (callable) Function that is invoked
  55.      *   with log messages; stream_size: (int) When the size of a stream is
  56.      *   greater than this number, the stream data will not be logged (set to
  57.      *   "0" to not log any stream data); scrub_auth: (bool) Set to false to
  58.      *   disable the scrubbing of auth data from the logged messages; http:
  59.      *   (bool) Set to false to disable the "debug" feature of lower level HTTP
  60.      *   adapters (e.g., verbose curl output).
  61.      * - endpoint: (string) The full URI of the webservice. This is only
  62.      *   required when connecting to a custom endpoint (e.g., a local version
  63.      *   of S3).
  64.      * - endpoint_provider: (callable) An optional PHP callable that
  65.      *   accepts a hash of options including a "service" and "region" key and
  66.      *   returns NULL or a hash of endpoint data, of which the "endpoint" key
  67.      *   is required. See Aws\Endpoint\EndpointProvider for a list of built-in
  68.      *   providers.
  69.      * - handler: (callable) A handler that accepts a command object,
  70.      *   request object and returns a promise that is fulfilled with an
  71.      *   Aws\ResultInterface object or rejected with an
  72.      *   Aws\Exception\AwsException. A handler does not accept a next handler
  73.      *   as it is terminal and expected to fulfill a command. If no handler is
  74.      *   provided, a default Guzzle handler will be utilized.
  75.      * - http: (array, default=array(0)) Set to an array of SDK request
  76.      *   options to apply to each request (e.g., proxy, verify, etc.).
  77.      * - http_handler: (callable) An HTTP handler is a function that
  78.      *   accepts a PSR-7 request object and returns a promise that is fulfilled
  79.      *   with a PSR-7 response object or rejected with an array of exception
  80.      *   data. NOTE: This option supersedes any provided "handler" option.
  81.      * - profile: (string) Allows you to specify which profile to use when
  82.      *   credentials are created from the AWS credentials file in your HOME
  83.      *   directory. This setting overrides the AWS_PROFILE environment
  84.      *   variable. Note: Specifying "profile" will cause the "credentials" key
  85.      *   to be ignored.
  86.      * - region: (string, required) Region to connect to. See
  87.      *   http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of
  88.      *   available regions.
  89.      * - retries: (int, default=int(3)) Configures the maximum number of
  90.      *   allowed retries for a client (pass 0 to disable retries).
  91.      * - scheme: (string, default=string(5) "https") URI scheme to use when
  92.      *   connecting connect. The SDK will utilize "https" endpoints (i.e.,
  93.      *   utilize SSL/TLS connections) by default. You can attempt to connect to
  94.      *   a service over an unencrypted "http" endpoint by setting ``scheme`` to
  95.      *   "http".
  96.      * - signature_provider: (callable) A callable that accepts a signature
  97.      *   version name (e.g., "v4"), a service name, and region, and
  98.      *   returns a SignatureInterface object or null. This provider is used to
  99.      *   create signers utilized by the client. See
  100.      *   Aws\Signature\SignatureProvider for a list of built-in providers
  101.      * - signature_version: (string) A string representing a custom
  102.      *   signature version to use with a service (e.g., v4). Note that
  103.      *   per/operation signature version MAY override this requested signature
  104.      *   version.
  105.      * - validate: (bool, default=bool(true)) Set to false to disable
  106.      *   client-side parameter validation.
  107.      * - version: (string, required) The version of the webservice to
  108.      *   utilize (e.g., 2006-03-01).
  109.      *
  110.      * @param array $args Client configuration arguments.
  111.      *
  112.      * @throws \InvalidArgumentException if any required options are missing
  113.      */
  114.     public function __construct(array $args)
  115.     {
  116.         list($service$exceptionClass) = $this->parseClass();
  117.         if (!isset($args['service'])) {
  118.             $args['service'] = $service;
  119.         }
  120.         if (!isset($args['exception_class'])) {
  121.             $args['exception_class'] = $exceptionClass;
  122.         }
  123.         $this->handlerList = new HandlerList();
  124.         $resolver = new ClientResolver(static::getArguments());
  125.         $config $resolver->resolve($args$this->handlerList);
  126.         $this->api $config['api'];
  127.         $this->signatureProvider $config['signature_provider'];
  128.         $this->endpoint = new Uri($config['endpoint']);
  129.         $this->credentialProvider $config['credentials'];
  130.         $this->region = isset($config['region']) ? $config['region'] : null;
  131.         $this->config $config['config'];
  132.         $this->defaultRequestOptions $config['http'];
  133.         $this->addSignatureMiddleware();
  134.         if (isset($args['with_resolved'])) {
  135.             $args['with_resolved']($config);
  136.         }
  137.     }
  138.     public function getHandlerList()
  139.     {
  140.         return $this->handlerList;
  141.     }
  142.     public function __call($name, array $args)
  143.     {
  144.         $params = isset($args[0]) ? $args[0] : [];
  145.         if (substr($name, -5) === 'Async') {
  146.             return $this->executeAsync(
  147.                 $this->getCommand(substr($name0, -5), $params)
  148.             );
  149.         }
  150.         return $this->execute($this->getCommand($name$params));
  151.     }
  152.     public function getConfig($option null)
  153.     {
  154.         return $option === null
  155.             $this->config
  156.             : (isset($this->config[$option])
  157.                 ? $this->config[$option]
  158.                 : null);
  159.     }
  160.     public function getCredentials()
  161.     {
  162.         $fn $this->credentialProvider;
  163.         return $fn();
  164.     }
  165.     public function getEndpoint()
  166.     {
  167.         return $this->endpoint;
  168.     }
  169.     public function getRegion()
  170.     {
  171.         return $this->region;
  172.     }
  173.     public function getApi()
  174.     {
  175.         return $this->api;
  176.     }
  177.     public function execute(CommandInterface $command)
  178.     {
  179.         return $this->executeAsync($command)->wait();
  180.     }
  181.     public function executeAsync(CommandInterface $command)
  182.     {
  183.         $handler $command->getHandlerList()->resolve();
  184.         return $handler($command);
  185.     }
  186.     public function getCommand($name, array $args = [])
  187.     {
  188.         // Fail fast if the command cannot be found in the description.
  189.         if (!isset($this->api['operations'][$name])) {
  190.             $name ucfirst($name);
  191.             if (!isset($this->api['operations'][$name])) {
  192.                 throw new \InvalidArgumentException("Operation not found: $name");
  193.             }
  194.         }
  195.         if (!isset($args['@http'])) {
  196.             $args['@http'] = $this->defaultRequestOptions;
  197.         } else {
  198.             $args['@http'] += $this->defaultRequestOptions;
  199.         }
  200.         return new Command($name$args, clone $this->getHandlerList());
  201.     }
  202.     public function getIterator($name, array $args = [])
  203.     {
  204.         $config $this->api->getPaginatorConfig($name);
  205.         if (!$config['result_key']) {
  206.             throw new \UnexpectedValueException(sprintf(
  207.                 'There are no resources to iterate for the %s operation of %s',
  208.                 $name$this->api['serviceFullName']
  209.             ));
  210.         }
  211.         $key is_array($config['result_key'])
  212.             ? $config['result_key'][0]
  213.             : $config['result_key'];
  214.         if ($config['output_token'] && $config['input_token']) {
  215.             return $this->getPaginator($name$args)->search($key);
  216.         }
  217.         $result $this->getCommand($name$args)->search($key);
  218.         return new \ArrayIterator((array) $result);
  219.     }
  220.     public function getPaginator($name, array $args = [])
  221.     {
  222.         $config $this->api->getPaginatorConfig($name);
  223.         return new ResultPaginator($this$name$args$config);
  224.     }
  225.     public function waitUntil($name, array $args = [])
  226.     {
  227.         return $this->getWaiter($name$args)->promise()->wait();
  228.     }
  229.     public function getWaiter($name, array $args = [])
  230.     {
  231.         $config = isset($args['@waiter']) ? $args['@waiter'] : [];
  232.         $config += $this->api->getWaiterConfig($name);
  233.         return new Waiter($this$name$args$config);
  234.     }
  235.     /**
  236.      * Get the signature_provider function of the client.
  237.      *
  238.      * @return callable
  239.      */
  240.     final protected function getSignatureProvider()
  241.     {
  242.         return $this->signatureProvider;
  243.     }
  244.     /**
  245.      * Parse the class name and setup the custom exception class of the client
  246.      * and return the "service" name of the client and "exception_class".
  247.      *
  248.      * @return array
  249.      */
  250.     private function parseClass()
  251.     {
  252.         $klass get_class($this);
  253.         if ($klass === __CLASS__) {
  254.             return ['''Aws\Exception\AwsException'];
  255.         }
  256.         $service substr($klassstrrpos($klass'\\') + 1, -6);
  257.         return [
  258.             strtolower($service),
  259.             "Aws\\{$service}\\Exception\\{$service}Exception"
  260.         ];
  261.     }
  262.     private function addSignatureMiddleware()
  263.     {
  264.         // Sign requests. This may need to be modified later to support
  265.         // variable signatures per/operation.
  266.         $this->handlerList->appendSign(
  267.             Middleware::signer(
  268.                 $this->credentialProvider,
  269.                 constantly(SignatureProvider::resolve(
  270.                     $this->signatureProvider,
  271.                     $this->config['signature_version'],
  272.                     $this->api->getSigningName(),
  273.                     $this->region
  274.                 ))
  275.             ),
  276.             'signer'
  277.         );
  278.     }
  279.     /**
  280.      * Returns a service model and doc model with any necessary changes
  281.      * applied.
  282.      *
  283.      * @param array $api  Array of service data being documented.
  284.      * @param array $docs Array of doc model data.
  285.      *
  286.      * @return array Tuple containing a [Service, DocModel]
  287.      *
  288.      * @internal This should only used to document the service API.
  289.      * @codeCoverageIgnore
  290.      */
  291.     public static function applyDocFilters(array $api, array $docs)
  292.     {
  293.         return [
  294.             new Service($apiApiProvider::defaultProvider()),
  295.             new DocModel($docs)
  296.         ];
  297.     }
  298.     /**
  299.      * @deprecated
  300.      * @return static
  301.      */
  302.     public static function factory(array $config = [])
  303.     {
  304.         return new static($config);
  305.     }
  306. }