Skip to content

Update to Geocoder v3 and register named providers #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Geocoder for Lavarel 5
======================

If you still use **Lavarel 4**, please check out the `0.4.x` branch [here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x).
If you still use **Lavarel 4**, please check out the `0.4.x` branch [here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x)..

This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/)
in [**Laravel 5**](http://laravel.com/).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require" : {
"php" : ">=5.4",
"illuminate/support" : "~5.0",
"willdurand/geocoder" : "~2.4"
"willdurand/geocoder" : "~3.0"
},

"require-dev" : {
Expand Down
12 changes: 9 additions & 3 deletions config/geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
// Providers get called in the chain order given here.
// The first one to return a result will be used.
'providers' => [
'Geocoder\Provider\GoogleMapsProvider' => ['fr-FR', 'Île-de-France', true],
'Geocoder\Provider\FreeGeoIpProvider' => null,
// Named Providers
'Geocoder\Provider\GoogleMaps' => ['fr-FR', 'Île-de-France', true],
'Geocoder\Provider\FreeGeoIp' => null,
// Chain Provider
[
'Geocoder\Provider\GoogleMaps' => ['fr-FR', 'Île-de-France', true],
'Geocoder\Provider\FreeGeoIp' => null,
]
],
'adapter' => 'Geocoder\HttpAdapter\CurlHttpAdapter',
'adapter' => 'Ivory\HttpAdapter\CurlHttpAdapter',
];
61 changes: 35 additions & 26 deletions src/GeocoderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Toin0u\Geocoder;

use Geocoder\Geocoder;
use Geocoder\Provider\ChainProvider;
use Geocoder\ProviderAggregator;
use Geocoder\Provider\Chain;

/**
* Geocoder service provider
Expand Down Expand Up @@ -48,30 +48,11 @@ public function register()
return new $adapter;
});

$this->app->singleton('geocoder.chain', function($app) {
$providers = [];

foreach($app['config']->get('geocoder.providers') as $provider => $arguments) {
if (0 !== count($arguments)) {
$providers[] = call_user_func_array(
function ($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null) use ($app, $provider) {
return new $provider($app['geocoder.adapter'], $arg1, $arg2, $arg3, $arg4);
},
$arguments
);

continue;
}

$providers[] = new $provider($app['geocoder.adapter']);
}

return new ChainProvider($providers);
});

$this->app['geocoder'] = $this->app->share(function($app) {
$geocoder = new Geocoder;
$geocoder->registerProvider($app['geocoder.chain']);
$geocoder = new ProviderAggregator;
$geocoder->registerProviders(
$this->getGeocoderProviders($this->app['config']->get('geocoder.providers'))
);

return $geocoder;
});
Expand All @@ -84,6 +65,34 @@ function ($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null) use ($app, $pr
*/
public function provides()
{
return ['geocoder', 'geocoder.adapter', 'geocoder.chain'];
return ['geocoder', 'geocoder.adapter'];
}

protected function getGeocoderProviders(array $providersConfig)
{
$providers = [];

foreach($providersConfig as $provider => $arguments) {
//Chain provider
if(is_int($provider)){
$chainProviders = $this->getGeocoderProviders($arguments);
$providers[] = new Chain($chainProviders);
}else {
if (0 !== count($arguments)) {
$providers[] = call_user_func_array(
function ($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null) use ($provider) {
return new $provider($this->app['geocoder.adapter'], $arg1, $arg2, $arg3, $arg4);
},
$arguments
);

continue;
}

$providers[] = new $provider($this->app['geocoder.adapter']);
}
}

return $providers;
}
}
2 changes: 1 addition & 1 deletion tests/Facade/GeocoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function testGeocoderFacade()
{
$this->assertTrue(is_array($providers = \Geocoder::getProviders()));
$this->assertArrayHasKey('chain', $providers);
$this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $providers['chain']);
$this->assertInstanceOf('Geocoder\\Provider\\Chain', $providers['chain']);
}
}
41 changes: 27 additions & 14 deletions tests/GeocoderServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class GeocoderServiceProviderTest extends TestCase
public function testConfig()
{
$this->assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers')));
$this->assertCount(2, $providers);
$this->assertArrayHasKey('Geocoder\\Provider\\GoogleMapsProvider', $providers);
$this->assertArrayHasKey('Geocoder\\Provider\\FreeGeoIpProvider', $providers);
$this->assertSame('Geocoder\\HttpAdapter\\CurlHttpAdapter', $this->app['config']->get('geocoder.adapter'));
$this->assertCount(3, $providers);
$this->assertArrayHasKey('Geocoder\\Provider\\GoogleMaps', $providers);
$this->assertArrayHasKey('Geocoder\\Provider\\FreeGeoIp', $providers);
$this->assertSame('Ivory\\HttpAdapter\\CurlHttpAdapter', $this->app['config']->get('geocoder.adapter'));
}

public function testLoadedProviders()
Expand All @@ -35,25 +35,38 @@ public function testLoadedProviders()

public function testGeocoderDefaultAdapter()
{
$this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $this->app['geocoder.adapter']);
$this->assertInstanceOf('Ivory\\HttpAdapter\\CurlHttpAdapter', $this->app['geocoder.adapter']);
}

public function testGeocoderChainProvider()
{
$this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $this->app['geocoder.chain']);
$providers = $this->getProtectedProperty($this->app['geocoder'], 'providers');

$this->assertArrayHasKey('chain', $providers);

$this->assertInstanceOf('Geocoder\\Provider\\Chain', $providers['chain']);

$chainProviders = $this->getProtectedProperty($providers['chain'], 'providers');

$this->assertInstanceOf('Geocoder\\Provider\\GoogleMaps', $chainProviders[0]);
$this->assertSame('fr-FR', $chainProviders[0]->getLocale());
$this->assertInstanceOf('Ivory\\HttpAdapter\\CurlHttpAdapter', $chainProviders[0]->getAdapter());

$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIp', $chainProviders[1]);
$this->assertInstanceOf('Ivory\\HttpAdapter\\CurlHttpAdapter', $chainProviders[1]->getAdapter());

}

public function testGeocoderDefaultProvider()
public function testGeocoderNamedProviders()
{
$providers = $this->getProtectedProperty($this->app['geocoder.chain'], 'providers');
$providers = $this->getProtectedProperty($this->app['geocoder'], 'providers');

$this->assertInstanceOf('Geocoder\\Provider\\GoogleMapsProvider', $providers[0]);
$this->assertSame('fr-FR', $providers[0]->getLocale());
$this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $providers[0]->getAdapter());
$this->assertInstanceOf('Geocoder\\Provider\\GoogleMaps', $providers['google_maps']);
$this->assertSame('fr-FR', $providers['google_maps']->getLocale());
$this->assertInstanceOf('Ivory\\HttpAdapter\\CurlHttpAdapter', $providers['google_maps']->getAdapter());

$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIpProvider', $providers[1]);
$this->assertNull($providers[1]->getLocale());
$this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $providers[1]->getAdapter());
$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIp', $providers['free_geo_ip']);
$this->assertInstanceOf('Ivory\\HttpAdapter\\CurlHttpAdapter', $providers['free_geo_ip']->getAdapter());
}

public function testGeocoder()
Expand Down