Skip to content

Commit b89f986

Browse files
Replace HTTPlug factories by PSR-17 (geocoder-php#1184)
* Replace HTTPlug factories by PSR-17 * minor fix --------- Co-authored-by: Nyholm <[email protected]>
1 parent 9912a35 commit b89f986

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+242
-164
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 4.4.0 (2023-??-??)
4+
5+
* Added: Method `AbstractHttpProvider::createRequest()`
6+
* Deprecated: Method `AbstractHttpProvider::getMessageFactory()`
7+
38
## 4.3.0 (2022-07-30)
49

510
* Removed: Support for PHP 7.3

composer.json

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,30 @@
1919
"require": {
2020
"php": "^8.0",
2121
"igorw/get-in": "^1.0",
22-
"php-http/discovery": "^1.4",
23-
"php-http/message-factory": "^1.0.2",
22+
"php-http/discovery": "^1.17",
2423
"php-http/promise": "^1.0",
25-
"psr/http-client": "^1.0",
2624
"psr/http-client-implementation": "^1.0",
27-
"psr/http-message-implementation": "^1.0",
25+
"psr/http-factory-implementation": "^1.0",
2826
"psr/log": "^1.0 || ^2.0 || ^3.0",
2927
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
3028
},
3129
"require-dev": {
3230
"cache/array-adapter": "^1.0",
3331
"cache/simple-cache-bridge": "^1.0",
3432
"cache/void-adapter": "^1.0",
35-
"geocoder-php/provider-integration-tests": "^1.6.2",
33+
"geocoder-php/provider-integration-tests": "^1.6.3",
3634
"geoip2/geoip2": "~2.0",
3735
"nyholm/nsa": "^1.1",
3836
"nyholm/psr7": "^1.0",
3937
"php-cs-fixer/shim": "^3.22",
40-
"php-http/curl-client": "^2.2",
4138
"php-http/message": "^1.0",
39+
"php-http/message-factory": "^1.0.2",
4240
"php-http/mock-client": "^1.0",
4341
"phpstan/extension-installer": "^1.3",
4442
"phpstan/phpstan": "^1.10",
4543
"phpstan/phpstan-phpunit": "^1.3",
4644
"phpunit/phpunit": "^9.5",
45+
"symfony/http-client": "^5.4 || ^6.3",
4746
"symfony/stopwatch": "~2.5 || ~5.0"
4847
},
4948
"suggest": {
@@ -72,7 +71,7 @@
7271
"config": {
7372
"allow-plugins": {
7473
"phpstan/extension-installer": true,
75-
"php-http/discovery": true
74+
"php-http/discovery": false
7675
},
7776
"sort-packages": true
7877
},

src/Common/Model/Address.php

-4
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,6 @@ private static function createCountry($name, $code)
255255
}
256256

257257
/**
258-
* @param float $south
259-
* @param float $west
260-
* @param float $north
261-
*
262258
* @return Bounds|null
263259
*/
264260
private static function createBounds(?float $south, ?float $west, ?float $north, ?float $east)

src/Common/Model/Country.php

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ final class Country
3131
*/
3232
private $code;
3333

34-
/**
35-
* @param string $name
36-
* @param string $code
37-
*/
3834
public function __construct(string $name = null, string $code = null)
3935
{
4036
if (null === $name && null === $code) {

src/Common/ProviderAggregator.php

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ public function getProviders(): array
131131
*
132132
* @param GeocodeQuery|ReverseQuery $query
133133
* @param Provider[] $providers
134-
* @param Provider $currentProvider
135134
*
136135
* @throws ProviderNotRegistered
137136
*/

src/Common/StatefulGeocoder.php

-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ final class StatefulGeocoder implements Geocoder
4242
*/
4343
private $provider;
4444

45-
/**
46-
* @param string $locale
47-
*/
4845
public function __construct(Provider $provider, string $locale = null)
4946
{
5047
$this->provider = $provider;

src/Common/composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@
4343
"scripts": {
4444
"test": "vendor/bin/phpunit",
4545
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
46+
},
47+
"config": {
48+
"allow-plugins": {
49+
"php-http/discovery": false
50+
}
4651
}
4752
}

src/Http/Provider/AbstractHttpProvider.php

+128-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
use Geocoder\Exception\InvalidServerResponse;
1717
use Geocoder\Exception\QuotaExceeded;
1818
use Geocoder\Provider\AbstractProvider;
19-
use Http\Discovery\MessageFactoryDiscovery;
19+
use Http\Discovery\Psr17Factory;
2020
use Http\Message\MessageFactory;
2121
use Psr\Http\Client\ClientInterface;
22+
use Psr\Http\Message\RequestFactoryInterface;
2223
use Psr\Http\Message\RequestInterface;
24+
use Psr\Http\Message\ResponseFactoryInterface;
25+
use Psr\Http\Message\ResponseInterface;
26+
use Psr\Http\Message\StreamFactoryInterface;
27+
use Psr\Http\Message\StreamInterface;
28+
use Psr\Http\Message\UriInterface;
2329

2430
/**
2531
* @author William Durand <[email protected]>
@@ -33,14 +39,17 @@ abstract class AbstractHttpProvider extends AbstractProvider
3339
private $client;
3440

3541
/**
36-
* @var MessageFactory
42+
* @var RequestFactoryInterface&StreamFactoryInterface)|MessageFactory
3743
*/
3844
private $messageFactory;
3945

40-
public function __construct(ClientInterface $client, MessageFactory $factory = null)
46+
/**
47+
* @param Psr17Factory|MessageFactory|null $factory Passing a MessageFactory is @deprecated
48+
*/
49+
public function __construct(ClientInterface $client, MessageFactory|Psr17Factory $factory = null)
4150
{
4251
$this->client = $client;
43-
$this->messageFactory = $factory ?: MessageFactoryDiscovery::find();
52+
$this->messageFactory = $factory ?? ($client instanceof RequestFactoryInterface && $client instanceof StreamFactoryInterface ? $client : new Psr17Factory());
4453
}
4554

4655
/**
@@ -57,7 +66,35 @@ protected function getUrlContents(string $url): string
5766

5867
protected function getRequest(string $url): RequestInterface
5968
{
60-
return $this->getMessageFactory()->createRequest('GET', $url);
69+
return $this->createRequest('GET', $url);
70+
}
71+
72+
/**
73+
* @param array<string,string|string[]> $headers
74+
*/
75+
protected function createRequest(string $method, string $uri, array $headers = [], string $body = null): RequestInterface
76+
{
77+
if ($this->messageFactory instanceof MessageFactory) {
78+
return $this->messageFactory->createRequest($method, $uri, $headers, $body);
79+
}
80+
81+
$request = $this->messageFactory->createRequest($method, $uri);
82+
83+
foreach ($headers as $name => $value) {
84+
$request = $request->withAddedHeader($name, $value);
85+
}
86+
87+
if (null === $body) {
88+
return $request;
89+
}
90+
91+
$stream = $this->messageFactory->createStream($body);
92+
93+
if ($stream->isSeekable()) {
94+
$stream->seek(0);
95+
}
96+
97+
return $request->withBody($stream);
6198
}
6299

63100
/**
@@ -94,8 +131,93 @@ protected function getHttpClient(): ClientInterface
94131
return $this->client;
95132
}
96133

134+
/**
135+
* @deprecated Use createRequest instead
136+
*/
97137
protected function getMessageFactory(): MessageFactory
98138
{
99-
return $this->messageFactory;
139+
if ($this->messageFactory instanceof MessageFactory) {
140+
return $this->messageFactory;
141+
}
142+
143+
$factory = $this->messageFactory instanceof ResponseFactoryInterface ? $this->messageFactory : new Psr17Factory();
144+
145+
return new class($factory) implements MessageFactory {
146+
public function __construct(
147+
/**
148+
* @param RequestFactoryInterface&ResponseFactoryInterface&StreamFactoryInterface $factory
149+
*/
150+
private RequestFactoryInterface|ResponseFactoryInterface|StreamFactoryInterface $factory,
151+
) {
152+
}
153+
154+
/**
155+
* @param string $method
156+
* @param string|UriInterface $uri
157+
* @param array<string,string|string[]> $headers
158+
* @param resource|string|StreamInterface|null $body
159+
* @param string $protocolVersion
160+
*/
161+
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface
162+
{
163+
$request = $this->factory->createRequest($method, $uri);
164+
165+
foreach ($headers as $name => $value) {
166+
$request = $request->withAddedHeader($name, $value);
167+
}
168+
169+
if (null !== $body) {
170+
$request = $request->withBody($this->createStream($body));
171+
}
172+
173+
return $request->withProtocolVersion($protocolVersion);
174+
}
175+
176+
/**
177+
* @param int $statusCode
178+
* @param string|null $reasonPhrase
179+
* @param array<string,string|string[]> $headers
180+
* @param resource|string|StreamInterface|null $body
181+
* @param string $protocolVersion
182+
*/
183+
public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $protocolVersion = '1.1'): ResponseInterface
184+
{
185+
$response = $this->factory->createResponse($statusCode, $reasonPhrase);
186+
187+
foreach ($headers as $name => $value) {
188+
$response = $response->withAddedHeader($name, $value);
189+
}
190+
191+
if (null !== $body) {
192+
$response = $response->withBody($this->createStream($body));
193+
}
194+
195+
return $response->withProtocolVersion($protocolVersion);
196+
}
197+
198+
/**
199+
* @param string|resource|StreamInterface|null $body
200+
*/
201+
private function createStream($body = ''): StreamInterface
202+
{
203+
if ($body instanceof StreamInterface) {
204+
return $body;
205+
}
206+
207+
if (\is_string($body ?? '')) {
208+
$stream = $this->factory->createStream($body ?? '');
209+
} elseif (\is_resource($body)) {
210+
$stream = $this->factory->createStreamFromResource($body);
211+
} else {
212+
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body)));
213+
}
214+
215+
if ($stream->isSeekable()) {
216+
$stream->seek(0);
217+
}
218+
219+
return $stream;
220+
}
221+
};
100222
}
101223
}

