-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented compatibility with Geocoder 4.0
- Loading branch information
1 parent
e35aebf
commit b38a86d
Showing
7 changed files
with
179 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,40 +2,32 @@ | |
|
||
/** | ||
* This file is part of the GeocoderLaravel library. | ||
* | ||
* (c) Antoine Corcy <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Http\Adapter\Guzzle6\Client; | ||
// use Geocoder\Provider\Chain; | ||
// use Geocoder\Provider\BingMaps; | ||
// use Geocoder\Provider\FreeGeoIp; | ||
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress; | ||
// use Geocoder\Provider\MaxMindBinary; | ||
use Geocoder\Provider\BingMaps\BingMaps; | ||
use Geocoder\Provider\Chain\Chain; | ||
use Geocoder\Provider\FreeGeoIp\FreeGeoIp; | ||
use Geocoder\Provider\GoogleMaps\GoogleMaps; | ||
|
||
return [ | ||
'cache-duraction' => 999999999, | ||
'providers' => [ | ||
// Chain::class => [ | ||
// GoogleMaps::class => [ | ||
// 'en', | ||
// 'us', | ||
// true, | ||
// env('GOOGLE_MAPS_API_KEY'), | ||
// ], | ||
// FreeGeoIp::class => [], | ||
// ], | ||
// BingMaps::class => [ | ||
// 'en-US', | ||
// env('BING_MAPS_API_KEY'), | ||
// ], | ||
GoogleAddress::class => [ | ||
'en', | ||
Chain::class => [ | ||
GoogleMaps::class => [ | ||
'en-US', | ||
env('GOOGLE_MAPS_API_KEY'), | ||
], | ||
FreeGeoIp::class => [], | ||
], | ||
BingMaps::class => [ | ||
'en-US', | ||
env('BING_MAPS_API_KEY'), | ||
], | ||
GoogleMaps::class => [ | ||
'us', | ||
true, | ||
env('GOOGLE_MAPS_API_KEY'), | ||
], | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
<?php namespace Geocoder\Laravel; | ||
|
||
/** | ||
* This file is part of the Geocoder package. | ||
* This file is part of the Geocoder Laravel package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Mike Bronner <[email protected]> | ||
* @license MIT License | ||
*/ | ||
|
||
|
@@ -14,36 +15,37 @@ | |
use Geocoder\Dumper\Wkb; | ||
use Geocoder\Dumper\Wkt; | ||
use Geocoder\Geocoder; | ||
use Geocoder\Query\GeocodeQuery; | ||
use Geocoder\Query\ReverseQuery; | ||
use Geocoder\Laravel\Exceptions\InvalidDumperException; | ||
use Geocoder\Laravel\ProviderAndDumperAggregator; | ||
use Geocoder\ProviderAggregator; | ||
use Geocoder\Model\AddressCollection; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* @author Mike Bronner <[email protected]> | ||
*/ | ||
class ProviderAndDumperAggregator | ||
{ | ||
protected $results; | ||
protected $aggregator; | ||
protected $results; | ||
|
||
public function __construct(int $limit = Geocoder::DEFAULT_RESULT_LIMIT) | ||
{ | ||
$this->aggregator = new ProviderAggregator($limit); | ||
$this->results = collect(); | ||
} | ||
|
||
/** | ||
* @deprecated Use `get()` instead. | ||
*/ | ||
public function all() : array | ||
{ | ||
return $this->results->all(); | ||
} | ||
|
||
public function get() : AddressCollection | ||
public function get() : Collection | ||
{ | ||
return $this->results; | ||
} | ||
|
||
public function dump($dumper) : Collection | ||
public function dump(string $dumper) : Collection | ||
{ | ||
$dumperClasses = collect([ | ||
'geojson' => GeoJson::class, | ||
|
@@ -70,29 +72,69 @@ public function dump($dumper) : Collection | |
}); | ||
} | ||
|
||
public function geocodeQuery($query) | ||
public function geocodeQuery(GeocodeQuery $query) : self | ||
{ | ||
return $this->aggregator->geocodeQuery($query); | ||
$cacheKey = serialize($query); | ||
$this->results = cache()->remember( | ||
"geocoder-{$cacheKey}", | ||
config('geocoder.cache-duraction', 0), | ||
function () use ($query) { | ||
$addresses = collect(); | ||
$addressCollection = $this->aggregator->geocodeQuery($query); | ||
|
||
foreach ($addressCollection as $address) { | ||
$addresses->push($address); | ||
} | ||
|
||
return $addresses; | ||
} | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function reverseQuery($query) | ||
public function reverseQuery(ReverseQuery $query) : self | ||
{ | ||
return $this->aggregator->reverseQuery($query); | ||
$cacheKey = serialize($query); | ||
$this->results = cache()->remember( | ||
"geocoder-{$cacheKey}", | ||
config('geocoder.cache-duraction', 0), | ||
function () use ($query) { | ||
$addresses = collect(); | ||
$addressCollection = $this->aggregator->reverseQuery($query); | ||
|
||
foreach ($addressCollection as $address) { | ||
$addresses->push($address); | ||
} | ||
|
||
return $addresses; | ||
} | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function getName() | ||
public function getName() : string | ||
{ | ||
return $this->aggregator->getName(); | ||
} | ||
|
||
public function geocode(string $value) : self | ||
{ | ||
$cacheId = str_slug($value); | ||
$cacheKey = str_slug($value); | ||
$this->results = cache()->remember( | ||
"geocoder-{$cacheId}", | ||
"geocoder-{$cacheKey}", | ||
config('geocoder.cache-duraction', 0), | ||
function () use ($value) { | ||
return $this->aggregator->geocode($value); | ||
|
||
$addresses = collect(); | ||
$addressCollection = $this->aggregator->geocode($value); | ||
|
||
foreach ($addressCollection as $address) { | ||
$addresses->push($address); | ||
} | ||
|
||
return $addresses; | ||
} | ||
); | ||
|
||
|
@@ -106,47 +148,54 @@ public function reverse(float $latitude, float $longitude) : self | |
"geocoder-{$cacheId}", | ||
config('geocoder.cache-duraction', 0), | ||
function () use ($latitude, $longitude) { | ||
return $this->aggregator->reverse($latitude, $longitude); | ||
$addresses = collect(); | ||
$addressCollection = $this->aggregator->reverse($latitude, $longitude); | ||
|
||
foreach ($addressCollection as $address) { | ||
$addresses->push($address); | ||
} | ||
|
||
return $addresses; | ||
} | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function limit($limit) | ||
public function limit(int $limit) : self | ||
{ | ||
$this->aggregator->limit($limit); | ||
|
||
return $this; | ||
} | ||
|
||
public function getLimit() | ||
public function getLimit() : int | ||
{ | ||
return $this->aggregator->getLimit(); | ||
} | ||
|
||
public function registerProvider($provider) | ||
public function registerProvider(string $provider) : self | ||
{ | ||
$this->aggregator->registerProvider($provider); | ||
|
||
return $this; | ||
} | ||
|
||
public function registerProviders($providers = []) | ||
public function registerProviders(array $providers = []) : self | ||
{ | ||
$this->aggregator->registerProviders($providers); | ||
|
||
return $this; | ||
} | ||
|
||
public function using($name) | ||
public function using(string $name) : self | ||
{ | ||
$this->aggregator->using($name); | ||
|
||
return $this; | ||
} | ||
|
||
public function getProviders() | ||
public function getProviders() : array | ||
{ | ||
return $this->aggregator->getProviders(); | ||
} | ||
|
Oops, something went wrong.
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to consider using
php-http/curl-client
instead ofphp-http/guzzle6-adapter
. It is faster.b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, was going off the examples in the readme so far. Will switch over.
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not forget to use a psr7 implementation. Consider guzzle, zend or slim. They are auto discovered.
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you point me to the relevant documentation in geocoder, and what parts of the code here you think it should be added to? By default I'm passing most things through to the Geocoder library. Would it not be handled there?
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Geocoder itself is HTTP independent. It does not care what HTTP client or PSR7 implementation you are using. We let the user to decide that when installing the package. I see that you do not want to give the user the same flexibility. (Or maybe Im mistaken?)
We use HTTPlug to get this abstraction. Read about it here: http://docs.php-http.org/en/latest/httplug/users.html
I describe this in the docs: https://github.com/geocoder-php/Geocoder#installation
So ideally, you would remove php-http/message, curl-client, guzzle6-adapter and guzzle/psr7 and let the user choose what implementation they want.
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes, they can choose whatever they want ... its just that the default configuration comes with the guzzle adaptor right now, or curl adapter once I update it. But they are free to use any adaptor they want according to the Geocoder documentation. :)
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to read up more on the auto-discovery of the adapter. Can you point me to the code where that is happening? That probably should be implemented, having a bit of a hard time figuring that out. Thanks for bearing with me!
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason they are there now is so that composer can install all the dependencies. Once 4.0 is live I will remove them from composer.json.
Or do I misunderstand? Should I be only using those in my tests? They aren't actually used in the code, other than tests and config file. (I was going off this: geocoder-php/Geocoder#684 to get it working.)
b38a86d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that is right, We do not auto discover clients, only message factory:
https://github.com/geocoder-php/Geocoder/blob/master/src/Http/Provider/AbstractHttpProvider.php#L96
In your composer.json require:
No implementation of HTTP clients, PSR7 or php-http/message.
In your composer.json require-dev:
You should have a HTTP client, PSR7 and http-php/message because you want to be able to run the tests. Which PSR7 and HTTP client you choose does not matter.
Since we require virtual packages a user cannot install willdurand/geocoder without having a client installed. That is why we should suggest a command in the readme which includes a client, psr7 implementation and php-http/message.
When 4.0 is released I will update the readme to say:
This is unrelated. He was downloading 3.3 and read the docs for master. I moved that class.