diff --git a/.gitignore b/.gitignore index edc927f26..5d5fa0e4a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock composer.phar phpunit.xml php-cs-fixer.phar +.puli/ diff --git a/.travis.yml b/.travis.yml index c4cd39b92..1c6b940b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,6 @@ env: - deps="" php: - - 5.4 - 5.5 - 5.6 - 7.0 @@ -21,7 +20,7 @@ php: matrix: fast_finish: true include: - - php: 5.4 + - php: 5.5 env: deps="low" before_script: @@ -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 diff --git a/README.md b/README.md index 5fb058598..0ad4e240b 100644 --- a/README.md +++ b/README.md @@ -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(...); @@ -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 @@ -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), diff --git a/composer.json b/composer.json index 874f346b1..8122ff7f7 100644 --- a/composer.json +++ b/composer.json @@ -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", "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.", diff --git a/src/Geocoder/Provider/AbstractHttpProvider.php b/src/Geocoder/Provider/AbstractHttpProvider.php index e391981b7..38f9e4cb1 100644 --- a/src/Geocoder/Provider/AbstractHttpProvider.php +++ b/src/Geocoder/Provider/AbstractHttpProvider.php @@ -10,7 +10,10 @@ 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 @@ -18,27 +21,70 @@ 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; } } diff --git a/src/Geocoder/Provider/ArcGISOnline.php b/src/Geocoder/Provider/ArcGISOnline.php index da3994c98..1a1378abc 100644 --- a/src/Geocoder/Provider/ArcGISOnline.php +++ b/src/Geocoder/Provider/ArcGISOnline.php @@ -12,7 +12,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author ALKOUM Dorian @@ -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'; @@ -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)); diff --git a/src/Geocoder/Provider/BingMaps.php b/src/Geocoder/Provider/BingMaps.php index 296755ead..38896617a 100644 --- a/src/Geocoder/Provider/BingMaps.php +++ b/src/Geocoder/Provider/BingMaps.php @@ -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 @@ -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; @@ -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)); diff --git a/src/Geocoder/Provider/FreeGeoIp.php b/src/Geocoder/Provider/FreeGeoIp.php index 9f1aeb555..6ccb0e99c 100644 --- a/src/Geocoder/Provider/FreeGeoIp.php +++ b/src/Geocoder/Provider/FreeGeoIp.php @@ -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)); diff --git a/src/Geocoder/Provider/GeoIPs.php b/src/Geocoder/Provider/GeoIPs.php index 438775555..7eac8963c 100644 --- a/src/Geocoder/Provider/GeoIPs.php +++ b/src/Geocoder/Provider/GeoIPs.php @@ -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 @@ -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; } @@ -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)); diff --git a/src/Geocoder/Provider/GeoPlugin.php b/src/Geocoder/Provider/GeoPlugin.php index efe2172d0..2330c1e25 100644 --- a/src/Geocoder/Provider/GeoPlugin.php +++ b/src/Geocoder/Provider/GeoPlugin.php @@ -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)); diff --git a/src/Geocoder/Provider/Geonames.php b/src/Geocoder/Provider/Geonames.php index c7fe3425f..45b537fe9 100644 --- a/src/Geocoder/Provider/Geonames.php +++ b/src/Geocoder/Provider/Geonames.php @@ -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 @@ -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; @@ -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)); diff --git a/src/Geocoder/Provider/GoogleMaps.php b/src/Geocoder/Provider/GoogleMaps.php index 031dceb76..977dea858 100644 --- a/src/Geocoder/Provider/GoogleMaps.php +++ b/src/Geocoder/Provider/GoogleMaps.php @@ -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 @@ -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; @@ -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) { diff --git a/src/Geocoder/Provider/GoogleMapsBusiness.php b/src/Geocoder/Provider/GoogleMapsBusiness.php index 889c58cf2..3cb97891b 100644 --- a/src/Geocoder/Provider/GoogleMapsBusiness.php +++ b/src/Geocoder/Provider/GoogleMapsBusiness.php @@ -10,7 +10,7 @@ namespace Geocoder\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * Google Maps for Business @@ -31,16 +31,16 @@ class GoogleMapsBusiness extends GoogleMaps implements Provider private $privateKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $clientId Your Client ID. - * @param string $privateKey Your Private Key (optional). - * @param string $locale A locale (optional). - * @param string $region Region biasing (optional). - * @param bool $useSsl Whether to use an SSL connection (optional) + * @param HttpClient $client An HTTP adapter. + * @param string $clientId Your Client ID. + * @param string $privateKey Your Private Key (optional). + * @param string $locale A locale (optional). + * @param string $region Region biasing (optional). + * @param bool $useSsl Whether to use an SSL connection (optional) */ - public function __construct(HttpAdapterInterface $adapter, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false) + public function __construct(HttpClient $client, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false) { - parent::__construct($adapter, $locale, $region, $useSsl); + parent::__construct($client, $locale, $region, $useSsl); $this->clientId = $clientId; $this->privateKey = $privateKey; diff --git a/src/Geocoder/Provider/HostIp.php b/src/Geocoder/Provider/HostIp.php index d3c258af1..be9c31726 100644 --- a/src/Geocoder/Provider/HostIp.php +++ b/src/Geocoder/Provider/HostIp.php @@ -69,7 +69,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(); $data = json_decode($content, true); diff --git a/src/Geocoder/Provider/IpInfoDb.php b/src/Geocoder/Provider/IpInfoDb.php index a2ab2dc76..a81c2d49e 100644 --- a/src/Geocoder/Provider/IpInfoDb.php +++ b/src/Geocoder/Provider/IpInfoDb.php @@ -14,7 +14,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author William Durand @@ -42,15 +42,15 @@ class IpInfoDb extends AbstractHttpProvider implements Provider private $endpointUrl; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param string $precision The endpoint precision. Either "city" or "country" (faster) + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param string $precision The endpoint precision. Either "city" or "country" (faster) * - * @throws Geocoder\Exception\InvalidArgument + * @throws \Geocoder\Exception\InvalidArgument */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $precision = 'city') + public function __construct(HttpClient $client, $apiKey, $precision = 'city') { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; switch ($precision) { @@ -121,7 +121,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)); diff --git a/src/Geocoder/Provider/MapQuest.php b/src/Geocoder/Provider/MapQuest.php index 0e37cac46..a37819ac9 100644 --- a/src/Geocoder/Provider/MapQuest.php +++ b/src/Geocoder/Provider/MapQuest.php @@ -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 William Durand @@ -54,13 +54,13 @@ class MapQuest extends AbstractHttpProvider implements Provider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param bool $licensed True to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param bool $licensed True to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $licensed = false) + public function __construct(HttpClient $client, $apiKey, $licensed = false) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->licensed = $licensed; @@ -120,7 +120,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)); diff --git a/src/Geocoder/Provider/MaxMind.php b/src/Geocoder/Provider/MaxMind.php index 5b5ffee3e..0bd7d0217 100644 --- a/src/Geocoder/Provider/MaxMind.php +++ b/src/Geocoder/Provider/MaxMind.php @@ -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 Andrea Cristaudo @@ -56,14 +56,14 @@ class MaxMind extends AbstractHttpProvider implements Provider private $useSsl = false; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param string $service The specific Maxmind service to use (optional). - * @param bool $useSsl Whether to use an SSL connection (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param string $service The specific Maxmind service to use (optional). + * @param bool $useSsl Whether to use an SSL connection (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $service = self::CITY_EXTENDED_SERVICE, $useSsl = false) + public function __construct(HttpClient $client, $apiKey, $service = self::CITY_EXTENDED_SERVICE, $useSsl = false) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->service = $service; @@ -116,7 +116,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(); $fields = $this->fieldsForService($this->service); if (null === $content || '' === $content) { diff --git a/src/Geocoder/Provider/Nominatim.php b/src/Geocoder/Provider/Nominatim.php index d1a73d24b..f9ed8cac5 100644 --- a/src/Geocoder/Provider/Nominatim.php +++ b/src/Geocoder/Provider/Nominatim.php @@ -12,7 +12,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Niklas Närhinen @@ -27,13 +27,13 @@ class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider private $rootUrl; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $rootUrl Root URL of the nominatim server - * @param string $locale A locale (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $rootUrl Root URL of the nominatim server + * @param string $locale A locale (optional). */ - public function __construct(HttpAdapterInterface $adapter, $rootUrl, $locale = null) + public function __construct(HttpClient $client, $rootUrl, $locale = null) { - parent::__construct($adapter); + parent::__construct($client); $this->rootUrl = rtrim($rootUrl, '/'); $this->locale = $locale; @@ -163,7 +163,9 @@ private function executeQuery($query) $query = sprintf('%s&accept-language=%s', $query, $this->getLocale()); } - return (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + + return (string) $this->getHttpClient()->sendRequest($request)->getBody(); } private function getGeocodeEndpointUrl() diff --git a/src/Geocoder/Provider/OpenCage.php b/src/Geocoder/Provider/OpenCage.php index 5d48e9fc8..e98786b11 100644 --- a/src/Geocoder/Provider/OpenCage.php +++ b/src/Geocoder/Provider/OpenCage.php @@ -13,7 +13,7 @@ use Geocoder\Exception\QuotaExceeded; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author mtm @@ -38,14 +38,14 @@ class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param bool $useSsl Whether to use an SSL connection (optional). - * @param string|null $locale A locale (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param bool $useSsl Whether to use an SSL connection (optional). + * @param string|null $locale A locale (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $useSsl = false, $locale = null) + public function __construct(HttpClient $client, $apiKey, $useSsl = false, $locale = null) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->scheme = $useSsl ? 'https' : 'http'; @@ -99,7 +99,8 @@ private function executeQuery($query) $query = sprintf('%s&language=%s', $query, $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)); diff --git a/src/Geocoder/Provider/OpenStreetMap.php b/src/Geocoder/Provider/OpenStreetMap.php index 8097d3474..081b56b8e 100644 --- a/src/Geocoder/Provider/OpenStreetMap.php +++ b/src/Geocoder/Provider/OpenStreetMap.php @@ -10,7 +10,7 @@ namespace Geocoder\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Niklas Närhinen @@ -23,12 +23,12 @@ class OpenStreetMap extends Nominatim const ROOT_URL = 'http://nominatim.openstreetmap.org'; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $locale A locale (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $locale A locale (optional). */ - public function __construct(HttpAdapterInterface $adapter, $locale = null) + public function __construct(HttpClient $client, $locale = null) { - parent::__construct($adapter, static::ROOT_URL, $locale); + parent::__construct($client, static::ROOT_URL, $locale); } /** diff --git a/src/Geocoder/Provider/TomTom.php b/src/Geocoder/Provider/TomTom.php index fefec969f..e78750b70 100644 --- a/src/Geocoder/Provider/TomTom.php +++ b/src/Geocoder/Provider/TomTom.php @@ -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 Antoine Corcy @@ -38,13 +38,13 @@ class TomTom 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; @@ -102,7 +102,8 @@ private function executeQuery($query) $query = sprintf('%s&language=%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 (false !== stripos($content, "Developer Inactive")) { throw new InvalidCredentials('Map API Key provided is not valid.'); diff --git a/src/Geocoder/Provider/Yandex.php b/src/Geocoder/Provider/Yandex.php index 2a3f08d4f..6deab1e46 100644 --- a/src/Geocoder/Provider/Yandex.php +++ b/src/Geocoder/Provider/Yandex.php @@ -12,7 +12,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Antoine Corcy @@ -37,13 +37,13 @@ class Yandex extends AbstractHttpProvider implements LocaleAwareProvider private $toponym; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $locale A locale (optional). - * @param string $toponym Toponym biasing only for reverse geocoding (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $locale A locale (optional). + * @param string $toponym Toponym biasing only for reverse geocoding (optional). */ - public function __construct(HttpAdapterInterface $adapter, $locale = null, $toponym = null) + public function __construct(HttpClient $client, $locale = null, $toponym = null) { - parent::__construct($adapter); + parent::__construct($client); $this->locale = $locale; $this->toponym = $toponym; @@ -97,7 +97,8 @@ private function executeQuery($query) $query = sprintf('%s&results=%d', $query, $this->getLimit()); - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); $json = (array) json_decode($content, true); if (empty($json) || isset($json['error']) || diff --git a/tests/Geocoder/Tests/CachedResponseAdapter.php b/tests/Geocoder/Tests/CachedResponseAdapter.php deleted file mode 100644 index 3ef242d67..000000000 --- a/tests/Geocoder/Tests/CachedResponseAdapter.php +++ /dev/null @@ -1,69 +0,0 @@ -adapter = $adapter; - $this->useCache = $useCache; - $this->apiKey = $apiKey; - $this->cacheDir = $cacheDir; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'cached_response'; - } - - /** - * {@inheritDoc} - */ - protected function sendInternalRequest(InternalRequestInterface $internalRequest) - { - $url = (string) $internalRequest->getUri(); - if ($this->apiKey) { - $url = str_replace($this->apiKey, '[apikey]', $url); - } - - $file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url)); - - if ($this->useCache && is_file($file) && is_readable($file)) { - $content = unserialize(file_get_contents($file)); - - if (!empty($content)) { - return $this->adapter - ->getConfiguration() - ->getMessageFactory() - ->createResponse(200, RequestInterface::PROTOCOL_VERSION_1_1, [], $content); - } - } - - $response = $this->adapter->get($url); - - if ($this->useCache) { - file_put_contents($file, serialize((string) $response->getBody())); - } - - return $response; - } -} diff --git a/tests/Geocoder/Tests/CachedResponseClient.php b/tests/Geocoder/Tests/CachedResponseClient.php new file mode 100644 index 000000000..97cc212fd --- /dev/null +++ b/tests/Geocoder/Tests/CachedResponseClient.php @@ -0,0 +1,52 @@ +delegate = $delegate; + $this->useCache = $useCache; + $this->apiKey = $apiKey; + $this->cacheDir = $cacheDir; + } + + /** + * {@inheritDoc} + */ + public function sendRequest(RequestInterface $request) + { + $url = (string) $request->getUri(); + if ($this->apiKey) { + $url = str_replace($this->apiKey, '[apikey]', $url); + } + + $file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url)); + + if ($this->useCache && is_file($file) && is_readable($file)) { + return new Response(200, [], Psr7\stream_for(unserialize(file_get_contents($file)))); + } + + $response = $this->delegate->sendRequest($request); + + if ($this->useCache) { + file_put_contents($file, $response->getBody()->getContents()); + } + + return $response; + } +} diff --git a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php index be774d220..36df0f355 100644 --- a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php +++ b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php @@ -3,10 +3,8 @@ namespace Geocoder\Tests\Provider; use Geocoder\Tests\TestCase; - +use Http\Mock\Client; use Geocoder\Provider\AbstractProvider; -use Ivory\HttpAdapter\AbstractHttpAdapter; -use Ivory\HttpAdapter\Message\InternalRequestInterface; /** * @author William Durand @@ -15,8 +13,8 @@ class AbstractProviderTest extends TestCase { public function testGetLocalhostDefaults() { - $adapter = new MockHttpAdapter(); - $provider = new MockProvider($adapter); + $client = $this->prophesize('Http\Client\HttpClient'); + $provider = new MockProvider($client->reveal()); $result = $provider->getLocalhostDefaults(); $this->assertEquals(2, count($result)); @@ -32,15 +30,3 @@ public function getLocalhostDefaults() return parent::getLocalhostDefaults(); } } - -class MockHttpAdapter extends AbstractHttpAdapter -{ - public function getName() - { - return 'mock_http_adapter'; - } - - protected function sendInternalRequest(InternalRequestInterface $internalRequest) - { - } -} diff --git a/tests/Geocoder/Tests/Provider/GeonamesTest.php b/tests/Geocoder/Tests/Provider/GeonamesTest.php index d54cedf18..b3c779a76 100644 --- a/tests/Geocoder/Tests/Provider/GeonamesTest.php +++ b/tests/Geocoder/Tests/Provider/GeonamesTest.php @@ -19,7 +19,7 @@ public function testGetName() */ public function testGeocodeWithNullUsername() { - $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); + $provider = new Geonames($this->getMock('Http\Client\HttpClient'), null); $provider->geocode('foo'); } @@ -29,7 +29,7 @@ public function testGeocodeWithNullUsername() */ public function testReverseWithNullUsername() { - $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); + $provider = new Geonames($this->getMock('Http\Client\HttpClient'), null); $provider->reverse(1,2); } diff --git a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php index 80897f3fc..fadd0e861 100644 --- a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php +++ b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php @@ -27,7 +27,7 @@ public function testGetName() */ public function testGetDataWithNullApiKey() { - $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); + $provider = new IpInfoDb($this->getMock('Http\Client\HttpClient'), null); $provider->geocode('foo'); } @@ -196,7 +196,7 @@ public function testGetGeocodedDataWithCountryPrecision() */ public function testReverse() { - $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), 'api_key'); + $provider = new IpInfoDb($this->getMock('Http\Client\HttpClient'), 'api_key'); $provider->reverse(null, null); } } diff --git a/tests/Geocoder/Tests/TestCase.php b/tests/Geocoder/Tests/TestCase.php index d65fcaeeb..34be318e1 100644 --- a/tests/Geocoder/Tests/TestCase.php +++ b/tests/Geocoder/Tests/TestCase.php @@ -3,8 +3,10 @@ namespace Geocoder\Tests; use Geocoder\Model\AddressFactory; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Ivory\HttpAdapter\CurlHttpAdapter; +use GuzzleHttp\Psr7\Response; +use Http\Client\HttpClient; +use Http\Mock\Client as MockClient; +use Http\Adapter\Guzzle6\Client as GuzzleClient; /** * @author William Durand @@ -13,7 +15,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * @param null|object $expects - * @return HttpAdapterInterface + * @return HttpClient */ protected function getMockAdapter($expects = null) { @@ -23,61 +25,41 @@ protected function getMockAdapter($expects = null) $stream = $this->getMock('Psr\Http\Message\StreamInterface'); $stream - ->expects($this->any()) + ->expects($expects) ->method('__toString') ->will($this->returnValue('')); - $response = $this->getMock('Psr\Http\Message\MessageInterface'); - $response - ->expects($this->any()) - ->method('getBody') - ->will($this->returnValue($stream)); - - $adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface'); - $adapter - ->expects($expects) - ->method('get') - ->will($this->returnValue($response)); + $client = new MockClient(); + $client->addResponse(new Response(200, [], $stream)); - return $adapter; + return $client; } /** * @param $returnValue - * @return HttpAdapterInterface + * @return HttpClient */ protected function getMockAdapterReturns($returnValue) { - $body = $this->getMock('Psr\Http\Message\StreamInterface'); - $body - ->expects($this->once()) - ->method('__toString') - ->will($this->returnValue((string) $returnValue)); - - $response = $this->getMock('Psr\Http\Message\MessageInterface'); - $response - ->expects($this->once()) - ->method('getBody') - ->will($this->returnValue($body)); - - $adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface'); - $adapter - ->expects($this->once()) - ->method('get') - ->will($this->returnValue($response)); + $client = new MockClient(); + $client->addResponse(new Response(200, [], (string) $returnValue)); - return $adapter; + return $client; } /** * Because I was bored to fix the test suite because of * a change in a third-party API... * - * @return HttpAdapterInterface + * @return HttpClient */ protected function getAdapter($apiKey = null) { - return new CachedResponseAdapter(new CurlHttpAdapter(), $this->useCache(), $apiKey); + return new CachedResponseClient( + new GuzzleClient(), + $this->useCache(), + $apiKey + ); } /**