From 4d910810cd0cef3d5294ca485e78e4494528b61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 20 Apr 2024 21:40:03 +0200 Subject: [PATCH] Use composer InstalledRepository to determine install dir --- src/ExtensionInstaller.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ExtensionInstaller.php b/src/ExtensionInstaller.php index 33fff66..dff6d01 100644 --- a/src/ExtensionInstaller.php +++ b/src/ExtensionInstaller.php @@ -5,6 +5,7 @@ use Composer\Installer\LibraryInstaller; use Composer\Package\PackageInterface; use Composer\Package\Version\VersionParser; +use Composer\Repository\InstalledRepository; use Composer\Repository\InstalledRepositoryInterface; use Composer\Util\Filesystem; use Composer\Util\ProcessExecutor; @@ -12,27 +13,30 @@ abstract class ExtensionInstaller extends LibraryInstaller { + private $roundcubemailInstallPath; + protected $composer_type; - protected function getRoundcubemailInstallPath(): string + protected function setRoundcubemailInstallPath(InstalledRepositoryInterface $repo): void { - $rootPackage = $this->composer->getPackage(); - if ($rootPackage->getName() === 'roundcube/roundcubemail') { - $this->initializeVendorDir(); + // https://github.com/composer/composer/discussions/11927#discussioncomment-9116893 + $repo = new InstalledRepository([$repo]); - return dirname($this->vendorDir); - } + $roundcubemailPackages = $repo->findPackagesWithReplacersAndProviders('roundcube/roundcubemail'); + assert(count($roundcubemailPackages) === 1); + $roundcubemailPackage = $roundcubemailPackages[0]; - $roundcubemailPackage = $this->composer - ->getRepositoryManager() - ->findPackage('roundcube/roundcubemail', '*'); + $this->roundcubemailInstallPath = $this->getInstallPath($roundcubemailPackage); + } - 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); } @@ -44,6 +48,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() . '/'); } @@ -114,6 +119,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() . '/'); } @@ -187,6 +193,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() . '/'); }