Skip to content

Commit

Permalink
Support both composer 2.2 and 2.3+ on PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ralflang committed Oct 13, 2022
1 parent 316ec72 commit 153ec64
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/HordeInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ class HordeInstaller extends LibraryInstaller
public function reconfigure(): void
{
$mode = \strncasecmp(\PHP_OS, 'WIN', 3) === 0 ? 'copy' : 'symlink';
$flow = HordeReconfigureFlow::fromComposer($this->composer, new ComposerIoAdapter($this->io));
// This is needed to support PHP 7.4 (no union types) for both Composer 2.2 / 2.3
// Cannot use instanceof here as the class will not exist in 2.2.
if (get_class($this->composer) === 'Composer\PartialComposer') {
$flow = HordeReconfigureFlow::fromPartialComposer($this->composer, new ComposerIoAdapter($this->io));
} else {
// @phpstan-ignore-next-line
$flow = HordeReconfigureFlow::fromComposer($this->composer, new ComposerIoAdapter($this->io));
}

$flow->run();
}

Expand Down
8 changes: 7 additions & 1 deletion src/HordeReconfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$composer) {
die('Error: Command was run without a relation to composer itself');
}
$flow = HordeReconfigureFlow::fromComposer($composer, new SymphonyOutputAdapter($output));
// This is needed to support PHP 7.4 (no union types) for both Composer 2.2 / 2.3
// Cannot use instanceof here as the class will not exist in 2.2.
if (get_class($composer) === 'Composer\PartialComposer') {
$flow = HordeReconfigureFlow::fromPartialComposer($composer, new SymphonyOutputAdapter($output));
} else {
$flow = HordeReconfigureFlow::fromComposer($composer, new SymphonyOutputAdapter($output));
}
return $flow->run();
}
}
31 changes: 30 additions & 1 deletion src/HordeReconfigureFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Composer\InstalledVersions;
use Composer\PartialComposer;
use Composer\Composer;
use Composer\Util\Filesystem;
use Composer\Factory as ComposerFactory;
use Horde\Composer\IOAdapter\FlowIoInterface;
Expand All @@ -35,7 +36,35 @@ public function __construct(DirectoryTree $tree, FlowIoInterface $io, string $mo
$this->tree = $tree;
}

public static function fromComposer(PartialComposer $composer, ?FlowIoInterface $output = null): self
/**
* Named Constructor.
*
* @param Composer $composer
* @param FlowIoInterface|null $output
* @return self
*/
public static function fromComposer(Composer $composer, ?FlowIoInterface $output = null): self
{
return self::fromAnyComposer($composer, $output);
}

public static function fromPartialComposer(PartialComposer $composer, ?FlowIoInterface $output = null): self
{
return self::fromAnyComposer($composer, $output);
}

/**
* Actual implementation of fromComposer / fromPartialComposer named constructors
*
* Composer may provide a PartialCompoer or a Composer object.
* Use fromComposer and fromPartialComposer frontends
*
* @param Composer|PartialComposer $composer A (partial) composer instance
* @param FlowIoInterface|null $output An IO interface
*
* @TODO Refactor this once we require PHP 8.0 or higher
*/
private static function fromAnyComposer($composer, ?FlowIoInterface $output = null): self
{
$mode = \strncasecmp(\PHP_OS, 'WIN', 3) === 0 ? 'copy' : 'symlink';
$tree = DirectoryTree::fromComposerJsonPath(ComposerFactory::getComposerFile());
Expand Down

0 comments on commit 153ec64

Please sign in to comment.