Skip to content

Commit c7d907b

Browse files
committed
Use composer InstalledRepository to determine install dir
1 parent da8b3a7 commit c7d907b

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

src/ExtensionInstaller.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,48 @@
55
use Composer\Installer\LibraryInstaller;
66
use Composer\Package\PackageInterface;
77
use Composer\Package\Version\VersionParser;
8+
use Composer\Repository\InstalledRepository;
89
use Composer\Repository\InstalledRepositoryInterface;
10+
use Composer\Repository\RootPackageRepository;
911
use Composer\Util\Filesystem;
1012
use Composer\Util\ProcessExecutor;
1113
use React\Promise\PromiseInterface;
1214

1315
abstract class ExtensionInstaller extends LibraryInstaller
1416
{
17+
private $roundcubemailInstallPath;
18+
1519
protected $composer_type;
1620

17-
protected function getRoundcubemailInstallPath(): string
21+
protected function setRoundcubemailInstallPath(InstalledRepositoryInterface $installedRepo): void
1822
{
19-
$rootPackage = $this->composer->getPackage();
20-
if ($rootPackage->getName() === 'roundcube/roundcubemail') {
23+
// https://github.com/composer/composer/discussions/11927#discussioncomment-9116893
24+
$rootPackage = clone $this->composer->getPackage();
25+
$installedRepo = new InstalledRepository([
26+
$installedRepo,
27+
new RootPackageRepository($rootPackage),
28+
]);
29+
30+
$roundcubemailPackages = $installedRepo->findPackagesWithReplacersAndProviders('roundcube/roundcubemail');
31+
assert(count($roundcubemailPackages) === 1);
32+
$roundcubemailPackage = $roundcubemailPackages[0];
33+
34+
if ($roundcubemailPackage === $rootPackage) { // $this->getInstallPath($package) does not work for root package
2135
$this->initializeVendorDir();
22-
23-
return dirname($this->vendorDir);
36+
$this->roundcubemailInstallPath = dirname($this->vendorDir);
37+
} else {
38+
$this->roundcubemailInstallPath = $this->getInstallPath($roundcubemailPackage);
2439
}
40+
}
2541

26-
$roundcubemailPackage = $this->composer
27-
->getRepositoryManager()
28-
->findPackage('roundcube/roundcubemail', '*');
29-
30-
return $this->getInstallPath($roundcubemailPackage);
42+
protected function getRoundcubemailInstallPath(): string
43+
{
44+
return $this->roundcubemailInstallPath;
3145
}
3246

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

@@ -43,6 +57,8 @@ public function getInstallPath(PackageInterface $package)
4357

4458
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
4559
{
60+
$this->setRoundcubemailInstallPath($repo);
61+
4662
// initialize Roundcube environment
4763
if (!defined('INSTALL_PATH')) {
4864
define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/');
@@ -113,6 +129,8 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa
113129

114130
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
115131
{
132+
$this->setRoundcubemailInstallPath($repo);
133+
116134
// initialize Roundcube environment
117135
if (!defined('INSTALL_PATH')) {
118136
define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/');
@@ -186,6 +204,8 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
186204

187205
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
188206
{
207+
$this->setRoundcubemailInstallPath($repo);
208+
189209
// initialize Roundcube environment
190210
if (!defined('INSTALL_PATH')) {
191211
define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/');

0 commit comments

Comments
 (0)