public function __invoke(CommandInterface $command, RequestInterface $request) { if (!$this->queue) { $last = $this->lastCommand ? ' The last command sent was ' . $this->lastCommand->getName() . '.' : ''; throw new \RuntimeException('Mock queue is empty. Trying to send a ' . $command->getName() . ' command failed.' . $last); } $this->lastCommand = $command; $this->lastRequest = $request; $result = array_shift($this->queue); if (is_callable($result)) { $result = $result($command, $request); } if ($result instanceof \Exception) { $result = new RejectedPromise($result); } else { // Add an effective URI and statusCode if not present. $meta = $result['@metadata']; if (!isset($meta['effectiveUri'])) { $meta['effectiveUri'] = (string) $request->getUri(); } if (!isset($meta['statusCode'])) { $meta['statusCode'] = 200; } $result['@metadata'] = $meta; $result = Promise\promise_for($result); } $result->then($this->onFulfilled, $this->onRejected); return $result; }
/** * Calls the simpler HTTP specific handler and wraps the returned promise * with AWS specific values (e.g., a result object or AWS exception). * * @param CommandInterface $command Command being executed. * @param RequestInterface $request Request to send. * * @return Promise\PromiseInterface */ public function __invoke(CommandInterface $command, RequestInterface $request) { $fn = $this->httpHandler; return Promise\promise_for($fn($request, $command['@http'] ?: []))->then(function (ResponseInterface $res) use($command, $request) { return $this->parseResponse($command, $request, $res); }, function ($err) use($request, $command) { if (is_array($err)) { $exception = $this->parseError($err, $request, $command); return new Promise\RejectedPromise($exception); } return new Promise\RejectedPromise($err); }); }
/** * This function creates an AWS credential provider * * @return callable */ private function getCredentials() { return function () { $config = $this->getConfig(); $key = $config["securefiles_s3_key"]; $secret = $config["securefiles_s3_secret"]; if ($key && $secret) { return Promise\promise_for(new Credentials($key, $secret)); } $msg = 'Could not retrieve credentials'; return new RejectedPromise(new CredentialsException($msg)); }; }
/** * Runs a paginator asynchronously and uses a callback to handle results. * * The callback should have the signature: function (Aws\Result $result). * A non-null return value from the callback will be yielded by the * promise. This means that you can return promises from the callback that * will need to be resolved before continuing iteration over the remaining * items, essentially merging in other promises to the iteration. The last * non-null value returned by the callback will be the result that fulfills * the promise to any downstream promises. * * @param callable $handleResult Callback for handling each page of results. * The callback accepts the result that was * yielded as a single argument. If the * callback returns a promise, the promise * will be merged into the coroutine. * * @return Promise\Promise */ public function each(callable $handleResult) { return Promise\coroutine(function () use($handleResult) { $nextToken = null; do { $command = $this->createNextCommand($this->args, $nextToken); $result = (yield $this->client->executeAsync($command)); $nextToken = $this->determineNextToken($result); $retVal = $handleResult($result); if ($retVal !== null) { (yield Promise\promise_for($retVal)); } } while ($nextToken); }); }
/** * Calls the simpler HTTP specific handler and wraps the returned promise * with AWS specific values (e.g., a result object or AWS exception). * * @param CommandInterface $command Command being executed. * @param RequestInterface $request Request to send. * * @return Promise\PromiseInterface */ public function __invoke(CommandInterface $command, RequestInterface $request) { $fn = $this->httpHandler; $options = $command['@http'] ?: []; $stats = []; if ($this->collectStats) { $options['http_stats_receiver'] = static function (array $transferStats) use(&$stats) { $stats = $transferStats; }; } elseif (isset($options['http_stats_receiver'])) { throw new \InvalidArgumentException('Providing a custom HTTP stats' . ' receiver to Aws\\WrappedHttpHandler is not supported.'); } return Promise\promise_for($fn($request, $options))->then(function (ResponseInterface $res) use($command, $request, &$stats) { return $this->parseResponse($command, $request, $res, $stats); }, function ($err) use($request, $command, &$stats) { if (is_array($err)) { $err = $this->parseError($err, $request, $command, $stats); } return new Promise\RejectedPromise($err); }); }
/** * Returns a promise that will clean up any references when it completes. * * @return PromiseInterface */ private function createPromise() { // Create the promise $promise = call_user_func($this->promiseCreator, $this); $this->promiseCreator = null; // Cleans up the promise state and references. $cleanup = function () { $this->before = $this->client = $this->queue = null; }; // When done, ensure cleanup and that any remaining are processed. return $promise->then(function () use($cleanup) { return Promise\promise_for($this->flushQueue())->then($cleanup); }, function ($reason) use($cleanup) { $cleanup(); return Promise\rejection_for($reason); }); }
public function determineBucketRegionAsync($bucketName) { if ($cached = $this->cache->get($this->getCacheKey($bucketName))) { return Promise\promise_for($cached); } return $this->lookupBucketRegion($bucketName)->then(function ($region) use($bucketName) { $this->cache->set($this->getCacheKey($bucketName), $region); return $region; }); }
/** * Transfers the given request and applies request options. * * The URI of the request is not modified and the request options are used * as-is without merging in default options. * * @param RequestInterface $request * @param array $options * * @return Promise\PromiseInterface */ private function transfer(RequestInterface $request, array $options) { // save_to -> sink if (isset($options['save_to'])) { $options['sink'] = $options['save_to']; unset($options['save_to']); } // exceptions -> http_errors if (isset($options['exceptions'])) { $options['http_errors'] = $options['exceptions']; unset($options['exceptions']); } $request = $this->applyOptions($request, $options); $handler = $options['handler']; try { return Promise\promise_for($handler($request, $options)); } catch (\Exception $e) { return Promise\rejection_for($e); } }
/** * Credentials provider that creates credentials using an ini file stored * in the current user's home directory. * * @param string|null $profile Profile to use. If not specified will use * the "default" profile. * @param string|null $filename If provided, uses a custom filename rather * than looking in the home directory for the * * @return callable */ public static function ini($profile = null, $filename = null) { $filename = $filename ?: self::getHomeDir() . '/.aws/credentials'; $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); return function () use($profile, $filename) { if (!is_readable($filename)) { return self::reject("Cannot read credentials from {$filename}"); } $data = parse_ini_file($filename, true); if ($data === false) { return self::reject("Invalid credentials file: {$filename}"); } if (!isset($data[$profile])) { return self::reject("'{$profile}' not found in credentials file"); } if (!isset($data[$profile]['aws_access_key_id']) || !isset($data[$profile]['aws_secret_access_key'])) { return self::reject("No credentials present in INI profile " . "'{$profile}' ({$filename})"); } return Promise\promise_for(new Credentials($data[$profile]['aws_access_key_id'], $data[$profile]['aws_secret_access_key'], isset($data[$profile]['aws_security_token']) ? $data[$profile]['aws_security_token'] : null)); }; }
/** * todo ini 文件加载 * * @param null $profile * @param null $filename * * @return \Closure */ public static function ini($profile = null, $filename = null) { $filename = $filename ?: './.cloudatlas/credentials'; $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); $credentialConcrete = static::getCredentialConcrete(); return function () use($profile, $filename, $credentialConcrete) { if (!is_readable($filename)) { return self::reject("Cannot read credentials from {$filename}"); } $data = parse_ini_file($filename, true); if ($data === false) { return self::reject("Invalid credentials file: {$filename}"); } if (!isset($data[$profile])) { return self::reject("'{$profile}' not found in credentials file."); } if (!isset($data[$profile][constant(static::ENV_KEY)]) || !isset($data[$profile][constant(static::ENV_SECRET)])) { return self::reject("No credentials present in INI profile " . "'{$profile}' ({$filename})"); } return Promise\promise_for(new $credentialConcrete($data[$profile][constant(static::ENV_KEY)], $data[$profile][constant(static::ENV_SECRET)])); }; }