Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace HTTPlug factories by PSR-17 #1184

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 4.4.0 (2023-??-??)

* Added: Method `AbstractHttpProvider::createRequest()`
* Deprecated: Method `AbstractHttpProvider::getMessageFactory()`

## 4.3.0 (2022-07-30)

* Removed: Support for PHP 7.3
Expand Down
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,30 @@
"require": {
"php": "^8.0",
"igorw/get-in": "^1.0",
"php-http/discovery": "^1.4",
"php-http/message-factory": "^1.0.2",
"php-http/discovery": "^1.17",
"php-http/promise": "^1.0",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "^1.0",
"psr/http-message-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"cache/array-adapter": "^1.0",
"cache/simple-cache-bridge": "^1.0",
"cache/void-adapter": "^1.0",
"geocoder-php/provider-integration-tests": "^1.6.2",
"geocoder-php/provider-integration-tests": "^1.6.3",
"geoip2/geoip2": "~2.0",
"nyholm/nsa": "^1.1",
"nyholm/psr7": "^1.0",
"php-cs-fixer/shim": "^3.22",
"php-http/curl-client": "^2.2",
"php-http/message": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/mock-client": "^1.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.5",
"symfony/http-client": "^5.4 || ^6.3",
"symfony/stopwatch": "~2.5 || ~5.0"
},
"suggest": {
Expand Down Expand Up @@ -72,7 +71,7 @@
"config": {
"allow-plugins": {
"phpstan/extension-installer": true,
"php-http/discovery": true
"php-http/discovery": false
},
"sort-packages": true
},
Expand Down
4 changes: 0 additions & 4 deletions src/Common/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,6 @@ private static function createCountry($name, $code)
}

/**
* @param float $south
* @param float $west
* @param float $north
*
* @return Bounds|null
*/
private static function createBounds(?float $south, ?float $west, ?float $north, ?float $east)
Expand Down
4 changes: 0 additions & 4 deletions src/Common/Model/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ final class Country
*/
private $code;

/**
* @param string $name
* @param string $code
*/
public function __construct(string $name = null, string $code = null)
{
if (null === $name && null === $code) {
Expand Down
1 change: 0 additions & 1 deletion src/Common/ProviderAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public function getProviders(): array
*
* @param GeocodeQuery|ReverseQuery $query
* @param Provider[] $providers
* @param Provider $currentProvider
*
* @throws ProviderNotRegistered
*/
Expand Down
3 changes: 0 additions & 3 deletions src/Common/StatefulGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ final class StatefulGeocoder implements Geocoder
*/
private $provider;

/**
* @param string $locale
*/
public function __construct(Provider $provider, string $locale = null)
{
$this->provider = $provider;
Expand Down
5 changes: 5 additions & 0 deletions src/Common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
134 changes: 128 additions & 6 deletions src/Http/Provider/AbstractHttpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Provider\AbstractProvider;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\Psr17Factory;
use Http\Message\MessageFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

/**
* @author William Durand <william.durand1@gmail.com>
Expand All @@ -33,14 +39,17 @@ abstract class AbstractHttpProvider extends AbstractProvider
private $client;

/**
* @var MessageFactory
* @var RequestFactoryInterface&StreamFactoryInterface)|MessageFactory
*/
private $messageFactory;

public function __construct(ClientInterface $client, MessageFactory $factory = null)
/**
* @param Psr17Factory|MessageFactory|null $factory Passing a MessageFactory is @deprecated
*/
public function __construct(ClientInterface $client, MessageFactory|Psr17Factory $factory = null)
{
$this->client = $client;
$this->messageFactory = $factory ?: MessageFactoryDiscovery::find();
$this->messageFactory = $factory ?? ($client instanceof RequestFactoryInterface && $client instanceof StreamFactoryInterface ? $client : new Psr17Factory());
}

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

protected function getRequest(string $url): RequestInterface
{
return $this->getMessageFactory()->createRequest('GET', $url);
return $this->createRequest('GET', $url);
}

/**
* @param array<string,string|string[]> $headers
*/
protected function createRequest(string $method, string $uri, array $headers = [], string $body = null): RequestInterface
{
if ($this->messageFactory instanceof MessageFactory) {
return $this->messageFactory->createRequest($method, $uri, $headers, $body);
}

$request = $this->messageFactory->createRequest($method, $uri);

foreach ($headers as $name => $value) {
$request = $request->withAddedHeader($name, $value);
}

if (null === $body) {
return $request;
}

$stream = $this->messageFactory->createStream($body);

if ($stream->isSeekable()) {
$stream->seek(0);
}

return $request->withBody($stream);
}

/**
Expand Down Expand Up @@ -94,8 +131,93 @@ protected function getHttpClient(): ClientInterface
return $this->client;
}

/**
* @deprecated Use createRequest instead
*/
protected function getMessageFactory(): MessageFactory
{
return $this->messageFactory;
if ($this->messageFactory instanceof MessageFactory) {
return $this->messageFactory;
}

$factory = $this->messageFactory instanceof ResponseFactoryInterface ? $this->messageFactory : new Psr17Factory();

return new class($factory) implements MessageFactory {
public function __construct(
/**
* @param RequestFactoryInterface&ResponseFactoryInterface&StreamFactoryInterface $factory
*/
private RequestFactoryInterface|ResponseFactoryInterface|StreamFactoryInterface $factory,
) {
}

/**
* @param string $method
* @param string|UriInterface $uri
* @param array<string,string|string[]> $headers
* @param resource|string|StreamInterface|null $body
* @param string $protocolVersion
*/
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface
{
$request = $this->factory->createRequest($method, $uri);

foreach ($headers as $name => $value) {
$request = $request->withAddedHeader($name, $value);
}

if (null !== $body) {
$request = $request->withBody($this->createStream($body));
}

return $request->withProtocolVersion($protocolVersion);
}

/**
* @param int $statusCode
* @param string|null $reasonPhrase
* @param array<string,string|string[]> $headers
* @param resource|string|StreamInterface|null $body
* @param string $protocolVersion
*/
public function createResponse($statusCode = 200, $reasonPhrase = null, array $headers = [], $body = null, $protocolVersion = '1.1'): ResponseInterface
{
$response = $this->factory->createResponse($statusCode, $reasonPhrase);

foreach ($headers as $name => $value) {
$response = $response->withAddedHeader($name, $value);
}

if (null !== $body) {
$response = $response->withBody($this->createStream($body));
}

return $response->withProtocolVersion($protocolVersion);
}

/**
* @param string|resource|StreamInterface|null $body
*/
private function createStream($body = ''): StreamInterface
{
if ($body instanceof StreamInterface) {
return $body;
}

if (\is_string($body ?? '')) {
$stream = $this->factory->createStream($body ?? '');
} elseif (\is_resource($body)) {
$stream = $this->factory->createStreamFromResource($body);
} else {
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body)));
}

if ($stream->isSeekable()) {
$stream->seek(0);
}

return $stream;
}
};
}
}
16 changes: 9 additions & 7 deletions src/Http/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
],
"require": {
"php": "^8.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.6",
"php-http/httplug": "^1.0 || ^2.0",
"php-http/message-factory": "^1.0.2",
"psr/http-message": "^1.0 || ^2.0",
"psr/http-message-implementation": "^1.0",
"php-http/discovery": "^1.17",
"psr/http-client-implementation": "^1.0",
"psr/http-factory-implementation": "^1.0",
"willdurand/geocoder": "^4.0"
},
"require-dev": {
Expand Down Expand Up @@ -48,5 +45,10 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
}
5 changes: 5 additions & 0 deletions src/Plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
2 changes: 1 addition & 1 deletion src/Provider/AlgoliaPlaces/AlgoliaPlaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function getTypes(): array

