From cbf795667613ea4206fab54a867686f1b1813843 Mon Sep 17 00:00:00 2001 From: Anaxamaxan Date: Fri, 31 Jan 2014 12:23:40 -0800 Subject: [PATCH 1/2] Changed to use ChainProvider instead of limiting to only a single provider. --- src/Toin0u/Geocoder/GeocoderServiceProvider.php | 9 ++++++--- src/config/config.php | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Toin0u/Geocoder/GeocoderServiceProvider.php b/src/Toin0u/Geocoder/GeocoderServiceProvider.php index 36457a7..0186f4d 100644 --- a/src/Toin0u/Geocoder/GeocoderServiceProvider.php +++ b/src/Toin0u/Geocoder/GeocoderServiceProvider.php @@ -12,6 +12,7 @@ namespace Toin0u\Geocoder; use Geocoder\Geocoder; +use Geocoder\Provider\ChainProvider; use Illuminate\Support\ServiceProvider; /** @@ -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) { diff --git a/src/config/config.php b/src/config/config.php index 98441ae..ac13892 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -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' ); From 16c2492f1ac3de346664fa115b27b057702d049d Mon Sep 17 00:00:00 2001 From: Anaxamaxan Date: Fri, 31 Jan 2014 12:57:16 -0800 Subject: [PATCH 2/2] Add tests for using chain provider instead of single provider --- tests/Geocoder/Tests/GeocoderFacadeTest.php | 4 ++-- .../Tests/GeocoderServiceProviderTest.php | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/tests/Geocoder/Tests/GeocoderFacadeTest.php b/tests/Geocoder/Tests/GeocoderFacadeTest.php index faf414a..ee0c673 100644 --- a/tests/Geocoder/Tests/GeocoderFacadeTest.php +++ b/tests/Geocoder/Tests/GeocoderFacadeTest.php @@ -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']); } } diff --git a/tests/Geocoder/Tests/GeocoderServiceProviderTest.php b/tests/Geocoder/Tests/GeocoderServiceProviderTest.php index b7cde81..fa6b136 100644 --- a/tests/Geocoder/Tests/GeocoderServiceProviderTest.php +++ b/tests/Geocoder/Tests/GeocoderServiceProviderTest.php @@ -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')); } @@ -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); + } + }