src/Http/composer.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
],
1616
"require": {
1717
"php": "^8.0",
18-
"php-http/client-implementation": "^1.0",
19-
"php-http/discovery": "^1.6",
20-
"php-http/httplug": "^1.0 || ^2.0",
21-
"php-http/message-factory": "^1.0.2",
22-
"psr/http-message": "^1.0 || ^2.0",
23-
"psr/http-message-implementation": "^1.0",
18+
"php-http/discovery": "^1.17",
19+
"psr/http-client-implementation": "^1.0",
20+
"psr/http-factory-implementation": "^1.0",
2421
"willdurand/geocoder": "^4.0"
2522
},
2623
"require-dev": {
@@ -48,5 +45,10 @@
4845
"scripts": {
4946
"test": "vendor/bin/phpunit",
5047
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
48+
},
49+
"config": {
50+
"allow-plugins": {
51+
"php-http/discovery": false
52+
}
5153
}
52-
}
54+
}

src/Plugin/composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@
4242
"scripts": {
4343
"test": "vendor/bin/phpunit",
4444
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
45+
},
46+
"config": {
47+
"allow-plugins": {
48+
"php-http/discovery": false
49+
}
4550
}
4651
}

src/Provider/AlgoliaPlaces/AlgoliaPlaces.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function getTypes(): array
115115