protected function getRequest(string $url): RequestInterface
{
return $this->getMessageFactory()->createRequest(
return $this->createRequest(
'POST',
$url,
$this->buildHeaders(),
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/AlgoliaPlaces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ You should set a locale on the query. If it is missing, results may not be as co
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;

$httpClient = new \GuzzleHttp\Client();
$httpClient = new \Http\Discovery\Psr18Client();

// Unauthenticated
$provider = new \Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces($httpClient);
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/AlgoliaPlaces/Tests/AlgoliaPlacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Geocoder\Location;
use Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces;
use Geocoder\Query\GeocodeQuery;
use Http\Client\Curl\Client as HttplugClient;
use Http\Discovery\Psr18Client;
use Psr\Http\Client\ClientInterface;

/**
Expand All @@ -35,7 +35,7 @@ protected function getCacheDir(): string
*/
protected function getHttpClient(string $apiKey = null, string $appCode = null): ClientInterface
{
return new CachedResponseClient(new HttplugClient(), $this->getCacheDir(), $apiKey, $appCode);
return new CachedResponseClient(new Psr18Client(), $this->getCacheDir(), $apiKey, $appCode);
}

public function testGeocodeQueryWithLocale(): void
Expand Down
7 changes: 3 additions & 4 deletions src/Provider/AlgoliaPlaces/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
"require": {
"php": "^8.0",
"ext-json": "*",
"geocoder-php/common-http": "^4.1",
"geocoder-php/common-http": "^4.6",
"willdurand/geocoder": "^4.0"
},
"provide": {
"geocoder-php/provider-implementation": "1.0"
},
"require-dev": {
"geocoder-php/provider-integration-tests": "^1.1",
"php-http/curl-client": "^2.2",
"geocoder-php/provider-integration-tests": "^1.6.3",
"php-http/message": "^1.0",
"phpunit/phpunit": "^9.5"
},
Expand All @@ -45,4 +44,4 @@
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
}
}
}
4 changes: 2 additions & 2 deletions src/Provider/ArcGISOnline/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Since a token is required for the `geocodeAddresses` API, the
### Without a token

```php
$httpClient = new \GuzzleHttp\Client();
$httpClient = new \Http\Discovery\Psr18Client();

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

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

```php

$httpClient = new \GuzzleHttp\Client();
$httpClient = new \Http\Discovery\Psr18Client();

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