Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on Keep a Changelog.
- Added adapter-based Lang provider support with `DeepL` and `Google Translate` adapters plus shared remote request/caching infrastructure (#533)

### Changed
- Refactored `HttpClient` internals behind explicit `CurlAdapter` and `MultiCurlAdapter` wrappers while preserving the existing facade methods and keeping `php-curl-class` as the underlying transport for this phase (#534)
- Refactored the Lang package to resolve adapter instances through `LangFactory` configuration and load file translations lazily on first use instead of preloading them during web boot (#533)
- **BREAKING:** Reshaped Lang configuration so `lang.default` now selects the adapter, locale fallback moved to `lang.default_locale`, and the unused `lang.enabled` toggle was removed (#533)
- **BREAKING:** Removed `Lang::isEnabled()` from the public Lang API because it no longer affected runtime behavior (#533)
Expand Down
166 changes: 166 additions & 0 deletions src/HttpClient/Adapters/CurlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

declare(strict_types=1);

/**
* Quantum PHP Framework
* An open-source software development framework for PHP
* @link https://quantumphp.io
*/

namespace Quantum\HttpClient\Adapters;

use Quantum\HttpClient\Contracts\CurlAdapterInterface;
use Curl\Curl;

/**
* Class CurlAdapter
* @package Quantum\HttpClient
*/
class CurlAdapter implements CurlAdapterInterface
{
private Curl $client;

public function __construct(?Curl $client = null)
{
$this->client = $client ?? new Curl();
}

public function setUrl(string $url): CurlAdapterInterface
{
$this->client->setUrl($url);

return $this;
}

/**
* @param mixed $value
*/
public function setOpt(int $option, $value): CurlAdapterInterface
{
$this->client->setOpt($option, $value);

return $this;
}

/**
* @param array<int, mixed> $options
*/
public function setOpts(array $options): CurlAdapterInterface
{
$this->client->setOpts($options);

return $this;
}

/**
* @param mixed $value
*/
public function setHeader(string $key, $value): CurlAdapterInterface
{
$this->client->setHeader($key, $value);

return $this;
}

/**
* @param array<int|string, mixed> $headers
*/
public function setHeaders(array $headers): CurlAdapterInterface
{
$this->client->setHeaders($headers);

return $this;
}

/**
* @param mixed $data
* @return mixed
*/
public function buildPostData($data)
{
return $this->client->buildPostData($data);
}

/**
* @return mixed
*/
public function start()
{
return $this->client->exec();
}

/**
* @return int|string
*/
public function getId()
{
return $this->client->getId();
}

public function isError(): bool
{
return $this->client->isError();
}

public function getErrorCode(): int
{
return $this->client->getErrorCode();
}

public function getErrorMessage(): ?string
{
return $this->client->getErrorMessage();
}

/**
* @return iterable<string, mixed>
*/
public function getResponseHeaders(): iterable
{
return $this->client->getResponseHeaders();
}

/**
* @return mixed
*/
public function getResponseCookies()
{
return $this->client->getResponseCookies();
}

/**
* @return mixed
*/
public function getResponse()
{
return $this->client->getResponse();
}

/**
* @return mixed
*/
public function getInfo(?int $option = null)
{
return $option !== null ? $this->client->getInfo($option) : $this->client->getInfo();
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public function getUrl(): ?string
{
return $this->client->getUrl();
}

public function supportsMethod(string $method): bool
{
return method_exists($this->client, $method);
}

/**
* @param array<mixed> $arguments
* @return mixed
*/
public function callMethod(string $method, array $arguments)
{
return $this->client->$method(...$arguments);
}
}
145 changes: 145 additions & 0 deletions src/HttpClient/Adapters/MultiCurlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

declare(strict_types=1);

/**
* Quantum PHP Framework
* An open-source software development framework for PHP
* @link https://quantumphp.io
*/

namespace Quantum\HttpClient\Adapters;

use Quantum\HttpClient\Contracts\MultiCurlAdapterInterface;
use Curl\MultiCurl;
use Curl\Curl;

/**
* Class MultiCurlAdapter
* @package Quantum\HttpClient
*/
class MultiCurlAdapter implements MultiCurlAdapterInterface
{
private MultiCurl $client;

public function __construct(?MultiCurl $client = null)
{
$this->client = $client ?? new MultiCurl();
}

public function complete(callable $callback): MultiCurlAdapterInterface
{
$this->client->complete(function (Curl $instance) use ($callback): void {
$callback(new CurlAdapter($instance));
});

return $this;
}

public function success(callable $callback): MultiCurlAdapterInterface
{
$this->client->success(function (Curl $instance) use ($callback): void {
$callback(new CurlAdapter($instance));
});

return $this;
}

public function error(callable $callback): MultiCurlAdapterInterface
{
$this->client->error(function (Curl $instance) use ($callback): void {
$callback(new CurlAdapter($instance));
});

return $this;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* @return mixed
*/
public function start()
{
return $this->client->start();
}

/**
* @param array<string, mixed> $data
* @return mixed
*/
public function addGet(string $url, array $data = [])
{
return $this->wrapCurlResult($this->client->addGet($url, $data));
}

/**
* @param mixed $data
* @return mixed
*/
public function addPost(string $url, $data = '', bool $follow_303_with_post = false)
{
return $this->wrapCurlResult($this->client->addPost($url, $data, $follow_303_with_post));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* @param mixed $value
*/
public function setHeader(string $key, $value): MultiCurlAdapterInterface
{
$this->client->setHeader($key, $value);

return $this;
}

/**
* @param array<int|string, mixed> $headers
*/
public function setHeaders(array $headers): MultiCurlAdapterInterface
{
$this->client->setHeaders($headers);

return $this;
}

/**
* @param mixed $value
*/
public function setOpt(int $option, $value): MultiCurlAdapterInterface
{
$this->client->setOpt($option, $value);

return $this;
}

/**
* @param array<int, mixed> $options
*/
public function setOpts(array $options): MultiCurlAdapterInterface
{
$this->client->setOpts($options);

return $this;
}

public function supportsMethod(string $method): bool
{
return method_exists($this->client, $method);
}

/**
* @param array<mixed> $arguments
* @return mixed
*/
public function callMethod(string $method, array $arguments)
{
return $this->client->$method(...$arguments);
}

/**
* @param mixed $result
* @return mixed
*/
private function wrapCurlResult($result)
{
return $result instanceof Curl ? new CurlAdapter($result) : $result;
}
}
Loading
Loading