Skip to content

Commit 46b649c

Browse files
Skip auto-installing when the root package's extra.discovery is enough
1 parent 1c44ede commit 46b649c

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

.github/workflows/plugin.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ jobs:
4242
jq '.repositories[0].url="'$(pwd)'"' tests/plugin/pinning/composer.json > /tmp/plugin-pinning/composer.json
4343
cd /tmp/plugin-pinning
4444
composer update
45-
[ 'Slim\Psr7\Factory\RequestFactory' == $(php test.php) ]
45+
[ 'Slim\Psr7\Factory\RequestFactory MyClient' == $(php test.php) ]

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Change Log
22

3-
## 1.18.0 - 2023-XX-XX
3+
## 1.18.0 - 2023-05-03
44

55
- [#235](https://github.com/php-http/discovery/pull/235) - Deprecate HttpClientDiscovery, use Psr18ClientDiscovery instead
66
- [#238](https://github.com/php-http/discovery/pull/238) - Skip requiring php-http/message-factory when installing symfony/http-client 6.3+
7+
- [#239](https://github.com/php-http/discovery/pull/239) - Skip auto-installing when the root package's extra.discovery is enough
78

89
## 1.17.0 - 2023-04-26
910

src/Composer/Plugin.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,18 @@ public function postUpdate(Event $event)
149149
$composer->getPackage()->getRequires(),
150150
$composer->getPackage()->getDevRequires(),
151151
];
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+
}
152162

153-
$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType());
163+
$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType(), $pinnedAbstractions);
154164
$missingRequires = [
155165
'require' => array_fill_keys(array_merge([], ...array_values($missingRequires[0])), '*'),
156166
'require-dev' => array_fill_keys(array_merge([], ...array_values($missingRequires[1])), '*'),
@@ -229,7 +239,7 @@ public function postUpdate(Event $event)
229239
}
230240
}
231241

232-
public function getMissingRequires(InstalledRepositoryInterface $repo, array $requires, bool $isProject): array
242+
public function getMissingRequires(InstalledRepositoryInterface $repo, array $requires, bool $isProject, array $pinnedAbstractions): array
233243
{
234244
$allPackages = [];
235245
$devPackages = method_exists($repo, 'getDevPackageNames') ? array_fill_keys($repo->getDevPackageNames(), true) : [];
@@ -269,7 +279,14 @@ public function getMissingRequires(InstalledRepositoryInterface $repo, array $re
269279
$rules = array_intersect_key(self::PROVIDE_RULES, $rules);
270280

271281
while ($rules) {
272-
$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;
273290

274291
foreach (array_shift($rules) as $candidate => $deps) {
275292
[$candidate, $version] = explode(':', $candidate, 2) + [1 => null];

tests/Composer/PluginTest.php

+6-2
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()
@@ -50,6 +50,10 @@ public static function provideMissingRequires()
5050

5151
yield 'async-httplug' => [$expected, $repo, $rootRequires, []];
5252

53+
unset($expected[0]['php-http/async-client-implementation']);
54+
55+
yield 'pinned' => [$expected, $repo, $rootRequires, [], ['php-http/async-client-implementation' => true]];
56+
5357
$repo = new InstalledArrayRepository([
5458
'php-http/discovery' => new Package('php-http/discovery', '1.0.0.0', '1.0'),
5559
'nyholm/psr7' => new Package('nyholm/psr7', '1.0.0.0', '1.0'),

tests/plugin/pinning/composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"require": {
1414
"nyholm/psr7": "*",
1515
"php-http/discovery": "99.99.x-dev",
16+
"psr/http-client": "*",
17+
"psr/http-client-implementation": "*",
1618
"slim/psr7": "*"
1719
},
1820
"config": {
@@ -22,6 +24,7 @@
2224
},
2325
"extra": {
2426
"discovery": {
27+
"Psr\\Http\\Client\\ClientInterface": "MyClient",
2528
"Psr\\Http\\Message\\RequestFactoryInterface": "Slim\\Psr7\\Factory\\RequestFactory"
2629
}
2730
}

tests/plugin/pinning/test.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
<?php
22

33
use Http\Discovery\Psr17FactoryDiscovery;
4+
use Http\Discovery\Psr18ClientDiscovery;
5+
use Psr\Http\Client\ClientInterface;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
48

59
require __DIR__.'/vendor/autoload.php';
610

7-
echo get_class(Psr17FactoryDiscovery::findRequestFactory())."\n";
11+
class MyClient implements ClientInterface
12+
{
13+
public function sendRequest(RequestInterface $request): ResponseInterface
14+
{
15+
return Psr17FactoryDiscovery::findResponseFactory()->createResponse();
16+
}
17+
}
18+
19+
echo get_class(Psr17FactoryDiscovery::findRequestFactory());
20+
echo ' ', get_class(Psr18ClientDiscovery::find());
21+
echo "\n";

0 commit comments

Comments
 (0)