Skip to content

Commit

Permalink
Use Composer in InstalledPiePackages
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Feb 27, 2025
1 parent 405b27b commit 1e14281
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
15 changes: 14 additions & 1 deletion src/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
namespace Php\Pie\Command;

use Php\Pie\BinaryFile;
use Php\Pie\ComposerIntegration\PieComposerFactory;
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieInstalledJsonMetadataKeys;
use Php\Pie\Installing\BinaryFileFailedVerification;
use Php\Pie\Platform\InstalledPiePackages;
use Php\Pie\Platform\OperatingSystem;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

use function array_key_exists;
Expand All @@ -31,6 +35,7 @@ final class ShowCommand extends Command
{
public function __construct(
private readonly InstalledPiePackages $installedPiePackages,
private readonly ContainerInterface $container,
) {
parent::__construct();
}
Expand All @@ -46,7 +51,15 @@ public function execute(InputInterface $input, OutputInterface $output): int
{
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);

$piePackages = $this->installedPiePackages->allPiePackages($targetPlatform);
$composer = PieComposerFactory::createPieComposer(
$this->container,
PieComposerRequest::noOperation(
new NullOutput(),
$targetPlatform,
),
);

$piePackages = $this->installedPiePackages->allPiePackages($composer);
$phpEnabledExtensions = $targetPlatform->phpBinaryPath->extensions();
$extensionPath = $targetPlatform->phpBinaryPath->extensionPath();
$extensionEnding = $targetPlatform->operatingSystem === OperatingSystem::Windows ? '.dll' : '.so';
Expand Down
17 changes: 13 additions & 4 deletions src/Command/UninstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace Php\Pie\Command;

use Composer\Composer;
use Php\Pie\ComposerIntegration\ComposerIntegrationHandler;
use Php\Pie\ComposerIntegration\PieComposerFactory;
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieOperation;
use Php\Pie\DependencyResolver\Package;
use Php\Pie\DependencyResolver\RequestedPackageAndVersion;
use Php\Pie\Platform\InstalledPiePackages;
use Php\Pie\Platform\TargetPlatform;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -57,7 +58,15 @@ public function execute(InputInterface $input, OutputInterface $output): int

$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);

$piePackage = $this->findPiePackageByPackageName($packageToRemove, $targetPlatform);
$composer = PieComposerFactory::createPieComposer(
$this->container,
PieComposerRequest::noOperation(
new NullOutput(),
$targetPlatform,
),
);

$piePackage = $this->findPiePackageByPackageName($packageToRemove, $composer);

if ($piePackage === null) {
$output->writeln('<error>No package found: ' . $packageToRemove . '</error>');
Expand Down Expand Up @@ -88,9 +97,9 @@ public function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function findPiePackageByPackageName(string $packageToRemove, TargetPlatform $targetPlatform): Package|null
private function findPiePackageByPackageName(string $packageToRemove, Composer $composer): Package|null
{
$piePackages = $this->installedPiePackages->allPiePackages($targetPlatform);
$piePackages = $this->installedPiePackages->allPiePackages($composer);

foreach ($piePackages as $piePackage) {
if ($piePackage->name() === $packageToRemove) {
Expand Down
20 changes: 3 additions & 17 deletions src/Platform/InstalledPiePackages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace Php\Pie\Platform;

use Composer\Composer;
use Composer\Package\BasePackage;
use Composer\Package\CompletePackageInterface;
use Php\Pie\ComposerIntegration\PieComposerFactory;
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\DependencyResolver\Package;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Output\NullOutput;

use function array_combine;
use function array_filter;
Expand All @@ -23,31 +20,20 @@
*/
class InstalledPiePackages
{
/** @psalm-suppress PossiblyUnusedMethod no direct reference; used in service locator */
public function __construct(private readonly ContainerInterface $container)
{
}

/**
* Returns a list of PIE packages according to PIE; this does NOT check if
* the extension is actually enabled in the target PHP.
*
* @return ListOfPiePackages
*/
public function allPiePackages(TargetPlatform $targetPlatform): array
public function allPiePackages(Composer $composer): array
{
$composerInstalledPackages = array_map(
static function (CompletePackageInterface $package): Package {
return Package::fromComposerCompletePackage($package);
},
array_filter(
PieComposerFactory::createPieComposer(
$this->container,
PieComposerRequest::noOperation(
new NullOutput(),
$targetPlatform,
),
)
$composer
->getRepositoryManager()
->getLocalRepository()
->getPackages(),
Expand Down
26 changes: 25 additions & 1 deletion test/unit/Platform/InstalledPiePackagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Php\PieUnitTest\Platform;

use Composer\Composer;
use Composer\Package\CompletePackage;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Repository\RepositoryManager;
use Php\Pie\Platform\InstalledPiePackages;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -13,6 +17,26 @@ final class InstalledPiePackagesTest extends TestCase
{
public function testAllPiePackages(): void
{
self::fail('to be implemented'); // @todo implement this test
$localRepo = $this->createMock(InstalledRepositoryInterface::class);
$localRepo->method('getPackages')->willReturn([
new CompletePackage('foo/bar1', '1.2.3.0', '1.2.3'),
new CompletePackage('foo/bar2', '1.2.3.0', '1.2.3'),
]);

$repoManager = $this->createMock(RepositoryManager::class);
$repoManager->method('getLocalRepository')->willReturn($localRepo);

$composer = $this->createMock(Composer::class);
$composer->method('getRepositoryManager')->willReturn($repoManager);

$packages = (new InstalledPiePackages())->allPiePackages($composer);

self::assertArrayHasKey('bar1', $packages);
self::assertArrayHasKey('bar2', $packages);

self::assertSame('bar1', $packages['bar1']->extensionName()->name());
self::assertSame('foo/bar1', $packages['bar1']->name());
self::assertSame('bar2', $packages['bar2']->extensionName()->name());
self::assertSame('foo/bar2', $packages['bar2']->name());
}
}

0 comments on commit 1e14281

Please sign in to comment.