1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace Aws;
- use Aws\Api\Service;
- use Psr\Http\Message\RequestInterface;
- use Psr\Log\InvalidArgumentException;
- class EndpointParameterMiddleware
- {
-
- private $nextHandler;
-
- private $service;
-
- public static function wrap(Service $service)
- {
- return function (callable $handler) use ($service) {
- return new self($handler, $service);
- };
- }
- public function __construct(callable $nextHandler, Service $service)
- {
- $this->nextHandler = $nextHandler;
- $this->service = $service;
- }
- public function __invoke(CommandInterface $command, RequestInterface $request)
- {
- $nextHandler = $this->nextHandler;
- $operation = $this->service->getOperation($command->getName());
- if (!empty($operation['endpoint']['hostPrefix'])) {
- $prefix = $operation['endpoint']['hostPrefix'];
-
-
- preg_match_all("/\{([a-zA-Z0-9]+)}/", $prefix, $parameters);
- if (!empty($parameters[1])) {
-
-
- foreach ($parameters[1] as $index => $parameter) {
- if (empty($command[$parameter])) {
- throw new \InvalidArgumentException(
- "The parameter '{$parameter}' must be set and not empty."
- );
- }
-
-
- $prefix = str_replace(
- $parameters[0][$index],
- $command[$parameter],
- $prefix
- );
- }
- }
- $uri = $request->getUri();
- $host = $prefix . $uri->getHost();
- if (!\Aws\is_valid_hostname($host)) {
- throw new \InvalidArgumentException(
- "The supplied parameters result in an invalid hostname: '{$host}'."
- );
- }
- $request = $request->withUri($uri->withHost($host));
- }
- return $nextHandler($command, $request);
- }
- }
|