Skip to content

Commit

Permalink
Skip requiring php-http/message-factory when installing symfony/http-…
Browse files Browse the repository at this point in the history
…client 6.3+ (#238)
  • Loading branch information
nicolas-grekas committed May 3, 2023
1 parent 5e1ace8 commit 9f61af9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
method: "Http\\Discovery\\HttpClientDiscovery::find();"
# We should fail if we dont have php-http/message-factory or PSR-17
- expect: cant-find
requirements: "symfony/http-client:^5 php-http/httplug php-http/message-factory guzzlehttp/psr7:^1"
requirements: "symfony/http-client:^5 php-http/httplug guzzlehttp/psr7:^1"
method: "Http\\Discovery\\HttpClientDiscovery::find();"
- expect: cant-find
requirements: "symfony/http-client:^5 php-http/httplug guzzlehttp/psr7:^1 http-interop/http-factory-guzzle"
Expand All @@ -58,11 +58,11 @@ jobs:
method: "Http\\Discovery\\Psr18ClientDiscovery::find();"
# Test that we find PSR-18 Symfony 4
- expect: will-find
requirements: "symfony/http-client:^4 php-http/httplug nyholm/psr7:^1.3"
requirements: "symfony/http-client:^4 php-http/httplug php-http/message-factory nyholm/psr7:^1.3"
method: "Http\\Discovery\\Psr18ClientDiscovery::find();"
# Test that we find PSR-18 Symfony 5
- expect: will-find
requirements: "symfony/http-client:^5 php-http/httplug nyholm/psr7:^1.3"
requirements: "symfony/http-client:^5 php-http/httplug php-http/message-factory nyholm/psr7:^1.3"
method: "Http\\Discovery\\Psr18ClientDiscovery::find();"
# Test that we find PSR-17 http-interop
- expect: will-find
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.18.0 - 2023-XX-XX

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

## 1.17.0 - 2023-04-26

Expand Down
37 changes: 23 additions & 14 deletions src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ class Plugin implements PluginInterface, EventSubscriberInterface
*/
private const PROVIDE_RULES = [
'php-http/async-client-implementation' => [
'symfony/http-client:>=6.3' => ['guzzlehttp/promises', 'psr/http-factory-implementation'],
'symfony/http-client' => ['guzzlehttp/promises', 'php-http/message-factory', 'psr/http-factory-implementation'],
'php-http/guzzle7-adapter' => [],
'php-http/guzzle6-adapter' => [],
'php-http/curl-client' => [],
'php-http/react-adapter' => [],
],
'php-http/client-implementation' => [
'symfony/http-client:>=6.3' => ['psr/http-factory-implementation'],
'symfony/http-client' => ['php-http/message-factory', 'psr/http-factory-implementation'],
'php-http/guzzle7-adapter' => [],
'php-http/guzzle6-adapter' => [],
Expand Down Expand Up @@ -147,8 +149,18 @@ public function postUpdate(Event $event)
$composer->getPackage()->getRequires(),
$composer->getPackage()->getDevRequires(),
];
$pinnedAbstractions = [];
$pinned = $composer->getPackage()->getExtra()['discovery'] ?? [];
foreach (self::INTERFACE_MAP as $abstraction => $interfaces) {
foreach (isset($pinned[$abstraction]) ? [] : $interfaces as $interface) {
if (!isset($pinned[$interface])) {
continue 2;
}
}
$pinnedAbstractions[$abstraction] = true;
}

$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType());
$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType(), $pinnedAbstractions);
$missingRequires = [
'require' => array_fill_keys(array_merge([], ...array_values($missingRequires[0])), '*'),
'require-dev' => array_fill_keys(array_merge([], ...array_values($missingRequires[1])), '*'),
Expand Down Expand Up @@ -227,7 +239,7 @@ public function postUpdate(Event $event)
}
}

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

while ($rules) {
$abstractions[] = $abstraction = key($rules);
$abstraction = key($rules);

if (isset($pinnedAbstractions[$abstraction])) {
unset($rules[$abstraction]);
continue;
}

$abstractions[] = $abstraction;

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

$dep = key($candidates);
[$dep] = explode(':', $dep, 2);
$missingRequires[$dev][$abstraction] = [$dep];

if ($isProject && !$dev && isset($devPackages[$dep])) {
$missingRequires[2][$abstraction][] = $dep;
}

foreach (current($candidates) as $dep) {
if (isset(self::PROVIDE_RULES[$dep])) {
$abstractions[] = $dep;
} elseif (!isset($allPackages[$dep])) {
$missingRequires[$dev][$abstraction][] = $dep;
} elseif ($isProject && !$dev && isset($devPackages[$dep])) {
$missingRequires[0][$abstraction][] = $dep;
$missingRequires[2][$abstraction][] = $dep;
}
}
}
}

Expand Down
17 changes: 7 additions & 10 deletions tests/Composer/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class PluginTest extends TestCase
/**
* @dataProvider provideMissingRequires
*/
public function testMissingRequires(array $expected, InstalledArrayRepository $repo, array $rootRequires, array $rootDevRequires)
public function testMissingRequires(array $expected, InstalledArrayRepository $repo, array $rootRequires, array $rootDevRequires, $pinnedAbstractions = [])
{
$plugin = new Plugin();

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

public static function provideMissingRequires()
Expand All @@ -42,8 +42,6 @@ public static function provideMissingRequires()
'psr/http-message-implementation' => [],
'php-http/async-client-implementation' => [
'symfony/http-client',
'guzzlehttp/promises',
'php-http/message-factory',
],
'psr/http-factory-implementation' => [
'nyholm/psr7',
Expand All @@ -52,6 +50,10 @@ public static function provideMissingRequires()

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

unset($expected[0]['php-http/async-client-implementation']);

yield 'pinned' => [$expected, $repo, $rootRequires, [], ['php-http/async-client-implementation' => true]];

$repo = new InstalledArrayRepository([
'php-http/discovery' => new Package('php-http/discovery', '1.0.0.0', '1.0'),
'nyholm/psr7' => new Package('nyholm/psr7', '1.0.0.0', '1.0'),
Expand All @@ -60,15 +62,10 @@ public static function provideMissingRequires()

$rootRequires = [
'php-http/discovery' => $link,
'php-http/async-client-implementation' => $link,
'psr/http-factory-implementation' => $link,
];

$expected = [[
'php-http/async-client-implementation' => [
'symfony/http-client',
'guzzlehttp/promises',
'php-http/message-factory',
],
'psr/http-factory-implementation' => [
'nyholm/psr7',
],
Expand Down

0 comments on commit 9f61af9

Please sign in to comment.