GetBucketLocationParser.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\Api\Parser\AbstractParser;
  4. use Aws\Api\StructureShape;
  5. use Aws\CommandInterface;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Http\Message\StreamInterface;
  8. /**
  9. * @internal Decorates a parser for the S3 service to correctly handle the
  10. * GetBucketLocation operation.
  11. */
  12. class GetBucketLocationParser extends AbstractParser
  13. {
  14. /**
  15. * @param callable $parser Parser to wrap.
  16. */
  17. public function __construct(callable $parser)
  18. {
  19. $this->parser = $parser;
  20. }
  21. public function __invoke(
  22. CommandInterface $command,
  23. ResponseInterface $response
  24. ) {
  25. $fn = $this->parser;
  26. $result = $fn($command, $response);
  27. if ($command->getName() === 'GetBucketLocation') {
  28. $location = 'us-east-1';
  29. if (preg_match('/>(.+?)<\/LocationConstraint>/', $response->getBody(), $matches)) {
  30. $location = $matches[1] === 'EU' ? 'eu-west-1' : $matches[1];
  31. }
  32. $result['LocationConstraint'] = $location;
  33. }
  34. return $result;
  35. }
  36. public function parseMemberFromStream(
  37. StreamInterface $stream,
  38. StructureShape $member,
  39. $response
  40. ) {
  41. return $this->parser->parseMemberFromStream($stream, $member, $response);
  42. }
  43. }