-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginProvider.php
147 lines (123 loc) · 4.04 KB
/
PluginProvider.php
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Plugin;
use Geocoder\Collection;
use Geocoder\Exception\Exception;
use Geocoder\Exception\LogicException;
use Geocoder\Plugin\Exception\LoopException;
use Geocoder\Plugin\Promise\GeocoderFulfilledPromise;
use Geocoder\Plugin\Promise\GeocoderRejectedPromise;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Geocoder\Query\ReverseQuery;
/**
* @author Joel Wurtz <joel.wurtz@gmail.com>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class PluginProvider implements Provider
{
/**
* @var Provider
*/
private $provider;
/**
* @var Plugin[]
*/
private $plugins;
/**
* A list of options.
*
* @var array{max_restarts?: int<0, max>}
*/
private $options;
/**
* @param Plugin[] $plugins
* @param array{max_restarts?: int<0, max>} $options
*/
public function __construct(Provider $provider, array $plugins = [], array $options = [])
{
$this->provider = $provider;
$this->plugins = $plugins;
$this->options = $this->configure($options);
}
public function geocodeQuery(GeocodeQuery $query): Collection
{
$pluginChain = $this->createPluginChain($this->plugins, function (GeocodeQuery $query) {
try {
return new GeocoderFulfilledPromise($this->provider->geocodeQuery($query));
} catch (Exception $exception) {
return new GeocoderRejectedPromise($exception);
}
});
return $pluginChain($query)->wait();
}
public function reverseQuery(ReverseQuery $query): Collection
{
$pluginChain = $this->createPluginChain($this->plugins, function (ReverseQuery $query) {
try {
return new GeocoderFulfilledPromise($this->provider->reverseQuery($query));
} catch (Exception $exception) {
return new GeocoderRejectedPromise($exception);
}
});
return $pluginChain($query)->wait();
}
public function getName(): string
{
return $this->provider->getName();
}
/**
* Configure the plugin provider.
*
* @param array{max_restarts?: int<0, max>} $options
*
* @return array{max_restarts: int<0, max>}
*/
private function configure(array $options = []): array
{
$defaults = [
'max_restarts' => 10,
];
$config = array_merge($defaults, $options);
// Make sure no invalid values are provided
if (count($config) !== count($defaults)) {
throw new LogicException(sprintf('Valid options to the PluginProviders are: %s', implode(', ', array_values($defaults))));
}
return $config;
}
/**
* Create the plugin chain.
*
* @param Plugin[] $pluginList A list of plugins
* @param callable $clientCallable Callable making the HTTP call
*
* @return callable
*/
private function createPluginChain(array $pluginList, callable $clientCallable)
{
$firstCallable = $lastCallable = $clientCallable;
while ($plugin = array_pop($pluginList)) {
$lastCallable = function (Query $query) use ($plugin, $lastCallable, &$firstCallable) {
return $plugin->handleQuery($query, $lastCallable, $firstCallable);
};
$firstCallable = $lastCallable;
}
$firstCalls = 0;
$firstCallable = function (Query $query) use ($lastCallable, &$firstCalls) {
if ($firstCalls > $this->options['max_restarts']) {
throw LoopException::create('Too many restarts in plugin provider', $query);
}
++$firstCalls;
return $lastCallable($query);
};
return $firstCallable;
}
}