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..e69055484 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: diff --git a/composer.json b/composer.json index 3e456669d..080a9518a 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,23 @@ ], "require": { "php": ">=5.4.0", - "egeloen/http-adapter": "~0.8|~1.0", - "igorw/get-in": "~1.0" + "igorw/get-in": "~1.0", + "psr/http-message-implementation": "^1.0", + "php-http/client-implementation": "^1.0", + "php-http/message-factory": "^1.0", + "php-http/httplug": "^1.0" }, "require-dev": { "geoip2/geoip2": "~2.0", - "symfony/stopwatch": "~2.5" + "symfony/stopwatch": "~2.5", + "php-http/mock-client": "^0.2", + "php-http/message": "^1.0", + "php-http/guzzle6-adapter": "^1.0", + "php-http/discovery": "^0.7.0", + "puli/composer-plugin": "^1.0.0@beta", + "puli/discovery": "^1.0.0@beta", + "puli/repository": "^1.0.0@beta", + "puli/url-generator": "^1.0.0@beta" }, "suggest": { "ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.", diff --git a/puli.json b/puli.json new file mode 100644 index 000000000..9bb3de40b --- /dev/null +++ b/puli.json @@ -0,0 +1,164 @@ +{ + "version": "1.0", + "name": "willdurand/geocoder", + "config": { + "bootstrap-file": "vendor/autoload.php" + }, + "packages": { + "clue/stream-filter": { + "install-path": "vendor/clue/stream-filter", + "installer": "composer", + "env": "dev" + }, + "geoip2/geoip2": { + "install-path": "vendor/geoip2/geoip2", + "installer": "composer", + "env": "dev" + }, + "guzzlehttp/guzzle": { + "install-path": "vendor/guzzlehttp/guzzle", + "installer": "composer", + "env": "dev" + }, + "guzzlehttp/promises": { + "install-path": "vendor/guzzlehttp/promises", + "installer": "composer", + "env": "dev" + }, + "guzzlehttp/psr7": { + "install-path": "vendor/guzzlehttp/psr7", + "installer": "composer", + "env": "dev" + }, + "igorw/get-in": { + "install-path": "vendor/igorw/get-in", + "installer": "composer" + }, + "justinrainbow/json-schema": { + "install-path": "vendor/justinrainbow/json-schema", + "installer": "composer", + "env": "dev" + }, + "maxmind-db/reader": { + "install-path": "vendor/maxmind-db/reader", + "installer": "composer", + "env": "dev" + }, + "maxmind/web-service-common": { + "install-path": "vendor/maxmind/web-service-common", + "installer": "composer", + "env": "dev" + }, + "php-http/client-common": { + "install-path": "vendor/php-http/client-common", + "installer": "composer", + "env": "dev" + }, + "php-http/discovery": { + "install-path": "vendor/php-http/discovery", + "installer": "composer", + "env": "dev" + }, + "php-http/guzzle6-adapter": { + "install-path": "vendor/php-http/guzzle6-adapter", + "installer": "composer", + "env": "dev" + }, + "php-http/httplug": { + "install-path": "vendor/php-http/httplug", + "installer": "composer" + }, + "php-http/message": { + "install-path": "vendor/php-http/message", + "installer": "composer", + "env": "dev" + }, + "php-http/message-factory": { + "install-path": "vendor/php-http/message-factory", + "installer": "composer" + }, + "php-http/mock-client": { + "install-path": "vendor/php-http/mock-client", + "installer": "composer", + "env": "dev" + }, + "php-http/promise": { + "install-path": "vendor/php-http/promise", + "installer": "composer" + }, + "psr/http-message": { + "install-path": "vendor/psr/http-message", + "installer": "composer" + }, + "psr/log": { + "install-path": "vendor/psr/log", + "installer": "composer", + "env": "dev" + }, + "puli/composer-plugin": { + "install-path": "vendor/puli/composer-plugin", + "installer": "composer", + "env": "dev" + }, + "puli/discovery": { + "install-path": "vendor/puli/discovery", + "installer": "composer", + "env": "dev" + }, + "puli/repository": { + "install-path": "vendor/puli/repository", + "installer": "composer", + "env": "dev" + }, + "puli/url-generator": { + "install-path": "vendor/puli/url-generator", + "installer": "composer", + "env": "dev" + }, + "ramsey/uuid": { + "install-path": "vendor/ramsey/uuid", + "installer": "composer", + "env": "dev" + }, + "seld/jsonlint": { + "install-path": "vendor/seld/jsonlint", + "installer": "composer", + "env": "dev" + }, + "symfony/process": { + "install-path": "vendor/symfony/process", + "installer": "composer", + "env": "dev" + }, + "symfony/stopwatch": { + "install-path": "vendor/symfony/stopwatch", + "installer": "composer", + "env": "dev" + }, + "webmozart/assert": { + "install-path": "vendor/webmozart/assert", + "installer": "composer", + "env": "dev" + }, + "webmozart/expression": { + "install-path": "vendor/webmozart/expression", + "installer": "composer", + "env": "dev" + }, + "webmozart/glob": { + "install-path": "vendor/webmozart/glob", + "installer": "composer", + "env": "dev" + }, + "webmozart/json": { + "install-path": "vendor/webmozart/json", + "installer": "composer", + "env": "dev" + }, + "webmozart/path-util": { + "install-path": "vendor/webmozart/path-util", + "installer": "composer", + "env": "dev" + } + } +} diff --git a/src/Geocoder/Provider/AbstractHttpProvider.php b/src/Geocoder/Provider/AbstractHttpProvider.php index e391981b7..a8c519682 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,33 @@ class AbstractHttpProvider extends AbstractProvider { /** - * @var HttpAdapterInterface + * @var HttpClient */ - private $adapter; + protected $client; /** - * @param HttpAdapterInterface $adapter An HTTP adapter + * @var MessageFactory */ - public function __construct(HttpAdapterInterface $adapter) + protected $messageFactory; + + /** + * @param HttpClient $client An HTTP adapter + */ + public function __construct(HttpClient $client = null, MessageFactory $factory = null) { parent::__construct(); - $this->adapter = $adapter; + $this->client = $client ?: HttpClientDiscovery::find(); + $this->messageFactory = $factory ?: MessageFactoryDiscovery::find(); } /** * Returns the HTTP adapter. * - * @return HttpAdapterInterface + * @return HttpClient */ - public function getAdapter() + public function getHttpClient() { - return $this->adapter; + return $this->client; } } diff --git a/src/Geocoder/Provider/ArcGISOnline.php b/src/Geocoder/Provider/ArcGISOnline.php index da3994c98..b214e694e 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 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->messageFactory->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..279602a28 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 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->messageFactory->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..e40dc68cf 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->messageFactory->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..c665bf95e 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 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->messageFactory->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..a5e173016 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->messageFactory->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..9fc2410f4 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 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->messageFactory->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..0274d20e5 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 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->messageFactory->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..35828b07b 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 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..a91a4e28f 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->messageFactory->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..c465b97c7 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 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 */ - 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->messageFactory->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..0c5c60655 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 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->messageFactory->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..487a9934f 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 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->messageFactory->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..9042af9c9 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 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->messageFactory->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 600820af7..95365f824 100644 --- a/src/Geocoder/Provider/OpenCage.php +++ b/src/Geocoder/Provider/OpenCage.php @@ -12,7 +12,7 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author mtm @@ -37,14 +37,14 @@ class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. + * @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'; @@ -98,7 +98,8 @@ private function executeQuery($query) $query = sprintf('%s&language=%s', $query, $this->getLocale()); } - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->messageFactory->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..131f9e3d5 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 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..65a48b235 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 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->messageFactory->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..46eda91df 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 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->messageFactory->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..4b89cfd33 100644 --- a/tests/Geocoder/Tests/TestCase.php +++ b/tests/Geocoder/Tests/TestCase.php @@ -3,8 +3,9 @@ namespace Geocoder\Tests; use Geocoder\Model\AddressFactory; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Ivory\HttpAdapter\CurlHttpAdapter; +use Http\Client\HttpClient; +use Http\Mock\Client as MockClient; +use Http\Adapter\Guzzle6\Client as GuzzleClient; /** * @author William Durand @@ -27,19 +28,16 @@ protected function getMockAdapter($expects = null) ->method('__toString') ->will($this->returnValue('')); - $response = $this->getMock('Psr\Http\Message\MessageInterface'); + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); $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($response); - return $adapter; + return $client; } /** @@ -54,30 +52,31 @@ protected function getMockAdapterReturns($returnValue) ->method('__toString') ->will($this->returnValue((string) $returnValue)); - $response = $this->getMock('Psr\Http\Message\MessageInterface'); + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); $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($response); - 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 + ); } /**