Skip to content

Commit 9ea2bcb

Browse files
authored
Implement Httplug (#512)
* Implement httplug * Fix tests and remove .puli from repo * Remove php 5.4 * Updated dependencies * Doc update and use latest discovery * Make sure we using phpuinit 4 * Using discovery 1.0 * Fixed doc block alignment * Use PSR7 implementations instead of mocks
1 parent c2373a6 commit 9ea2bcb

28 files changed

+279
-258
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.lock
33
composer.phar
44
phpunit.xml
55
php-cs-fixer.phar
6+
.puli/

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ env:
1212
- deps=""
1313

1414
php:
15-
- 5.4
1615
- 5.5
1716
- 5.6
1817
- 7.0
@@ -21,7 +20,7 @@ php:
2120
matrix:
2221
fast_finish: true
2322
include:
24-
- php: 5.4
23+
- php: 5.5
2524
env: deps="low"
2625

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

34-
script: phpunit --coverage-text
33+
script: ./vendor/bin/phpunit --coverage-text

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ since each HTTP-based provider implements
7373
[PSR-7](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md).
7474

7575
```php
76-
$curl = new \Ivory\HttpAdapter\CurlHttpAdapter();
77-
$geocoder = new \Geocoder\Provider\GoogleMaps($curl);
76+
$adapter = new \Http\Adapter\Guzzle6\Client();
77+
$geocoder = new \Geocoder\Provider\GoogleMaps($adapter);
7878

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

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

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

226226
### Providers
227227

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

361361
``` php
362362
$geocoder = new \Geocoder\ProviderAggregator();
363-
$adapter = new \Ivory\HttpAdapter\CurlHttpAdapter();
363+
$adapter = new \Http\Adapter\Guzzle6\Client();
364364

365365
$chain = new \Geocoder\Provider\Chain([
366366
new \Geocoder\Provider\FreeGeoIp($adapter),

composer.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
1212
}
1313
],
1414
"require": {
15-
"php": "^5.4|^7.0",
16-
"egeloen/http-adapter": "~0.8|~1.0",
17-
"igorw/get-in": "~1.0"
15+
"php": "^5.5 || ^7.0",
16+
"igorw/get-in": "^1.0",
17+
"psr/http-message-implementation": "^1.0",
18+
"php-http/client-implementation": "^1.0",
19+
"php-http/message-factory": "^1.0.2",
20+
"php-http/httplug": "^1.0",
21+
"php-http/discovery": "^1.0"
1822
},
1923
"require-dev": {
24+
"phpunit/phpunit": "^4.8",
2025
"geoip2/geoip2": "~2.0",
21-
"symfony/stopwatch": "~2.5"
26+
"symfony/stopwatch": "~2.5",
27+
"php-http/message": "^1.0",
28+
"php-http/guzzle6-adapter": "^1.0",
29+
"php-http/mock-client": "^0.3.0"
2230
},
2331
"suggest": {
2432
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",

src/Geocoder/Provider/AbstractHttpProvider.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,81 @@
1010

1111
namespace Geocoder\Provider;
1212

13-
use Ivory\HttpAdapter\HttpAdapterInterface;
13+
use Http\Message\MessageFactory;
14+
use Http\Discovery\HttpClientDiscovery;
15+
use Http\Discovery\MessageFactoryDiscovery;
16+
use Http\Client\HttpClient;
1417

1518
/**
1619
* @author William Durand <william.durand1@gmail.com>
1720
*/
1821
class AbstractHttpProvider extends AbstractProvider
1922
{
2023
/**
21-
* @var HttpAdapterInterface
24+
* @var HttpClient
2225
*/
23-
private $adapter;
26+
private $client;
2427

2528
/**
26-
* @param HttpAdapterInterface $adapter An HTTP adapter
29+
* @var MessageFactory
2730
*/
28-
public function __construct(HttpAdapterInterface $adapter)
31+
private $messageFactory;
32+
33+
/**
34+
* @param HttpClient $client
35+
* @param MessageFactory|null $factory
36+
*/
37+
public function __construct(HttpClient $client, MessageFactory $factory = null)
2938
{
3039
parent::__construct();
3140

32-
$this->adapter = $adapter;
41+
$this->client = $client;
42+
$this->messageFactory = $factory;
3343
}
3444

3545
/**
3646
* Returns the HTTP adapter.
3747
*
38-
* @return HttpAdapterInterface
48+
* @return HttpClient
49+
*/
50+
protected function getHttpClient()
51+
{
52+
return $this->client;
53+
}
54+
55+
/**
56+
* @return MessageFactory
3957
*/
40-
public function getAdapter()
58+
protected function getMessageFactory()
4159
{
42-
return $this->adapter;
60+
if ($this->messageFactory === null) {
61+
$this->messageFactory = MessageFactoryDiscovery::find();
62+
}
63+
64+
return $this->messageFactory;
65+
}
66+
67+
/**
68+
* @param HttpClient $client
69+
*
70+
* @return AbstractHttpProvider
71+
*/
72+
public function setClient(HttpClient $client)
73+
{
74+
$this->client = $client;
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* @param MessageFactory $messageFactory
81+
*
82+
* @return AbstractHttpProvider
83+
*/
84+
public function setMessageFactory(MessageFactory $messageFactory)
85+
{
86+
$this->messageFactory = $messageFactory;
87+
88+
return $this;
4389
}
4490
}

src/Geocoder/Provider/ArcGISOnline.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use Geocoder\Exception\NoResult;
1414
use Geocoder\Exception\UnsupportedOperation;
15-
use Ivory\HttpAdapter\HttpAdapterInterface;
15+
use Http\Client\HttpClient;
1616

1717
/**
1818
* @author ALKOUM Dorian <baikunz@gmail.com>
@@ -40,13 +40,13 @@ class ArcGISOnline extends AbstractHttpProvider implements Provider
4040
private $protocol;
4141

4242
/**
43-
* @param HttpAdapterInterface $adapter An HTTP adapter
44-
* @param string $sourceCountry Country biasing (optional)
45-
* @param bool $useSsl Whether to use an SSL connection (optional)
43+
* @param HttpClient $client An HTTP adapter
44+
* @param string $sourceCountry Country biasing (optional)
45+
* @param bool $useSsl Whether to use an SSL connection (optional)
4646
*/
47-
public function __construct(HttpAdapterInterface $adapter, $sourceCountry = null, $useSsl = false)
47+
public function __construct(HttpClient $client, $sourceCountry = null, $useSsl = false)
4848
{
49-
parent::__construct($adapter);
49+
parent::__construct($client);
5050

5151
$this->sourceCountry = $sourceCountry;
5252
$this->protocol = $useSsl ? 'https' : 'http';
@@ -167,8 +167,9 @@ private function buildQuery($query)
167167
*/
168168
private function executeQuery($query)
169169
{
170-
$query = $this->buildQuery($query);
171-
$content = (string) $this->getAdapter()->get($query)->getBody();
170+
$query = $this->buildQuery($query);
171+
$request = $this->getMessageFactory()->createRequest('GET', $query);
172+
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
172173

173174
if (empty($content)) {
174175
throw new NoResult(sprintf('Could not execute query "%s".', $query));

src/Geocoder/Provider/BingMaps.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Geocoder\Exception\InvalidCredentials;
1414
use Geocoder\Exception\NoResult;
1515
use Geocoder\Exception\UnsupportedOperation;
16-
use Ivory\HttpAdapter\HttpAdapterInterface;
16+
use Http\Client\HttpClient;
1717

1818
/**
1919
* @author David Guyon <dguyon@gmail.com>
@@ -38,13 +38,13 @@ class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
3838
private $apiKey;
3939

4040
/**
41-
* @param HttpAdapterInterface $adapter An HTTP adapter
42-
* @param string $apiKey An API key
43-
* @param string $locale A locale (optional)
41+
* @param HttpClient $client An HTTP adapter
42+
* @param string $apiKey An API key
43+
* @param string $locale A locale (optional)
4444
*/
45-
public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null)
45+
public function __construct(HttpClient $client, $apiKey, $locale = null)
4646
{
47-
parent::__construct($adapter);
47+
parent::__construct($client);
4848

4949
$this->apiKey = $apiKey;
5050
$this->locale = $locale;
@@ -100,7 +100,8 @@ private function executeQuery($query)
100100
$query = sprintf('%s&culture=%s', $query, str_replace('_', '-', $this->getLocale()));
101101
}
102102

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

105106
if (empty($content)) {
106107
throw new NoResult(sprintf('Could not execute query "%s".', $query));

src/Geocoder/Provider/FreeGeoIp.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function getName()
6565
*/
6666
private function executeQuery($query)
6767
{
68-
$content = (string) $this->getAdapter()->get($query)->getBody();
68+
$request = $this->getMessageFactory()->createRequest('GET', $query);
69+
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
6970

7071
if (empty($content)) {
7172
throw new NoResult(sprintf('Could not execute query %s', $query));

src/Geocoder/Provider/GeoIPs.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Geocoder\Exception\NoResult;
1616
use Geocoder\Exception\QuotaExceeded;
1717
use Geocoder\Exception\UnsupportedOperation;
18-
use Ivory\HttpAdapter\HttpAdapterInterface;
18+
use Http\Client\HttpClient;
1919

2020
/**
2121
* @author Andrea Cristaudo <andrea.cristaudo@gmail.com>
@@ -50,12 +50,12 @@ class GeoIPs extends AbstractHttpProvider implements Provider
5050
private $apiKey;
5151

5252
/**
53-
* @param HttpAdapterInterface $adapter An HTTP adapter
54-
* @param string $apiKey An API key
53+
* @param HttpClient $client An HTTP adapter
54+
* @param string $apiKey An API key
5555
*/
56-
public function __construct(HttpAdapterInterface $adapter, $apiKey)
56+
public function __construct(HttpClient $client, $apiKey)
5757
{
58-
parent::__construct($adapter);
58+
parent::__construct($client);
5959

6060
$this->apiKey = $apiKey;
6161
}
@@ -107,7 +107,8 @@ public function getName()
107107
*/
108108
private function executeQuery($query)
109109
{
110-
$content = (string) $this->getAdapter()->get($query)->getBody();
110+
$request = $this->getMessageFactory()->createRequest('GET', $query);
111+
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
111112

112113
if (empty($content)) {
113114
throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query));

src/Geocoder/Provider/GeoPlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function getName()
6262
*/
6363
private function executeQuery($query)
6464
{
65-
$content = (string) $this->getAdapter()->get($query)->getBody();
65+
$request = $this->getMessageFactory()->createRequest('GET', $query);
66+
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
6667

6768
if (empty($content)) {
6869
throw new NoResult(sprintf('Could not execute query "%s".', $query));

0 commit comments

Comments
 (0)