Skip to content

Commit

Permalink
Dedup install path computation
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 20, 2024
1 parent c7d907b commit c98d6e4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/ExtensionInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

abstract class ExtensionInstaller extends LibraryInstaller
{
/** @var string|null */
private $roundcubemailInstallPath;

/** @var string */
protected $composer_type;

protected function setRoundcubemailInstallPath(InstalledRepositoryInterface $installedRepo): void
Expand Down Expand Up @@ -46,13 +48,17 @@ protected function getRoundcubemailInstallPath(): string

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

$vendorDir = $this->getVendorDir();

return sprintf('%s/%s', $vendorDir, $this->getPackageName($package));
return $vendorDir . \DIRECTORY_SEPARATOR
. str_replace('/', \DIRECTORY_SEPARATOR, $this->getPackageName($package));
}

public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
Expand All @@ -70,7 +76,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa
$postInstall = function () use ($package) {
$config_file = $this->rcubeConfigFile();
$package_name = $this->getPackageName($package);
$package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name;
$package_dir = $this->getInstallPath($package);
$extra = $package->getExtra();

if (is_writable($config_file) && \PHP_SAPI === 'cli' && $this->confirmInstall($package_name)) {
Expand Down Expand Up @@ -143,16 +149,15 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
$fs = new Filesystem();

// backup persistent files e.g. config.inc.php
$package_name = $this->getPackageName($initial);
$package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name;
$package_dir = $this->getInstallPath($initial);
$temp_dir = $package_dir . '-' . sprintf('%010d%010d', mt_rand(), mt_rand());

// make a backup of existing files (for restoring persistent files)
$fs->copy($package_dir, $temp_dir);

$postUpdate = function () use ($target, $extra, $fs, $temp_dir) {
$package_name = $this->getPackageName($target);
$package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name;
$package_dir = $this->getInstallPath($target);

// restore persistent files
$persistent_files = !empty($extra['roundcube']['persistent-files']) ? $extra['roundcube']['persistent-files'] : ['config.inc.php'];
Expand Down Expand Up @@ -217,7 +222,7 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $
$postUninstall = function () use ($package, $config) {
// post-uninstall: deactivate package
$package_name = $this->getPackageName($package);
$package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name;
$package_dir = $this->getInstallPath($package);

$this->rcubeAlterConfig($package_name, false);

Expand Down Expand Up @@ -400,7 +405,7 @@ private function rcubeRunScript($script, PackageInterface $package)
{
$package_name = $this->getPackageName($package);
$package_type = $package->getType();
$package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name;
$package_dir = $this->getInstallPath($package);

// check for executable shell script
if (($scriptfile = realpath($package_dir . \DIRECTORY_SEPARATOR . $script)) && is_executable($scriptfile)) {
Expand All @@ -424,7 +429,7 @@ private function rcubeRunScript($script, PackageInterface $package)
}

/**
* normalize Roundcube version string.
* Normalize Roundcube version string.
*/
private static function versionNormalize(string $version): string
{
Expand Down

0 comments on commit c98d6e4

Please sign in to comment.