Skip to content

Commit e3d22b8

Browse files
committed
made HttpClientFactory a service
1 parent 2a6b882 commit e3d22b8

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/Command/BisectCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107107
return 1;
108108
}
109109

110-
$client = HttpClientFactory::createClient([
110+
$client = (new HttpClientFactory())->createClient([
111111
RequestOptions::TIMEOUT => 30,
112112
RequestOptions::CONNECT_TIMEOUT => 10,
113113
'headers' => [

src/Command/FixerApplication.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function __construct(
9393
private ?string $editorUrl,
9494
#[AutowiredParameter]
9595
private string $usedLevel,
96+
private HttpClientFactory $httpClientFactory,
9697
)
9798
{
9899
}
@@ -325,7 +326,7 @@ private function downloadPhar(
325326
$output->writeln('<fg=green>Checking if there\'s a new PHPStan Pro release...</>');
326327
}
327328

328-
$client = HttpClientFactory::createClient([
329+
$client = $this->httpClientFactory->createClient([
329330
RequestOptions::TIMEOUT => 30,
330331
RequestOptions::CONNECT_TIMEOUT => 5,
331332
]);

src/Internal/HttpClientFactory.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
namespace PHPStan\Internal;
44

55
use GuzzleHttp\Client;
6+
use PHPStan\DependencyInjection\AutowiredService;
67
use function extension_loaded;
78

9+
#[AutowiredService]
810
final class HttpClientFactory
911
{
1012

13+
/**
14+
* @param array<mixed> $defaults
15+
*
16+
* @see \GuzzleHttp\RequestOptions
17+
*/
18+
public function __construct(private readonly array $defaults = [])
19+
{
20+
}
21+
1122
/**
1223
* @param array<mixed> $config
1324
*
1425
* @see \GuzzleHttp\RequestOptions
1526
*/
16-
public static function createClient(array $config): Client
27+
public function createClient(array $config): Client
1728
{
1829
if (
1930
!isset($config['headers']['Accept-Encoding'])
@@ -22,7 +33,7 @@ public static function createClient(array $config): Client
2233
$config['headers']['Accept-Encoding'] = 'gzip,deflate';
2334
}
2435

25-
return new Client($config);
36+
return new Client($config + $this->defaults);
2637
}
2738

2839
}

0 commit comments

Comments
 (0)