Skip to content

feat(php): Restore Guzzle as a suggestion #888

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 6 commits into from
Aug 3, 2022
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
25 changes: 22 additions & 3 deletions clients/algoliasearch-client-php/lib/Algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,29 @@ public static function setLogger(LoggerInterface $logger)

public static function getHttpClient()
{
$guzzleVersion = null;
if (interface_exists('\GuzzleHttp\ClientInterface')) {
if (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int) mb_substr(
\GuzzleHttp\Client::VERSION,
0,
1
);
} else {
$guzzleVersion = \GuzzleHttp\ClientInterface::MAJOR_VERSION;
}
}

if (null === self::$httpClient) {
self::setHttpClient(
new \Algolia\AlgoliaSearch\Http\GuzzleHttpClient()
);
if (class_exists('\GuzzleHttp\Client') && 6 <= $guzzleVersion) {
self::setHttpClient(
new \Algolia\AlgoliaSearch\Http\GuzzleHttpClient()
);
} else {
self::setHttpClient(
new \Algolia\AlgoliaSearch\Http\CurlHttpClient()
);
}
}

return self::$httpClient;
Expand Down
151 changes: 151 additions & 0 deletions clients/algoliasearch-client-php/lib/Http/CurlHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me think that our CTS should include tests for all of the requesters we expose, could you please add a task for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also for the JS client, I've imported tests from the previous major to ensure it still work properly, do we have tests for this one? It could be a great addition in the meantime of the CTS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created task : APIC-607


namespace Algolia\AlgoliaSearch\Http;

use Algolia\AlgoliaSearch\Http\Psr7\Response;
use Psr\Http\Message\RequestInterface;

final class CurlHttpClient implements HttpClientInterface
{
private $curlMHandle;

private $curlOptions;

public function __construct($curlOptions = [])
{
$this->curlOptions = $curlOptions;
}

public function sendRequest(
RequestInterface $request,
$timeout,
$connectTimeout
) {
$curlHandle = curl_init();

// set curl options
try {
foreach ($this->curlOptions as $curlOption => $optionValue) {
curl_setopt($curlHandle, constant($curlOption), $optionValue);
}
} catch (\Exception $e) {
$this->invalidOptions($this->curlOptions, $e->getMessage());
}

$curlHeaders = [];
foreach ($request->getHeaders() as $key => $values) {
$curlHeaders[] = $key . ': ' . implode(',', $values);
}

curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);

curl_setopt(
$curlHandle,
CURLOPT_USERAGENT,
implode(',', $request->getHeader('User-Agent'))
);
//Return the output instead of printing it
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_FAILONERROR, true);
curl_setopt($curlHandle, CURLOPT_ENCODING, '');
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
// TODO: look into cert
// curl_setopt($curlHandle, CURLOPT_CAINFO, $this->caInfoPath);

curl_setopt($curlHandle, CURLOPT_URL, (string) $request->getUri());
$version = curl_version();
if (
version_compare($version['version'], '7.16.2', '>=') &&
$connectTimeout < 1
) {
curl_setopt(
$curlHandle,
CURLOPT_CONNECTTIMEOUT_MS,
$connectTimeout * 1000
);
curl_setopt($curlHandle, CURLOPT_TIMEOUT_MS, $timeout * 1000);
} else {
curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, $timeout);
}

// The problem is that on (Li|U)nix, when libcurl uses the standard name resolver,
// a SIGALRM is raised during name resolution which libcurl thinks is the timeout alarm.
curl_setopt($curlHandle, CURLOPT_NOSIGNAL, 1);
curl_setopt($curlHandle, CURLOPT_FAILONERROR, false);

$method = $request->getMethod();
if ('GET' === $method) {
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curlHandle, CURLOPT_HTTPGET, true);
curl_setopt($curlHandle, CURLOPT_POST, false);
} else {
if ('POST' === $method) {
$body = (string) $request->getBody();
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $body);
} elseif ('DELETE' === $method) {
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($curlHandle, CURLOPT_POST, false);
} elseif ('PUT' === $method) {
$body = (string) $request->getBody();
curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $body);
curl_setopt($curlHandle, CURLOPT_POST, true);
}
}
$mhandle = $this->getMHandle($curlHandle);

// Do all the processing.
$running = null;
do {
$mrc = curl_multi_exec($mhandle, $running);
} while (CURLM_CALL_MULTI_PERFORM === $mrc);

while ($running && CURLM_OK === $mrc) {
if (-1 === curl_multi_select($mhandle, 0.1)) {
usleep(100);
}
do {
$mrc = curl_multi_exec($mhandle, $running);
} while (CURLM_CALL_MULTI_PERFORM === $mrc);
}

$statusCode = (int) curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
$responseBody = curl_multi_getcontent($curlHandle);
$error = curl_error($curlHandle);

$this->releaseMHandle($curlHandle);
curl_close($curlHandle);

return new Response($statusCode, [], $responseBody, '1.1', $error);
}

private function getMHandle($curlHandle)
{
if (!is_resource($this->curlMHandle)) {
$this->curlMHandle = curl_multi_init();
}
curl_multi_add_handle($this->curlMHandle, $curlHandle);

return $this->curlMHandle;
}

private function releaseMHandle($curlHandle)
{
curl_multi_remove_handle($this->curlMHandle, $curlHandle);
}

private function invalidOptions(array $curlOptions = [], $errorMsg = '')
{
throw new \OutOfBoundsException(
sprintf(
'AlgoliaSearch curloptions options keys are invalid. %s given. error message : %s',
json_encode($curlOptions),
$errorMsg
)
);
}
}
3 changes: 2 additions & 1 deletion config/generation.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ module.exports = {
'!clients/algoliasearch-client-javascript/packages/algoliasearch/jest.config.ts',

// PHP
'!clients/algoliasearch-client-php/*',
'!clients/algoliasearch-client-php/**',
'clients/algoliasearch-client-php/lib/Api/*',
'clients/algoliasearch-client-php/lib/Model/**',
'clients/algoliasearch-client-php/lib/Configuration/*',
'clients/algoliasearch-client-php/lib/ApiException.php',
'clients/algoliasearch-client-php/lib/ObjectSerializer.php',
'clients/algoliasearch-client-php/composer.json',
],
};
4 changes: 3 additions & 1 deletion templates/php/composer.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^2.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
Expand All @@ -47,5 +46,8 @@
},
"autoload-dev": {
"psr-4": { "{{escapedInvokerPackage}}\\Test\\" : "{{testBasePath}}/" }
},
"suggest": {
"guzzlehttp/guzzle": "If you prefer to use Guzzle HTTP client instead of the Http Client implementation provided by the package"
}
}