Skip to content

Commit d8273fa

Browse files
committed
add support of providers arguments + tests + doc - fix #5
1 parent 9c56371 commit d8273fa

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,27 @@ $ php artisan config:publish toin0u/geocoder-laravel
7373
The service provider creates the following services:
7474

7575
* `geocoder`: the Geocoder instance.
76-
* `geocoder.provider`: the provider used by Geocoder.
76+
* `geocoder.chain`: the chain provider used by Geocoder.
7777
* `geocoder.adapter`: the HTTP adapter used to get data from remotes APIs.
7878

79-
By default, the `geocoder.provider` service uses FreeGeoIP and the `geocoder.adapter` service uses the cURL adapter.
80-
Override these services to use the adapter/provider you want by editing
81-
`app/config/packages/toin0u/geocoder-laravel/config.php`:
79+
By default, the `geocoder.chain` service contains `GoogleMapsProvider` and `FreeGeoIpProvider`.
80+
The `geocoder.adapter` service uses the cURL adapter. Override these services to use the
81+
adapter/providers you want by editing `app/config/packages/toin0u/geocoder-laravel/config.php`:
8282

8383
```php
8484
return array(
85-
'provider' => 'Geocoder\Provider\GoogleMapsProvider',
85+
'providers' => array(
86+
'Geocoder\Provider\GoogleMapsProvider' => array('my-locale', 'my-region', $ssl = true),
87+
'Geocoder\Provider\CloudMadeProvider' => array('my-api-key'),
88+
'Geocoder\Provider\FreeGeoIpProvider' => null, // or array()
89+
// ...
90+
),
8691
'adapter' => 'Geocoder\HttpAdapter\CurlHttpAdapter'
8792
);
8893
```
8994

95+
NB: As you can see the array value of the provider is the constructor arguments.
96+
9097
See [the Geocoder documentation](http://geocoder-php.org/Geocoder/) for a list of available adapters and providers.
9198

9299

src/Toin0u/Geocoder/GeocoderServiceProvider.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,25 @@ public function register()
5252
return new $adapter;
5353
});
5454

55-
$this->app->singleton('geocoder.provider', function($app) {
56-
$providers = $app['config']->get('geocoder-laravel::providers');
55+
$this->app->singleton('geocoder.chain', function($app) {
56+
$providers = array();
5757

58-
foreach($providers as &$provider) {
59-
$provider = new $provider($app['geocoder.adapter']);
58+
foreach($app['config']->get('geocoder-laravel::providers') as $provider => $arguments) {
59+
if (0 !== count($arguments)) {
60+
$providers[] = new $provider($app['geocoder.adapter'], implode(',', $arguments));
61+
62+
continue;
63+
}
64+
65+
$providers[] = new $provider($app['geocoder.adapter']);
6066
}
6167

6268
return new ChainProvider($providers);
6369
});
6470

6571
$this->app['geocoder'] = $this->app->share(function($app) {
6672
$geocoder = new Geocoder;
67-
$geocoder->registerProvider($app['geocoder.provider']);
73+
$geocoder->registerProvider($app['geocoder.chain']);
6874

6975
return $geocoder;
7076
});
@@ -77,6 +83,6 @@ public function register()
7783
*/
7884
public function provides()
7985
{
80-
return array('geocoder', 'geocoder.adapter', 'geocoder.provider');
86+
return array('geocoder', 'geocoder.adapter', 'geocoder.chain');
8187
}
8288
}

src/config/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// Providers get called in the chain order given here.
1414
// The first one to return a result will be used.
1515
'providers' => array(
16-
'Geocoder\Provider\GoogleMapsProvider',
17-
'Geocoder\Provider\FreeGeoIpProvider',
16+
'Geocoder\Provider\GoogleMapsProvider' => array('fr-FR', 'Île-de-France', true),
17+
'Geocoder\Provider\FreeGeoIpProvider' => null,
1818
),
1919
'adapter' => 'Geocoder\HttpAdapter\CurlHttpAdapter'
2020
);

tests/Geocoder/Tests/GeocoderServiceProviderTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class GeocoderServiceProviderTest extends TestCase
1919
public function testConfig()
2020
{
2121
$this->assertTrue(is_array($providers = $this->app['config']->get('geocoder-laravel::providers')));
22-
$this->assertContains('Geocoder\\Provider\\GoogleMapsProvider', $providers);
23-
$this->assertContains('Geocoder\\Provider\\FreeGeoIpProvider', $providers);
22+
$this->assertCount(2, $providers);
23+
$this->assertArrayHasKey('Geocoder\\Provider\\GoogleMapsProvider', $providers);
24+
$this->assertArrayHasKey('Geocoder\\Provider\\FreeGeoIpProvider', $providers);
2425
$this->assertSame('Geocoder\\HttpAdapter\\CurlHttpAdapter', $this->app['config']->get('geocoder-laravel::adapter'));
2526
}
2627

@@ -39,14 +40,15 @@ public function testGeocoderDefaultAdapter()
3940

4041
public function testGeocoderChainProvider()
4142
{
42-
$this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $this->app['geocoder.provider']);
43+
$this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $this->app['geocoder.chain']);
4344
}
4445

4546
public function testGeocoderDefaultProvider()
4647
{
47-
$providers = $this->getProtectedProperty($this->app['geocoder.provider'], 'providers');
48+
$providers = $this->getProtectedProperty($this->app['geocoder.chain'], 'providers');
4849

4950
$this->assertInstanceOf('Geocoder\\Provider\\GoogleMapsProvider', $providers[0]);
51+
$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIpProvider', $providers[1]);
5052
}
5153

5254
public function testGeocoder()

0 commit comments

Comments
 (0)