Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;

use function array_combine;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;

use function sprintf;

#[AsCommand(
Expand All @@ -37,6 +29,7 @@ public function __construct(
private readonly ContainerInterface $container,
private readonly DependencyResolver $dependencyResolver,
private readonly ComposerIntegrationHandler $composerIntegrationHandler,
private readonly InvokeSubCommand $invokeSubCommand,
) {
parent::__construct();
}
Expand All @@ -48,27 +41,15 @@ public function configure(): void
CommandHelper::configureDownloadBuildInstallOptions($this);
}

private function invokeInstallForProject(InputInterface $input, OutputInterface $output): int
{
$originalSuppliedOptions = array_filter($input->getOptions());
$installForProjectInput = new ArrayInput(array_merge(
['command' => 'install-extensions-for-project'],
array_combine(
array_map(static fn ($key) => '--' . $key, array_keys($originalSuppliedOptions)),
array_values($originalSuppliedOptions),
),
));

$application = $this->getApplication();
Assert::notNull($application);

return $application->doRun($installForProjectInput, $output);
}

public function execute(InputInterface $input, OutputInterface $output): int
{
if (! $input->getArgument(CommandHelper::ARG_REQUESTED_PACKAGE_AND_VERSION)) {
return $this->invokeInstallForProject($input, $output);
return ($this->invokeSubCommand)(
$this,
['command' => 'install-extensions-for-project'],
$input,
$output,
);
}

if (! TargetPlatform::isRunningAsRoot()) {
Expand Down
27 changes: 22 additions & 5 deletions src/Command/InstallExtensionsForProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use OutOfRangeException;
use Php\Pie\ComposerIntegration\PieComposerFactory;
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieJsonEditor;
use Php\Pie\ExtensionName;
use Php\Pie\ExtensionType;
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
use Php\Pie\Installing\InstallForPhpProject\FindRootPackage;
use Php\Pie\Installing\InstallForPhpProject\InstallPiePackageFromPath;
use Php\Pie\Installing\InstallForPhpProject\InstallSelectedPackage;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -32,6 +34,8 @@
use function assert;
use function getcwd;
use function in_array;
use function is_string;
use function realpath;
use function sprintf;
use function str_starts_with;
use function strlen;
Expand All @@ -50,6 +54,7 @@ public function __construct(
private readonly FindRootPackage $findRootPackage,
private readonly FindMatchingPackages $findMatchingPackages,
private readonly InstallSelectedPackage $installSelectedPackage,
private readonly InstallPiePackageFromPath $installPiePackageFromPath,
private readonly ContainerInterface $container,
) {
parent::__construct();
Expand All @@ -67,16 +72,28 @@ public function execute(InputInterface $input, OutputInterface $output): int
$helper = $this->getHelper('question');
assert($helper instanceof QuestionHelper);

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

$rootPackage = $this->findRootPackage->forCwd($input, $output);

if (ExtensionType::isValid($rootPackage->getType())) {
$output->writeln('<error>This composer.json is for an extension, installing missing packages is not supported.</error>');

return Command::INVALID;
$cwd = realpath(getcwd());
if (! is_string($cwd) || $cwd === '') {
$output->writeln('<error>Failed to determine current working directory.</error>');

return Command::FAILURE;
}

return ($this->installPiePackageFromPath)(
$this,
$cwd,
$rootPackage,
PieJsonEditor::fromTargetPlatform(CommandHelper::determineTargetPlatformFromInputs($input, new NullOutput())),
$input,
$output,
);
}

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

$output->writeln(sprintf(
'Checking extensions for your project <info>%s</info> (path: %s)',
$rootPackage->getPrettyName(),
Expand Down
44 changes: 44 additions & 0 deletions src/Command/InvokeSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Php\Pie\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;

use function array_combine;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class InvokeSubCommand
{
/** @param array<array-key, mixed> $subCommandInput */
public function __invoke(
Command $command,
array $subCommandInput,
InputInterface $originalCommandInput,
OutputInterface $output,
): int {
$originalSuppliedOptions = array_filter($originalCommandInput->getOptions());
$installForProjectInput = new ArrayInput(array_merge(
$subCommandInput,
array_combine(
array_map(static fn ($key) => '--' . $key, array_keys($originalSuppliedOptions)),
array_values($originalSuppliedOptions),
),
));

$application = $command->getApplication();
Assert::notNull($application);

return $application->doRun($installForProjectInput, $output);
}
}
1 change: 1 addition & 0 deletions src/Installing/InstallForPhpProject/FindRootPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class FindRootPackage
{
public function forCwd(InputInterface $input, OutputInterface $output): RootPackageInterface
Expand Down
53 changes: 53 additions & 0 deletions src/Installing/InstallForPhpProject/InstallPiePackageFromPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Php\Pie\Installing\InstallForPhpProject;

use Composer\Package\RootPackageInterface;
use Php\Pie\Command\InvokeSubCommand;
use Php\Pie\ComposerIntegration\PieJsonEditor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function sprintf;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class InstallPiePackageFromPath
{
/** @psalm-suppress PossiblyUnusedMethod no direct reference; used in service locator */
public function __construct(private readonly InvokeSubCommand $invokeSubCommand)
{
}

/** @param non-empty-string $piePackagePath */
public function __invoke(Command $invokeContext, string $piePackagePath, RootPackageInterface $pieRootPackage, PieJsonEditor $pieJsonEditor, InputInterface $input, OutputInterface $output): int
{
$output->writeln(sprintf('Installing PIE extension from <info>%s</info>', $piePackagePath));
$pieJsonEditor
->ensureExists()
->addRepository('path', $piePackagePath);

try {
return ($this->invokeSubCommand)(
$invokeContext,
[
'command' => 'install',
'requested-package-and-version' => $pieRootPackage->getName() . ':*@dev',
],
$input,
$output,
);
} finally {
$output->writeln(
sprintf(
'Removing temporary path repository: %s',
$piePackagePath,
),
OutputInterface::VERBOSITY_VERBOSE,
);
$pieJsonEditor->removeRepository($piePackagePath);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace Php\PieBehaviourTest\Installing\InstallForPhpProject;

use Composer\Package\RootPackage;
use Php\Pie\Command\InvokeSubCommand;
use Php\Pie\ComposerIntegration\PieJsonEditor;
use Php\Pie\Installing\InstallForPhpProject\InstallPiePackageFromPath;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;

#[CoversClass(InstallPiePackageFromPath::class)]
final class InstallPiePackageFromPathTest extends TestCase
{
private Command&MockObject $command;
private RootPackage $rootPackage;
private InvokeSubCommand&MockObject $invokeSubCommand;
private PieJsonEditor&MockObject $pieJsonEditor;
private InputInterface&MockObject $input;
private BufferedOutput $output;

public function setUp(): void
{
parent::setUp();

$this->command = $this->createMock(Command::class);
$this->rootPackage = new RootPackage('foo/bar', '1.2.3.0', '1.2.3');
$this->invokeSubCommand = $this->createMock(InvokeSubCommand::class);
$this->pieJsonEditor = $this->createMock(PieJsonEditor::class);
$this->input = $this->createMock(InputInterface::class);
$this->output = new BufferedOutput();
}

public function testInvokeWithSuccessfulSubCommand(): void
{
$this->pieJsonEditor
->expects(self::once())
->method('ensureExists')
->willReturnSelf();
$this->pieJsonEditor
->expects(self::once())
->method('addRepository')
->with('path', '/path/to/pie/package');
$this->pieJsonEditor
->expects(self::once())
->method('removeRepository')
->with('/path/to/pie/package');

$this->invokeSubCommand->expects(self::once())
->method('__invoke')
->with(
$this->command,
[
'command' => 'install',
'requested-package-and-version' => 'foo/bar:*@dev',
],
$this->input,
$this->output,
)
->willReturn(Command::SUCCESS);

self::assertSame(
Command::SUCCESS,
(new InstallPiePackageFromPath($this->invokeSubCommand))(
$this->command,
'/path/to/pie/package',
$this->rootPackage,
$this->pieJsonEditor,
$this->input,
$this->output,
),
);
}

public function testInvokeWithSubCommandException(): void
{
$this->pieJsonEditor
->expects(self::once())
->method('ensureExists')
->willReturnSelf();
$this->pieJsonEditor
->expects(self::once())
->method('addRepository')
->with('path', '/path/to/pie/package');

// We still expect the package path to be removed even if there is an exception!
$this->pieJsonEditor
->expects(self::once())
->method('removeRepository')
->with('/path/to/pie/package');

$this->invokeSubCommand->expects(self::once())
->method('__invoke')
->with(
$this->command,
[
'command' => 'install',
'requested-package-and-version' => 'foo/bar:*@dev',
],
$this->input,
$this->output,
)
->willThrowException(new RuntimeException('oh no'));

$install = new InstallPiePackageFromPath($this->invokeSubCommand);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('oh no');
$install(
$this->command,
'/path/to/pie/package',
$this->rootPackage,
$this->pieJsonEditor,
$this->input,
$this->output,
);
}
}
Loading