Skip to content

Commit 4926302

Browse files
committed
Implement multiple providers
Fixes #47
1 parent 245ff56 commit 4926302

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

config/geocoder.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Ivory\HttpAdapter\CurlHttpAdapter;
44
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
5+
use Geocoder\Provider\Chain;
56
use Geocoder\Provider\BingMaps;
67
use Geocoder\Provider\FreeGeoIp;
78
use Geocoder\Provider\GoogleMaps;
@@ -20,17 +21,25 @@
2021
// Providers get called in the chain order given here.
2122
// The first one to return a result will be used.
2223
'providers' => [
24+
Chain::class => [
25+
GoogleMaps::class => [
26+
'de-DE',
27+
'Wien, Österreich',
28+
true,
29+
env('GOOGLE_MAPS_API_KEY'),
30+
],
31+
BingMaps::class => [
32+
'en-US',
33+
env('BING_MAPS_API_KEY'),
34+
],
35+
FreeGeoIp::class => [],
36+
],
2337
GoogleMaps::class => [
2438
'de-DE',
2539
'Wien, Österreich',
2640
true,
2741
env('GOOGLE_MAPS_API_KEY'),
2842
],
29-
BingMaps::class => [
30-
'en-US',
31-
env('BING_MAPS_API_KEY'),
32-
],
33-
FreeGeoIp::class => [],
3443
],
3544
// 'adapter' => CurlHttpAdapter::class,
3645
'adapter' => Guzzle6HttpAdapter::class,

src/GeocoderServiceProvider.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Geocoder\ProviderAggregator;
1515
use Geocoder\Provider\Chain;
16+
use Illuminate\Support\Collection;
1617
use Illuminate\Support\ServiceProvider;
1718
use ReflectionClass;
1819

@@ -50,26 +51,32 @@ public function register()
5051
return new $adapter;
5152
});
5253

53-
$this->app->singleton('geocoder.chain', function ($app) {
54-
$providers = collect(config('geocoder.providers'))
55-
->map(function ($arguments, $provider) {
56-
$arguments = $this->prepArguments($arguments, $provider);
57-
$reflection = new ReflectionClass($provider);
58-
59-
return $reflection->newInstanceArgs($arguments);
60-
});
61-
62-
return new Chain($providers->toArray());
63-
});
64-
6554
$this->app->singleton('geocoder', function ($app) {
6655
$geocoder = new ProviderAggregator();
67-
$geocoder->registerProvider($app['geocoder.chain']);
56+
$geocoder->registerProviders(
57+
$this->getProviders(collect(config('geocoder.providers')))
58+
);
6859

6960
return $geocoder;
7061
});
7162
}
7263

64+
private function getProviders(Collection $providers)
65+
{
66+
$providers = $providers->map(function ($arguments, $provider) {
67+
$arguments = $this->prepArguments($arguments, $provider);
68+
$reflection = new ReflectionClass($provider);
69+
70+
if ($provider === 'Geocoder\Provider\Chain') {
71+
return $reflection->newInstance($arguments);
72+
}
73+
74+
return $reflection->newInstanceArgs($arguments);
75+
});
76+
77+
return $providers->toArray();
78+
}
79+
7380
private function prepArguments(array $arguments, $provider)
7481
{
7582
$specificAdapter = $this->providerRequiresSpecificAdapter($provider);
@@ -81,11 +88,17 @@ private function prepArguments(array $arguments, $provider)
8188
}
8289

8390
if ($this->providerRequiresAdapter($provider)) {
84-
array_unshift($arguments, $this->app['geocoder.adapter']);
91+
array_unshift($arguments, app('geocoder.adapter'));
8592

8693
return $arguments;
8794
}
8895

96+
if ($provider === 'Geocoder\Provider\Chain') {
97+
return $this->getProviders(
98+
collect(config('geocoder.providers.Geocoder\Provider\Chain'))
99+
);
100+
}
101+
89102
return $arguments;
90103
}
91104

tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class GeocoderServiceProviderTest extends TestCase
77
public function testItResolvesAGivenAddress()
88
{
99
$result = app('geocoder')
10+
->using('chain')
1011
->geocode('1600 Pennsylvania Ave., Washington, DC USA')
1112
->all();
1213
$this->assertEquals('1600', $result[0]->getStreetNumber());
@@ -44,4 +45,17 @@ public function testItCanUseMaxMindBinaryWithoutProvider()
4445
$this->assertEquals('Washington', $result[0]->getLocality());
4546
$this->assertEquals('20003', $result[0]->getPostalCode());
4647
}
48+
49+
public function testItCanUseASpecificProvider()
50+
{
51+
// dd(config('geocoder.providers'));
52+
$result = app('geocoder')
53+
->using('google_maps')
54+
->geocode('1600 Pennsylvania Ave., Washington, DC USA')
55+
->all();
56+
$this->assertEquals('1600', $result[0]->getStreetNumber());
57+
$this->assertEquals('Pennsylvania Avenue Southeast', $result[0]->getStreetName());
58+
$this->assertEquals('Washington', $result[0]->getLocality());
59+
$this->assertEquals('20003', $result[0]->getPostalCode());
60+
}
4761
}

0 commit comments

Comments
 (0)