Skip to content

Change from single provider to chain provider #8

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

Merged
merged 2 commits into from
May 12, 2014
Merged
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
9 changes: 6 additions & 3 deletions src/Toin0u/Geocoder/GeocoderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Toin0u\Geocoder;

use Geocoder\Geocoder;
use Geocoder\Provider\ChainProvider;
use Illuminate\Support\ServiceProvider;

/**
Expand Down Expand Up @@ -52,9 +53,11 @@ public function register()
});

$this->app->singleton('geocoder.provider', function($app) {
$provider = $app['config']->get('geocoder-laravel::provider');

return new $provider($app['geocoder.adapter']);
$providers = $app['config']->get('geocoder-laravel::providers');
foreach($providers as &$provider) {
$provider = new $provider($app['geocoder.adapter']);
}
return new ChainProvider($providers);
});

$this->app['geocoder'] = $this->app->share(function($app) {
Expand Down
5 changes: 4 additions & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*/

return array(
'provider' => 'Geocoder\Provider\FreeGeoIpProvider',
/* Providers get called in the chain order given here. The first one to return a result will be used. */
'providers' => array(
'Geocoder\Provider\FreeGeoIpProvider'
),
'adapter' => 'Geocoder\HttpAdapter\CurlHttpAdapter'
);
4 changes: 2 additions & 2 deletions tests/Geocoder/Tests/GeocoderFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GeocoderFacadeTest extends TestCase
public function testGeocoderFacade()
{
$this->assertTrue(is_array($providers = \Geocoder::getProviders()));
$this->assertArrayHasKey('free_geo_ip', $providers);
$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoipProvider', $providers['free_geo_ip']);
$this->assertArrayHasKey('chain', $providers);
$this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $providers['chain']);
}
}
21 changes: 19 additions & 2 deletions tests/Geocoder/Tests/GeocoderServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GeocoderServiceProviderTest extends TestCase
{
public function testConfig()
{
$this->assertSame('Geocoder\Provider\FreeGeoIpProvider', $this->app['config']->get('geocoder-laravel::provider'));
$this->assertContains('Geocoder\Provider\FreeGeoIpProvider', $this->app['config']->get('geocoder-laravel::providers'));
$this->assertSame('Geocoder\HttpAdapter\CurlHttpAdapter', $this->app['config']->get('geocoder-laravel::adapter'));
}

Expand All @@ -35,13 +35,30 @@ public function testGeocoderDefaultAdapter()
$this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $this->app['geocoder.adapter']);
}

public function testGeocoderChainProvider()
{
$this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $this->app['geocoder.provider']);
}

public function testGeocoderDefaultProvider()
{
$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIpProvider', $this->app['geocoder.provider']);
$providersArray = $this->getProtectedProperty($this->app['geocoder.provider'], 'providers');
$this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIpProvider', $providersArray[0]);

}

public function testGeocoder()
{
$this->assertInstanceOf('Geocoder\\Geocoder', $this->app['geocoder']);
}

protected function getProtectedProperty($testObj, $propertyName)
{
$reflection = new \ReflectionClass($testObj);
$property = $reflection->getProperty($propertyName);
$property->setAccessible(true);

return $property->getValue($testObj);
}

}