Skip to content

Implement Httplug #512

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 16 commits into from
Aug 16, 2016
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
composer.phar
phpunit.xml
php-cs-fixer.phar
.puli/
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ env:
- deps=""

php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand All @@ -21,7 +20,7 @@ php:
matrix:
fast_finish: true
include:
- php: 5.4
- php: 5.5
env: deps="low"

before_script:
Expand All @@ -31,4 +30,4 @@ before_script:
- if [ "$deps" = "" ]; then composer install --prefer-dist --no-interaction; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then composer require "geoip/geoip"; fi

script: phpunit --coverage-text
script: ./vendor/bin/phpunit --coverage-text
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ since each HTTP-based provider implements
[PSR-7](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md).

```php
$curl = new \Ivory\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Provider\GoogleMaps($curl);
$adapter = new \Http\Adapter\Guzzle6\Client();
$geocoder = new \Geocoder\Provider\GoogleMaps($adapter);

$geocoder->geocode(...);
$geocoder->reverse(...);
Expand Down Expand Up @@ -214,14 +214,14 @@ In order to talk to geocoding APIs, you need HTTP adapters. While it was part of
the library in Geocoder 1.x and 2.x, Geocoder 3.x and upper now relies on the
[PSR-7
Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md)
which defines how HTTP message should be implemented. Choose any library that
follows this PSR and implement the specified interfaces to use with Geocoder.
which defines how HTTP message should be implemented. You can use any library to send HTTP messages
that implements [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation).

As making choices is rather hard, Geocoder ships with the
[egeloen/http-adapter](https://github.com/egeloen/ivory-http-adapter) library by
default, but it is up to you to choose a different implementation.
To use Guzzle 6 you should run the follwing command:

**Note:** not all providers are HTTP-based.
```
$ composer require php-http/guzzle6-adapter
```

### Providers

Expand Down Expand Up @@ -360,7 +360,7 @@ when a provider returns a result. The result is returned by `GoogleMaps` because

``` php
$geocoder = new \Geocoder\ProviderAggregator();
$adapter = new \Ivory\HttpAdapter\CurlHttpAdapter();
$adapter = new \Http\Adapter\Guzzle6\Client();

