Skip to content

Commit

Permalink
Use composer InstalledRepository to determine install dir
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 20, 2024
1 parent da8b3a7 commit b4fdc1b
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/ExtensionInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,48 @@
use Composer\Installer\LibraryInstaller;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Repository\InstalledRepository;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Repository\RootPackageRepository;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use React\Promise\PromiseInterface;

abstract class ExtensionInstaller extends LibraryInstaller
{
private $roundcubemailInstallPath;

protected $composer_type;

protected function getRoundcubemailInstallPath(): string
protected function setRoundcubemailInstallPath(InstalledRepositoryInterface $installedRepo): void
{
$rootPackage = $this->composer->getPackage();
if ($rootPackage->getName() === 'roundcube/roundcubemail') {
// https://github.com/composer/composer/discussions/11927#discussioncomment-9116893
$rootPackage = clone $this->composer->getPackage();
$installedRepo = new InstalledRepository([
$installedRepo,
new RootPackageRepository($rootPackage),
]);

$roundcubemailPackages = $installedRepo->findPackagesWithReplacersAndProviders('roundcube/roundcubemail');
assert(count($roundcubemailPackages) === 1);
$roundcubemailPackage = $roundcubemailPackages[0];

if ($roundcubemailPackage === $rootPackage) { // $this->getInstallPath($package) does not work for root package
$this->initializeVendorDir();

return dirname($this->vendorDir);
$this->roundcubemailInstallPath = dirname($this->vendorDir);
} else {
$this->roundcubemailInstallPath = $this->getInstallPath($roundcubemailPackage);
}
}

$roundcubemailPackage = $this->composer
->getRepositoryManager()
->findPackage('roundcube/roundcubemail', '*');

return $this->getInstallPath($roundcubemailPackage);
protected function getRoundcubemailInstallPath(): string
{
return $this->roundcubemailInstallPath;
}

public function getInstallPath(PackageInterface $package)
{
if (!$this->supports($package->getType())) {
if (!$this->supports($package->getType()) || $this->roundcubemailInstallPath === null /* install path is not known at download phase */) {
return parent::getInstallPath($package);
}

Expand All @@ -44,6 +58,7 @@ public function getInstallPath(PackageInterface $package)
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
// initialize Roundcube environment
$this->setRoundcubemailInstallPath($repo);
if (!defined('INSTALL_PATH')) {
define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/');
}
Expand Down Expand Up @@ -114,6 +129,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
{
// initialize Roundcube environment
$this->setRoundcubemailInstallPath($repo);
if (!defined('INSTALL_PATH')) {
define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/');
}
Expand Down Expand Up @@ -187,6 +203,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
{
// initialize Roundcube environment
$this->setRoundcubemailInstallPath($repo);
if (!defined('INSTALL_PATH')) {
define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/');
}
Expand Down

0 comments on commit b4fdc1b

Please sign in to comment.