Skip to content

Commit 0415baa

Browse files
committed
WIP - starting work on Geocoder 4.0 compatibility
1 parent 5081daa commit 0415baa

File tree

7 files changed

+213
-64
lines changed

7 files changed

+213
-64
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [1.1.0] - 17 Jun 2017
6+
### Added
7+
- caching functionality for `geocode()` and `reverse()` methods.
8+
- `cache-duration` variable to geocoder config.
9+
510
## [1.0.2] - 20 Mar 2017
611
### Added
712
- unit test for reverse-geocoding.

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ If you are upgrading from a pre-1.x version of this package, please keep the
5353
```php
5454
use Geocoder\Laravel\Facades\Geocoder;
5555
```
56-
56+
5757
6. Update your query statements to use `->get()` (to retrieve a collection of
5858
GeoCoder objects) or `->all()` (to retrieve an array of arrays), then iterate
5959
to process each result.
@@ -71,6 +71,11 @@ See the [Geocoder documentation](http://geocoder-php.org/Geocoder/) for a list
7171
of available adapters and providers.
7272

7373
### Default Settings
74+
If you are upgrading and do not update your config file with the `cache-duraction`
75+
variable, cache will by default be disabled (it will have a `0` cache duration).
76+
The default cache duration provided by the config file is `999999999` minutes,
77+
essentially forever.
78+
7479
By default, the configuration specifies a Chain Provider as the first provider,
7580
containing GoogleMaps and FreeGeoIp providers. The first to return a result
7681
will be returned. After the Chain Provider, we have added the BingMaps provider
@@ -80,6 +85,7 @@ By default, the configuration specifies a Chain Provider as the first provider,
8085
there just to illustrate this point (and is used by the PHPUnit tests).
8186
```php
8287
return [
88+
'cache-duraction' => 999999999,
8389
'providers' => [
8490
Chain::class => [
8591
GoogleMaps::class => [

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
],
2525
"require": {
2626
"php": ">=5.4",
27+
"geocoder-php/common-http": "dev-master@dev",
28+
"geocoder-php/google-maps-provider": "dev-master@dev",
29+
"guzzlehttp/guzzle": "^6.2",
2730
"illuminate/cache": "~5.0",
2831
"illuminate/support": "~5.0",
29-
"willdurand/geocoder": "~3.3"
32+
"php-http/guzzle6-adapter": "^1.1",
33+
"php-http/message": "^1.5",
34+
"willdurand/geocoder": "dev-master@dev"
3035
},
3136
"require-dev": {
3237
"fzaninotto/faker": "~1.4",
@@ -56,7 +61,11 @@
5661
},
5762
"extra": {
5863
"branch-alias": {
59-
"dev-master": "1.0.0-dev"
64+
"dev-master": "1.2.0-dev"
6065
}
66+
},
67+
"config": {
68+
"preferred-install": "dist",
69+
"sort-packages": true
6170
}
6271
}

config/geocoder.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,35 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Ivory\HttpAdapter\CurlHttpAdapter;
13-
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
14-
use Geocoder\Provider\Chain;
15-
use Geocoder\Provider\BingMaps;
16-
use Geocoder\Provider\FreeGeoIp;
17-
use Geocoder\Provider\GoogleMaps;
18-
use Geocoder\Provider\MaxMindBinary;
12+
use Http\Adapter\Guzzle6\Client;
13+
// use Geocoder\Provider\Chain;
14+
// use Geocoder\Provider\BingMaps;
15+
// use Geocoder\Provider\FreeGeoIp;
16+
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress;
17+
// use Geocoder\Provider\MaxMindBinary;
1918

2019
return [
2120
'cache-duraction' => 999999999,
2221
'providers' => [
23-
Chain::class => [
24-
GoogleMaps::class => [
25-
'en',
26-
'us',
27-
true,
28-
env('GOOGLE_MAPS_API_KEY'),
29-
],
30-
FreeGeoIp::class => [],
31-
],
32-
BingMaps::class => [
33-
'en-US',
34-
env('BING_MAPS_API_KEY'),
35-
],
36-
GoogleMaps::class => [
22+
// Chain::class => [
23+
// GoogleMaps::class => [
24+
// 'en',
25+
// 'us',
26+
// true,
27+
// env('GOOGLE_MAPS_API_KEY'),
28+
// ],
29+
// FreeGeoIp::class => [],
30+
// ],
31+
// BingMaps::class => [
32+
// 'en-US',
33+
// env('BING_MAPS_API_KEY'),
34+
// ],
35+
GoogleAddress::class => [
3736
'en',
3837
'us',
3938
true,
4039
env('GOOGLE_MAPS_API_KEY'),
4140
],
4241
],
43-
'adapter' => CurlHttpAdapter::class,
42+
'adapter' => Client::class,
4443
];
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php namespace Geocoder\Laravel;
2+
3+
/**
4+
* This file is part of the GeocoderLaravel library.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
use Geocoder\ProviderAggregator;
10+
11+
/**
12+
* @author Mike Bronner <[email protected]>
13+
*/
14+
class GeocoderProviderAggregatorAdapter
15+
{
16+
protected $aggregator;
17+
18+
public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
19+
{
20+
$this->aggregator = new ProviderAggregator($limit);
21+
}
22+
23+
public function geocodeQuery($query)
24+
{
25+
return $this->aggregator->geocodeQuery($query);
26+
}
27+
28+
public function reverseQuery($query)
29+
{
30+
return $this->aggregator->reverseQuery($query);
31+
}
32+
33+
public function getName()
34+
{
35+
return $this->aggregator->getName();
36+
}
37+
38+
public function geocode($value)
39+
{
40+
return $this->aggregator->geocode($value);
41+
}
42+
43+
public function reverse(float $latitude, float $longitude)
44+
{
45+
return $this->aggregator->reverse($latitude, $longitude);
46+
}
47+
48+
public function limit($limit)
49+
{
50+
$this->aggregator->limit($limit);
51+
52+
return $this;
53+
}
54+
55+
public function getLimit()
56+
{
57+
return $this->aggregator->getLimit();
58+
}
59+
60+
public function registerProvider($provider)
61+
{
62+
$this->aggregator->registerProvider($provider);
63+
64+
return $this;
65+
}
66+
67+
public function registerProviders($providers = [])
68+
{
69+
$this->aggregator->registerProviders($providers);
70+
71+
return $this;
72+
}
73+
74+
public function using($name)
75+
{
76+
$this->aggregator->using($name);
77+
78+
return $this;
79+
}
80+
81+
public function getProviders()
82+
{
83+
return $this->aggregator->getProviders();
84+
}
85+
86+
protected function getProvider()
87+
{
88+
return $this->aggregator->getProvider();
89+
}
90+
}

src/ProviderAndDumperAggregator.php

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,35 @@
1515
use Geocoder\Dumper\Wkt;
1616
use Geocoder\Geocoder;
1717
use Geocoder\Laravel\Exceptions\InvalidDumperException;
18+
use Geocoder\Laravel\ProviderAndDumperAggregator;
1819
use Geocoder\ProviderAggregator;
20+
use Geocoder\Model\AddressCollection;
1921
use Illuminate\Support\Collection;
2022

2123
/**
2224
* @author Mike Bronner <[email protected]>
2325
*/
24-
class ProviderAndDumperAggregator extends ProviderAggregator implements Geocoder
26+
class ProviderAndDumperAggregator
2527
{
26-
/**
27-
* @var \Geocoder\Model\AddressCollection
28-
*/
2928
protected $results;
29+
protected $aggregator;
3030

31-
/**
32-
* @return array
33-
*/
34-
public function all()
31+
public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT)
32+
{
33+
$this->aggregator = new ProviderAggregator($limit);
34+
}
35+
36+
public function all() : array
3537
{
3638
return $this->results->all();
3739
}
3840

39-
/**
40-
* @param $dumper
41-
* @return Collection
42-
* @throws InvalidDumperException
43-
*/
44-
public function dump($dumper)
41+
public function get() : AddressCollection
42+
{
43+
return $this->results;
44+
}
45+
46+
public function dump($dumper) : Collection
4547
{
4648
$dumperClasses = collect([
4749
'geojson' => GeoJson::class,
@@ -68,48 +70,89 @@ public function dump($dumper)
6870
});
6971
}
7072

71-
/**
72-
* @param string
73-
* @return ProviderAndDumperAggregator
74-
*/
75-
public function geocode($value)
73+
public function geocodeQuery($query)
74+
{
75+
return $this->aggregator->geocodeQuery($query);
76+
}
77+
78+
public function reverseQuery($query)
79+
{
80+
return $this->aggregator->reverseQuery($query);
81+
}
82+
83+
public function getName()
84+
{
85+
return $this->aggregator->getName();
86+
}
87+
88+
public function geocode(string $value) : self
7689
{
7790
$cacheId = str_slug($value);
7891
$this->results = cache()->remember(
7992
"geocoder-{$cacheId}",
8093
config('geocoder.cache-duraction', 0),
8194
function () use ($value) {
82-
return parent::geocode($value);
95+
return $this->aggregator->geocode($value);
8396
}
8497
);
8598

8699
return $this;
87100
}
88101

89-
/**
90-
* @return \Geocoder\Model\AddressCollection
91-
*/
92-
public function get()
93-
{
94-
return $this->results;
95-
}
96-
97-
/**
98-
* @param float
99-
* @param float
100-
* @return ProviderAndDumperAggregator
101-
*/
102-
public function reverse($latitude, $longitude)
102+
public function reverse(float $latitude, float $longitude) : self
103103
{
104104
$cacheId = str_slug("{$latitude}-{$longitude}");
105105
$this->results = cache()->remember(
106106
"geocoder-{$cacheId}",
107107
config('geocoder.cache-duraction', 0),
108108
function () use ($latitude, $longitude) {
109-
return parent::reverse($latitude, $longitude);
109+
return $this->aggregator->reverse($latitude, $longitude);
110110
}
111111
);
112112

113113
return $this;
114114
}
115+
116+
public function limit($limit)
117+
{
118+
$this->aggregator->limit($limit);
119+
120+
return $this;
121+
}
122+
123+
public function getLimit()
124+
{
125+
return $this->aggregator->getLimit();
126+
}
127+
128+
public function registerProvider($provider)
129+
{
130+
$this->aggregator->registerProvider($provider);
131+
132+
return $this;
133+
}
134+
135+
public function registerProviders($providers = [])
136+
{
137+
$this->aggregator->registerProviders($providers);
138+
139+
return $this;
140+
}
141+
142+
public function using($name)
143+
{
144+
$this->aggregator->using($name);
145+
146+
return $this;
147+
}
148+
149+
public function getProviders()
150+
{
151+
return $this->aggregator->getProviders();
152+
}
153+
154+
protected function getProvider()
155+
{
156+
return $this->aggregator->getProvider();
157+
}
115158
}

src/Providers/GeocoderService.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,9 @@ public function register()
4848
{
4949
$this->app->alias('Geocoder', Geocoder::class);
5050
$this->app->singleton('geocoder', function () {
51-
$geocoder = new ProviderAndDumperAggregator();
52-
$geocoder->registerProviders(
51+
return (new ProviderAndDumperAggregator)->registerProviders(
5352
$this->getProviders(collect(config('geocoder.providers')))
5453
);
55-
56-
return $geocoder;
5754
});
5855
}
5956

0 commit comments

Comments
 (0)