-
Notifications
You must be signed in to change notification settings - Fork 522
Replace HTTPlug factories by PSR-17 #1184
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
|
@@ -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()); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like an issue while rebasing. Please remove this line and support php8 only.