summaryrefslogtreecommitdiffstats
path: root/apps/files_external/3rdparty/aws-sdk-php/Aws/Common/RulesEndpointProvider.php
blob: ec57cb862cad9f6323e1a4542da35d5f790458b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Aws\Common;

/**
 * Provides endpoints based on a rules configuration file.
 */
class RulesEndpointProvider
{
    /** @var array */
    private $patterns;

    /**
     * @param array $patterns Hash of endpoint patterns mapping to endpoint
     *                        configurations.
     */
    public function __construct(array $patterns)
    {
        $this->patterns = $patterns;
    }

    /**
     * Creates and returns the default RulesEndpointProvider based on the
     * public rule sets.
     *
     * @return self
     */
    public static function fromDefaults()
    {
        return new self(require __DIR__ . '/Resources/public-endpoints.php');
    }

    public function __invoke(array $args = array())
    {
        if (!isset($args['service'])) {
            throw new \InvalidArgumentException('Requires a "service" value');
        }

        if (!isset($args['region'])) {
            throw new \InvalidArgumentException('Requires a "region" value');
        }

        foreach ($this->getKeys($args['region'], $args['service']) as $key) {
            if (isset($this->patterns['endpoints'][$key])) {
                return $this->expand($this->patterns['endpoints'][$key], $args);
            }
        }

        throw new \RuntimeException('Could not resolve endpoint');
    }

    private function expand(array $config, array $args)
    {
        $scheme = isset($args['scheme']) ? $args['scheme'] : 'https';
        $config['endpoint'] = $scheme . '://' . str_replace(
            array('{service}', '{region}'),
            array($args['service'], $args['region']),
            $config['endpoint']
        );

        return $config;
    }

    private function getKeys($region, $service)
    {
        return array("$region/$service", "$region/*", "*/$service", "*/*");
    }
}