Skip to content

Commit

Permalink
Skip auto-installing when the root package's extra.discovery is enough (
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored May 3, 2023
1 parent 1c44ede commit 29ae6fa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
jq '.repositories[0].url="'$(pwd)'"' tests/plugin/pinning/composer.json > /tmp/plugin-pinning/composer.json
cd /tmp/plugin-pinning
composer update
[ 'Slim\Psr7\Factory\RequestFactory' == $(php test.php) ]
[ 'Slim\Psr7\Factory\RequestFactory-MyClient' == $(php test.php) ]
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Change Log

## 1.18.0 - 2023-XX-XX
## 1.18.0 - 2023-05-03

- [#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+
- [#239](https://github.com/php-http/discovery/pull/239) - Skip auto-installing when the root package's extra.discovery is enough

## 1.17.0 - 2023-04-26

Expand Down
23 changes: 20 additions & 3 deletions src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,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 @@ -229,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 @@ -269,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
8 changes: 6 additions & 2 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 Down Expand Up @@ -50,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 Down
3 changes: 3 additions & 0 deletions tests/plugin/pinning/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"require": {
"nyholm/psr7": "*",
"php-http/discovery": "99.99.x-dev",
"psr/http-client": "*",
"psr/http-client-implementation": "*",
"slim/psr7": "*"
},
"config": {
Expand All @@ -22,6 +24,7 @@
},
"extra": {
"discovery": {
"Psr\\Http\\Client\\ClientInterface": "MyClient",
"Psr\\Http\\Message\\RequestFactoryInterface": "Slim\\Psr7\\Factory\\RequestFactory"
}
}
Expand Down
16 changes: 15 additions & 1 deletion tests/plugin/pinning/test.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
<?php

use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

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

echo get_class(Psr17FactoryDiscovery::findRequestFactory())."\n";
class MyClient implements ClientInterface
{
public function sendRequest(RequestInterface $request): ResponseInterface
{
return Psr17FactoryDiscovery::findResponseFactory()->createResponse();
}
}

echo get_class(Psr17FactoryDiscovery::findRequestFactory());
echo '-', get_class(Psr18ClientDiscovery::find());
echo "\n";

0 comments on commit 29ae6fa

Please sign in to comment.