116116
protected function getRequest(string $url): RequestInterface
117117
{
118-
return $this->getMessageFactory()->createRequest(
118+
return $this->createRequest(
119119
'POST',
120120
$url,
121121
$this->buildHeaders(),

src/Provider/AlgoliaPlaces/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ You should set a locale on the query. If it is missing, results may not be as co
3434
use Geocoder\Query\GeocodeQuery;
3535
use Geocoder\Query\ReverseQuery;
3636

37-
$httpClient = new \GuzzleHttp\Client();
37+
$httpClient = new \Http\Discovery\Psr18Client();
3838

3939
// Unauthenticated
4040
$provider = new \Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces($httpClient);

src/Provider/AlgoliaPlaces/Tests/AlgoliaPlacesTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Geocoder\Location;
1818
use Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces;
1919
use Geocoder\Query\GeocodeQuery;
20-
use Http\Client\Curl\Client as HttplugClient;
20+
use Http\Discovery\Psr18Client;
2121
use Psr\Http\Client\ClientInterface;
2222

2323
/**
@@ -35,7 +35,7 @@ protected function getCacheDir(): string
3535
*/
3636
protected function getHttpClient(string $apiKey = null, string $appCode = null): ClientInterface
3737
{
38-
return new CachedResponseClient(new HttplugClient(), $this->getCacheDir(), $apiKey, $appCode);
38+
return new CachedResponseClient(new Psr18Client(), $this->getCacheDir(), $apiKey, $appCode);
3939
}
4040

4141
public function testGeocodeQueryWithLocale(): void

src/Provider/AlgoliaPlaces/composer.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
"require": {
1515
"php": "^8.0",
1616
"ext-json": "*",
17-
"geocoder-php/common-http": "^4.1",
17+
"geocoder-php/common-http": "^4.6",
1818
"willdurand/geocoder": "^4.0"
1919
},
2020
"provide": {
2121
"geocoder-php/provider-implementation": "1.0"
2222
},
2323
"require-dev": {
24-
"geocoder-php/provider-integration-tests": "^1.1",
25-
"php-http/curl-client": "^2.2",
24+
"geocoder-php/provider-integration-tests": "^1.6.3",
2625
"php-http/message": "^1.0",
2726
"phpunit/phpunit": "^9.5"
2827
},
@@ -45,4 +44,4 @@
4544
"test": "vendor/bin/phpunit",
4645
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
4746
}
48-
}
47+
}

src/Provider/ArcGISOnline/Readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Since a token is required for the `geocodeAddresses` API, the
3333
### Without a token
3434

3535
```php
36-
$httpClient = new \GuzzleHttp\Client();
36+
$httpClient = new \Http\Discovery\Psr18Client();
3737

3838
$provider = new \Geocoder\Provider\ArcGISList\ArcGISList($httpClient);
3939

@@ -45,7 +45,7 @@ $result = $geocoder->geocodeQuery(GeocodeQuery::create('Buckingham Palace, Londo
4545

4646
```php
4747

48-
$httpClient = new \GuzzleHttp\Client();
48+
$httpClient = new \Http\Discovery\Psr18Client();
4949

5050
// Your token is required.
5151
$provider = \Geocoder\Provider\ArcGISList\ArcGISList::token($httpClient, 'your-token');

0 commit comments

Comments
 (0)