Skip to content

Commit 9f61af9

Browse files
Skip requiring php-http/message-factory when installing symfony/http-client 6.3+ (#238)
1 parent 5e1ace8 commit 9f61af9

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

.github/workflows/installation.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
method: "Http\\Discovery\\HttpClientDiscovery::find();"
3232
# We should fail if we dont have php-http/message-factory or PSR-17
3333
- expect: cant-find
34-
requirements: "symfony/http-client:^5 php-http/httplug php-http/message-factory guzzlehttp/psr7:^1"
34+
requirements: "symfony/http-client:^5 php-http/httplug guzzlehttp/psr7:^1"
3535
method: "Http\\Discovery\\HttpClientDiscovery::find();"
3636
- expect: cant-find
3737
requirements: "symfony/http-client:^5 php-http/httplug guzzlehttp/psr7:^1 http-interop/http-factory-guzzle"
@@ -58,11 +58,11 @@ jobs:
5858
method: "Http\\Discovery\\Psr18ClientDiscovery::find();"
5959
# Test that we find PSR-18 Symfony 4
6060
- expect: will-find
61-
requirements: "symfony/http-client:^4 php-http/httplug nyholm/psr7:^1.3"
61+
requirements: "symfony/http-client:^4 php-http/httplug php-http/message-factory nyholm/psr7:^1.3"
6262
method: "Http\\Discovery\\Psr18ClientDiscovery::find();"
6363
# Test that we find PSR-18 Symfony 5
6464
- expect: will-find
65-
requirements: "symfony/http-client:^5 php-http/httplug nyholm/psr7:^1.3"
65+
requirements: "symfony/http-client:^5 php-http/httplug php-http/message-factory nyholm/psr7:^1.3"
6666
method: "Http\\Discovery\\Psr18ClientDiscovery::find();"
6767
# Test that we find PSR-17 http-interop
6868
- expect: will-find

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.18.0 - 2023-XX-XX
44

55
- [#235](https://github.com/php-http/discovery/pull/235) - Deprecate HttpClientDiscovery, use Psr18ClientDiscovery instead
6+
- [#238](https://github.com/php-http/discovery/pull/238) - Skip requiring php-http/message-factory when installing symfony/http-client 6.3+
67

78
## 1.17.0 - 2023-04-26
89

src/Composer/Plugin.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ class Plugin implements PluginInterface, EventSubscriberInterface
4545
*/
4646
private const PROVIDE_RULES = [
4747
'php-http/async-client-implementation' => [
48+
'symfony/http-client:>=6.3' => ['guzzlehttp/promises', 'psr/http-factory-implementation'],
4849
'symfony/http-client' => ['guzzlehttp/promises', 'php-http/message-factory', 'psr/http-factory-implementation'],
4950
'php-http/guzzle7-adapter' => [],
5051
'php-http/guzzle6-adapter' => [],
5152
'php-http/curl-client' => [],
5253
'php-http/react-adapter' => [],
5354
],
5455
'php-http/client-implementation' => [
56+
'symfony/http-client:>=6.3' => ['psr/http-factory-implementation'],
5557
'symfony/http-client' => ['php-http/message-factory', 'psr/http-factory-implementation'],
5658
'php-http/guzzle7-adapter' => [],
5759
'php-http/guzzle6-adapter' => [],
@@ -147,8 +149,18 @@ public function postUpdate(Event $event)
147149
$composer->getPackage()->getRequires(),
148150
$composer->getPackage()->getDevRequires(),
149151
];
152+
$pinnedAbstractions = [];
153+
$pinned = $composer->getPackage()->getExtra()['discovery'] ?? [];
154+
foreach (self::INTERFACE_MAP as $abstraction => $interfaces) {
155+
foreach (isset($pinned[$abstraction]) ? [] : $interfaces as $interface) {
156+
if (!isset($pinned[$interface])) {
157+
continue 2;
158+
}
159+
}
160+
$pinnedAbstractions[$abstraction] = true;
161+
}
150162

151-
$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType());
163+
$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType(), $pinnedAbstractions);
152164
$missingRequires = [
153165
'require' => array_fill_keys(array_merge([], ...array_values($missingRequires[0])), '*'),
154166
'require-dev' => array_fill_keys(array_merge([], ...array_values($missingRequires[1])), '*'),
@@ -227,7 +239,7 @@ public function postUpdate(Event $event)
227239
}
228240
}
229241

230-
public function getMissingRequires(InstalledRepositoryInterface $repo, array $requires, bool $isProject): array
242+
public function getMissingRequires(InstalledRepositoryInterface $repo, array $requires, bool $isProject, array $pinnedAbstractions): array
231243
{
232244
$allPackages = [];
233245
$devPackages = method_exists($repo, 'getDevPackageNames') ? array_fill_keys($repo->getDevPackageNames(), true) : [];
@@ -267,7 +279,14 @@ public function getMissingRequires(InstalledRepositoryInterface $repo, array $re
267279
$rules = array_intersect_key(self::PROVIDE_RULES, $rules);
268280

269281
while ($rules) {
270-
$abstractions[] = $abstraction = key($rules);
282+
$abstraction = key($rules);
283+
284+
if (isset($pinnedAbstractions[$abstraction])) {
285+
unset($rules[$abstraction]);
286+
continue;
287+
}
288+
289+
$abstractions[] = $abstraction;
271290

272291
foreach (array_shift($rules) as $candidate => $deps) {
273292
[$candidate, $version] = explode(':', $candidate, 2) + [1 => null];
@@ -332,22 +351,12 @@ public function getMissingRequires(InstalledRepositoryInterface $repo, array $re
332351
}
333352

334353
$dep = key($candidates);
354+
[$dep] = explode(':', $dep, 2);
335355
$missingRequires[$dev][$abstraction] = [$dep];
336356

337357
if ($isProject && !$dev && isset($devPackages[$dep])) {
338358
$missingRequires[2][$abstraction][] = $dep;
339359
}
340-
341-
foreach (current($candidates) as $dep) {
342-
if (isset(self::PROVIDE_RULES[$dep])) {
343-
$abstractions[] = $dep;
344-
} elseif (!isset($allPackages[$dep])) {
345-
$missingRequires[$dev][$abstraction][] = $dep;
346-
} elseif ($isProject && !$dev && isset($devPackages[$dep])) {
347-
$missingRequires[0][$abstraction][] = $dep;
348-
$missingRequires[2][$abstraction][] = $dep;
349-
}
350-
}
351360
}
352361
}
353362

tests/Composer/PluginTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class PluginTest extends TestCase
1717
/**
1818
* @dataProvider provideMissingRequires
1919
*/
20-
public function testMissingRequires(array $expected, InstalledArrayRepository $repo, array $rootRequires, array $rootDevRequires)
20+
public function testMissingRequires(array $expected, InstalledArrayRepository $repo, array $rootRequires, array $rootDevRequires, $pinnedAbstractions = [])
2121
{
2222
$plugin = new Plugin();
2323

24-
$this->assertSame($expected, $plugin->getMissingRequires($repo, [$rootRequires, $rootDevRequires], true));
24+
$this->assertSame($expected, $plugin->getMissingRequires($repo, [$rootRequires, $rootDevRequires], true, $pinnedAbstractions));
2525
}
2626

2727
public static function provideMissingRequires()
@@ -42,8 +42,6 @@ public static function provideMissingRequires()
4242
'psr/http-message-implementation' => [],
4343
'php-http/async-client-implementation' => [
4444
'symfony/http-client',
45-
'guzzlehttp/promises',
46-
'php-http/message-factory',
4745
],
4846
'psr/http-factory-implementation' => [
4947
'nyholm/psr7',
@@ -52,6 +50,10 @@ public static function provideMissingRequires()
5250

5351
yield 'async-httplug' => [$expected, $repo, $rootRequires, []];
5452

53+
unset($expected[0]['php-http/async-client-implementation']);
54+
55+
yield 'pinned' => [$expected, $repo, $rootRequires, [], ['php-http/async-client-implementation' => true]];
56+
5557
$repo = new InstalledArrayRepository([
5658
'php-http/discovery' => new Package('php-http/discovery', '1.0.0.0', '1.0'),
5759
'nyholm/psr7' => new Package('nyholm/psr7', '1.0.0.0', '1.0'),
@@ -60,15 +62,10 @@ public static function provideMissingRequires()
6062

6163
$rootRequires = [
6264
'php-http/discovery' => $link,
63-
'php-http/async-client-implementation' => $link,
65+
'psr/http-factory-implementation' => $link,
6466
];
6567

6668
$expected = [[
67-
'php-http/async-client-implementation' => [
68-
'symfony/http-client',
69-
'guzzlehttp/promises',
70-
'php-http/message-factory',
71-
],
7269
'psr/http-factory-implementation' => [
7370
'nyholm/psr7',
7471
],

0 commit comments

Comments
 (0)