20
20
use Geocoder \Laravel \Exceptions \InvalidDumperException ;
21
21
use Geocoder \ProviderAggregator ;
22
22
use Illuminate \Support \Collection ;
23
+ use ReflectionClass ;
23
24
24
25
/**
25
26
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
26
27
*/
27
28
class ProviderAndDumperAggregator
28
29
{
29
30
protected $ aggregator ;
31
+ protected $ limit ;
30
32
protected $ results ;
31
33
32
- public function __construct (int $ limit = Geocoder:: DEFAULT_RESULT_LIMIT )
34
+ public function __construct (int $ limit = null )
33
35
{
34
36
$ this ->aggregator = new ProviderAggregator ($ limit );
37
+ $ this ->limit = $ limit ;
35
38
$ this ->results = collect ();
36
39
}
37
40
@@ -78,7 +81,7 @@ public function dump(string $dumper) : Collection
78
81
public function geocodeQuery (GeocodeQuery $ query ) : self
79
82
{
80
83
$ cacheKey = serialize ($ query );
81
- $ this ->results = cache ( )->remember (
84
+ $ this ->results = app ( ' cache ' )->remember (
82
85
"geocoder- {$ cacheKey }" ,
83
86
config ('geocoder.cache-duraction ' , 0 ),
84
87
function () use ($ query ) {
@@ -92,7 +95,7 @@ function () use ($query) {
92
95
public function reverseQuery (ReverseQuery $ query ) : self
93
96
{
94
97
$ cacheKey = serialize ($ query );
95
- $ this ->results = cache ( )->remember (
98
+ $ this ->results = app ( ' cache ' )->remember (
96
99
"geocoder- {$ cacheKey }" ,
97
100
config ('geocoder.cache-duraction ' , 0 ),
98
101
function () use ($ query ) {
@@ -111,7 +114,7 @@ public function getName() : string
111
114
public function geocode (string $ value ) : self
112
115
{
113
116
$ cacheKey = str_slug (strtolower (urlencode ($ value )));
114
- $ this ->results = cache ( )->remember (
117
+ $ this ->results = app ( ' cache ' )->remember (
115
118
"geocoder- {$ cacheKey }" ,
116
119
config ('geocoder.cache-duraction ' , 0 ),
117
120
function () use ($ value ) {
@@ -125,7 +128,7 @@ function () use ($value) {
125
128
public function reverse (float $ latitude , float $ longitude ) : self
126
129
{
127
130
$ cacheKey = str_slug (strtolower (urlencode ("{$ latitude }- {$ longitude }" )));
128
- $ this ->results = cache ( )->remember (
131
+ $ this ->results = app ( ' cache ' )->remember (
129
132
"geocoder- {$ cacheKey }" ,
130
133
config ('geocoder.cache-duraction ' , 0 ),
131
134
function () use ($ latitude , $ longitude ) {
@@ -138,14 +141,16 @@ function () use ($latitude, $longitude) {
138
141
139
142
public function limit (int $ limit ) : self
140
143
{
141
- $ this ->aggregator ->limit ($ limit );
144
+ $ this ->aggregator = new ProviderAggregator (null , $ limit );
145
+ $ this ->registerProvidersFromConfig (collect (config ('geocoder.providers ' )));
146
+ $ this ->limit = $ limit ;
142
147
143
148
return $ this ;
144
149
}
145
150
146
151
public function getLimit () : int
147
152
{
148
- return $ this ->aggregator -> getLimit () ;
153
+ return $ this ->limit ;
149
154
}
150
155
151
156
public function registerProvider ($ provider ) : self
@@ -164,7 +169,7 @@ public function registerProviders(array $providers = []) : self
164
169
165
170
public function using (string $ name ) : self
166
171
{
167
- $ this ->aggregator ->using ($ name );
172
+ $ this ->aggregator = $ this -> aggregator ->using ($ name );
168
173
169
174
return $ this ;
170
175
}
@@ -178,4 +183,58 @@ protected function getProvider()
178
183
{
179
184
return $ this ->aggregator ->getProvider ();
180
185
}
186
+
187
+ public function registerProvidersFromConfig (Collection $ providers ) : self
188
+ {
189
+ $ this ->registerProviders ($ this ->getProvidersFromConfiguration ($ providers ));
190
+
191
+ return $ this ;
192
+ }
193
+
194
+ protected function getProvidersFromConfiguration (Collection $ providers ) : array
195
+ {
196
+ $ providers = $ providers ->map (function ($ arguments , $ provider ) {
197
+ $ arguments = $ this ->getArguments ($ arguments , $ provider );
198
+ $ reflection = new ReflectionClass ($ provider );
199
+
200
+ if ($ provider === 'Geocoder\Provider\Chain\Chain ' ) {
201
+ return $ reflection ->newInstance ($ arguments );
202
+ }
203
+
204
+ return $ reflection ->newInstanceArgs ($ arguments );
205
+ });
206
+
207
+ return $ providers ->toArray ();
208
+ }
209
+
210
+ protected function getArguments (array $ arguments , string $ provider ) : array
211
+ {
212
+ if ($ provider === 'Geocoder\Provider\Chain\Chain ' ) {
213
+ return $ this ->getProvidersFromConfiguration (
214
+ collect (config ('geocoder.providers.Geocoder\Provider\Chain\Chain ' ))
215
+ );
216
+ }
217
+
218
+ $ adapter = $ this ->getAdapterClass ($ provider );
219
+
220
+ if ($ adapter ) {
221
+ array_unshift ($ arguments , (new $ adapter ));
222
+ }
223
+
224
+ return $ arguments ;
225
+ }
226
+
227
+ protected function getAdapterClass (string $ provider ) : string
228
+ {
229
+ $ specificAdapters = collect ([
230
+ 'Geocoder\Provider\GeoIP2 ' => 'Geocoder\Adapter\GeoIP2Adapter ' ,
231
+ 'Geocoder\Provider\MaxMindBinary ' => null ,
232
+ ]);
233
+
234
+ if ($ specificAdapters ->has ($ provider )) {
235
+ return $ specificAdapters ->get ($ provider );
236
+ }
237
+
238
+ return config ('geocoder.adapter ' );
239
+ }
181
240
}
0 commit comments