Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Used to update the host based on a modeled endpoint trait
10
 *
11
 * IMPORTANT: this middleware must be added after the "build" step.
12
 *
13
 * @internal
14
 */
15
class EndpointParameterMiddleware
16
{
17
    /** @var callable */
18
    private $nextHandler;
19
 
20
    /** @var Service */
21
    private $service;
22
 
23
    /**
24
     * Create a middleware wrapper function
25
     *
26
     * @param Service $service
27
     * @param array $args
28
     * @return \Closure
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
            // Captures endpoint parameters stored in the modeled host.
53
            // These are denoted by enclosure in braces, i.e. '{param}'
54
            preg_match_all("/\{([a-zA-Z0-9]+)}/", $prefix, $parameters);
55
 
56
            if (!empty($parameters[1])) {
57
 
58
                // Captured parameters without braces stored in $parameters[1],
59
                // which should correspond to members in the Command object
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
                    // Captured parameters with braces stored in $parameters[0],
68
                    // which are replaced by their corresponding Command value
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
}