| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Aws\Api\Service; |
| 5 |
use Psr\Http\Message\RequestInterface; |
| 6 |
use Psr\Log\InvalidArgumentException; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class EndpointParameterMiddleware |
| 16 |
{ |
| 17 |
|
| 18 |
private $nextHandler; |
| 19 |
|
| 20 |
|
| 21 |
private $service; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public static function wrap(Service $service) |
| 31 |
{ |
| 32 |
return function (callable $handler) use ($service) { |
| 33 |
return new self($handler, $service); |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
public function __construct(callable $nextHandler, Service $service) |
| 38 |
{ |
| 39 |
$this->nextHandler = $nextHandler; |
| 40 |
$this->service = $service; |
| 41 |
} |
| 42 |
|
| 43 |
public function __invoke(CommandInterface $command, RequestInterface $request) |
| 44 |
{ |
| 45 |
$nextHandler = $this->nextHandler; |
| 46 |
|
| 47 |
$operation = $this->service->getOperation($command->getName()); |
| 48 |
|
| 49 |
if (!empty($operation['endpoint']['hostPrefix'])) { |
| 50 |
$prefix = $operation['endpoint']['hostPrefix']; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
preg_match_all("/\{([a-zA-Z0-9]+)}/", $prefix, $parameters); |
| 55 |
|
| 56 |
if (!empty($parameters[1])) { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
foreach ($parameters[1] as $index => $parameter) { |
| 61 |
if (empty($command[$parameter])) { |
| 62 |
throw new \InvalidArgumentException( |
| 63 |
"The parameter '{$parameter}' must be set and not empty." |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
$prefix = str_replace( |
| 70 |
$parameters[0][$index], |
| 71 |
$command[$parameter], |
| 72 |
$prefix |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
$uri = $request->getUri(); |
| 78 |
$host = $prefix . $uri->getHost(); |
| 79 |
if (!\Aws\is_valid_hostname($host)) { |
| 80 |
throw new \InvalidArgumentException( |
| 81 |
"The supplied parameters result in an invalid hostname: '{$host}'." |
| 82 |
); |
| 83 |
} |
| 84 |
$request = $request->withUri($uri->withHost($host)); |
| 85 |
} |
| 86 |
|
| 87 |
return $nextHandler($command, $request); |
| 88 |
} |
| 89 |
} |