$chain = new \Geocoder\Provider\Chain([
new \Geocoder\Provider\FreeGeoIp($adapter),
Expand Down
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
}
],
"require": {
"php": "^5.4|^7.0",
"egeloen/http-adapter": "~0.8|~1.0",
"igorw/get-in": "~1.0"
"php": "^5.5 || ^7.0",
"igorw/get-in": "^1.0",
"psr/http-message-implementation": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/httplug": "^1.0",
"php-http/discovery": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not define a bin directory as /bin and avoid executing /vendor/bin/phpunit?

"geoip2/geoip2": "~2.0",
"symfony/stopwatch": "~2.5"
"symfony/stopwatch": "~2.5",
"php-http/message": "^1.0",
"php-http/guzzle6-adapter": "^1.0",
"php-http/mock-client": "^0.3.0"
},
"suggest": {
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
Expand Down
64 changes: 55 additions & 9 deletions src/Geocoder/Provider/AbstractHttpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,81 @@

namespace Geocoder\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Message\MessageFactory;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Client\HttpClient;

/**
* @author William Durand <[email protected]>
*/
class AbstractHttpProvider extends AbstractProvider
{
/**
* @var HttpAdapterInterface
* @var HttpClient
*/
private $adapter;
private $client;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @var MessageFactory
*/
public function __construct(HttpAdapterInterface $adapter)
private $messageFactory;

/**
* @param HttpClient $client
* @param MessageFactory|null $factory
*/
public function __construct(HttpClient $client, MessageFactory $factory = null)
{
parent::__construct();

$this->adapter = $adapter;
$this->client = $client;
$this->messageFactory = $factory;
}

/**
* Returns the HTTP adapter.
*
* @return HttpAdapterInterface
* @return HttpClient
*/
protected function getHttpClient()
{
return $this->client;
}

/**
* @return MessageFactory
*/
public function getAdapter()
protected function getMessageFactory()
{
return $this->adapter;
if ($this->messageFactory === null) {
$this->messageFactory = MessageFactoryDiscovery::find();
}

return $this->messageFactory;
}

/**
* @param HttpClient $client
*
* @return AbstractHttpProvider
*/
public function setClient(HttpClient $client)
{
$this->client = $client;

return $this;
}

/**
* @param MessageFactory $messageFactory
*
* @return AbstractHttpProvider
*/
public function setMessageFactory(MessageFactory $messageFactory)
{
$this->messageFactory = $messageFactory;

return $this;
}
}
17 changes: 9 additions & 8 deletions src/Geocoder/Provider/ArcGISOnline.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;

/**
* @author ALKOUM Dorian <[email protected]>
Expand Down Expand Up @@ -40,13 +40,13 @@ class ArcGISOnline extends AbstractHttpProvider implements Provider
private $protocol;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $sourceCountry Country biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param HttpClient $client An HTTP adapter
* @param string $sourceCountry Country biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $sourceCountry = null, $useSsl = false)
public function __construct(HttpClient $client, $sourceCountry = null, $useSsl = false)
{
parent::__construct($adapter);
parent::__construct($client);

$this->sourceCountry = $sourceCountry;
$this->protocol = $useSsl ? 'https' : 'http';
Expand Down Expand Up @@ -167,8 +167,9 @@ private function buildQuery($query)
*/
private function executeQuery($query)
{
$query = $this->buildQuery($query);
$content = (string) $this->getAdapter()->get($query)->getBody();
$query = $this->buildQuery($query);
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
Expand Down
15 changes: 8 additions & 7 deletions src/Geocoder/Provider/BingMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;

/**
* @author David Guyon <[email protected]>
Expand All @@ -38,13 +38,13 @@ class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
private $apiKey;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $apiKey An API key
* @param string $locale A locale (optional)
* @param HttpClient $client An HTTP adapter
* @param string $apiKey An API key
* @param string $locale A locale (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null)
public function __construct(HttpClient $client, $apiKey, $locale = null)
{
parent::__construct($adapter);
parent::__construct($client);

$this->apiKey = $apiKey;
$this->locale = $locale;
Expand Down Expand Up @@ -100,7 +100,8 @@ private function executeQuery($query)
$query = sprintf('%s&culture=%s', $query, str_replace('_', '-', $this->getLocale()));
}

$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
Expand Down
3 changes: 2 additions & 1 deletion src/Geocoder/Provider/FreeGeoIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public function getName()
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

if (empty($content)) {
throw new NoResult(sprintf('Could not execute query %s', $query));
Expand Down
13 changes: 7 additions & 6 deletions src/Geocoder/Provider/GeoIPs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;

/**
* @author Andrea Cristaudo <[email protected]>
Expand Down Expand Up @@ -50,12 +50,12 @@ class GeoIPs extends AbstractHttpProvider implements Provider
private $apiKey;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $apiKey An API key
* @param HttpClient $client An HTTP adapter
* @param string $apiKey An API key
*/
public function __construct(HttpAdapterInterface $adapter, $apiKey)
public function __construct(HttpClient $client, $apiKey)
{
parent::__construct($adapter);
parent::__construct($client);

$this->apiKey = $apiKey;
}
Expand Down Expand Up @@ -107,7 +107,8 @@ public function getName()
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

if (empty($content)) {
throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query));
Expand Down
3 changes: 2 additions & 1 deletion src/Geocoder/Provider/GeoPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function getName()
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
Expand Down
15 changes: 8 additions & 7 deletions src/Geocoder/Provider/Geonames.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\AdminLevelCollection;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;

/**
* @author Giovanni Pirrotta <[email protected]>
Expand All @@ -39,13 +39,13 @@ class Geonames extends AbstractHttpProvider implements LocaleAwareProvider
private $username;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $username Username login (Free registration at http://www.geonames.org/login)
* @param string $locale A locale (optional)
* @param HttpClient $client An HTTP adapter
* @param string $username Username login (Free registration at http://www.geonames.org/login)
* @param string $locale A locale (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $username, $locale = null)
public function __construct(HttpClient $client, $username, $locale = null)
{
parent::__construct($adapter);
parent::__construct($client);

$this->username = $username;
$this->locale = $locale;
Expand Down Expand Up @@ -102,7 +102,8 @@ private function executeQuery($query)
$query = sprintf('%s&lang=%s', $query, substr($this->getLocale(), 0, 2));
}

$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
Expand Down
19 changes: 10 additions & 9 deletions src/Geocoder/Provider/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;

/**
* @author William Durand <[email protected]>
Expand Down Expand Up @@ -50,15 +50,15 @@ class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider
private $apiKey;

/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $locale A locale (optional)
* @param string $region Region biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param string $apiKey Google Geocoding API key (optional)
* @param HttpClient $client An HTTP adapter
* @param string $locale A locale (optional)
* @param string $region Region biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param string $apiKey Google Geocoding API key (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $locale = null, $region = null, $useSsl = false, $apiKey = null)
public function __construct(HttpClient $client, $locale = null, $region = null, $useSsl = false, $apiKey = null)
{
parent::__construct($adapter);
parent::__construct($client);

$this->locale = $locale;
$this->region = $region;
Expand Down Expand Up @@ -136,7 +136,8 @@ protected function buildQuery($query)
private function executeQuery($query)
{
$query = $this->buildQuery($query);
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();

// Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider
if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) {
Expand Down
Loading