diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b39dee6..3ccccbd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -116,3 +116,16 @@ jobs: cd test-composer/vendor/roundcube/roundcubemail ls -lah plugins/acl/config.* if [ ! -f plugins/acl/config.inc.php ]; then echo 'Config file was not created' && exit 1; fi + + - name: Test update - install plugin + run: | + cd test-composer + echo '// xxx no config update xxx' >> vendor/roundcube/roundcubemail/plugins/carddav/config.inc.php + composer update -v --prefer-dist --no-interaction --no-progress roundcube/carddav --prefer-lowest + + - name: Test update - verify install + run: | + cd test-composer + ls -lah vendor/roundcube/roundcubemail/plugins/carddav/config.* + if [ ! -f vendor/roundcube/roundcubemail/plugins/carddav/config.inc.php ]; then echo 'Config file was deleted' && exit 1; fi + if ! grep -Fq 'xxx no config update xxx' vendor/roundcube/roundcubemail/plugins/carddav/config.inc.php; then echo 'Config file was replaced' && exit 1; fi diff --git a/composer.json b/composer.json index f08d7df..54b491a 100644 --- a/composer.json +++ b/composer.json @@ -19,11 +19,11 @@ ], "require": { "php": ">=7.3 <8.4", - "composer-plugin-api": "^1.0 || ^2.0", + "composer-plugin-api": "^2.1", "roundcube/roundcubemail": "*" }, "require-dev": { - "composer/composer": "^2.0", + "composer/composer": "^2.1", "ergebnis/composer-normalize": "^2.13", "friendsofphp/php-cs-fixer": "^3.0", "phpstan/extension-installer": "^1.1", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 3812e72..f8f6daf 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,6 +3,7 @@ includes: parameters: level: 4 + checkMissingOverrideMethodAttribute: true paths: - . excludePaths: diff --git a/src/ExtensionInstaller.php b/src/ExtensionInstaller.php index 8cfbd61..b4272ac 100644 --- a/src/ExtensionInstaller.php +++ b/src/ExtensionInstaller.php @@ -2,6 +2,7 @@ namespace Roundcube\Composer; +use Composer\Installer\InstallationManager; use Composer\Installer\LibraryInstaller; use Composer\Package\PackageInterface; use Composer\Package\Version\VersionParser; @@ -35,23 +36,41 @@ protected function setRoundcubemailInstallPath(InstalledRepositoryInterface $ins if ($roundcubemailPackage === $rootPackage) { // $this->getInstallPath($package) does not work for root package $this->initializeVendorDir(); - $this->roundcubemailInstallPath = dirname($this->vendorDir); + $installPath = dirname($this->vendorDir); } else { - $this->roundcubemailInstallPath = $this->getInstallPath($roundcubemailPackage); + $installPath = $this->getInstallPath($roundcubemailPackage); + } + + if ($this->roundcubemailInstallPath === null) { + $this->roundcubemailInstallPath = $installPath; + } elseif ($this->roundcubemailInstallPath !== $installPath) { + throw new \Exception('Install path of "roundcube/roundcubemail" package has unexpectedly changed'); } } protected function getRoundcubemailInstallPath(): string { + // install path is not set at composer download phase + // never assume any path, but for this known composer behaviour get it from backtrace instead + if ($this->roundcubemailInstallPath === null) { + $backtrace = debug_backtrace(); + foreach ($backtrace as $frame) { + // relies on https://github.com/composer/composer/blob/2.7.4/src/Composer/Installer/InstallationManager.php#L243 + if (($frame['object'] ?? null) instanceof InstallationManager + && $frame['function'] === 'downloadAndExecuteBatch' + ) { + $this->setRoundcubemailInstallPath($frame['args'][0]); + } + } + } + return $this->roundcubemailInstallPath; } + #[\Override] 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())) { return parent::getInstallPath($package); } @@ -63,13 +82,21 @@ public function getInstallPath(PackageInterface $package) private function initializeRoundcubemailEnvironment(): void { - // initialize Roundcube environment if (!defined('INSTALL_PATH')) { define('INSTALL_PATH', $this->getRoundcubemailInstallPath() . '/'); } require_once INSTALL_PATH . 'program/include/iniset.php'; } + #[\Override] + public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) + { + $this->setRoundcubemailInstallPath($repo); + + return parent::isInstalled($repo, $package); + } + + #[\Override] public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $this->setRoundcubemailInstallPath($repo); @@ -131,6 +158,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa return null; } + #[\Override] public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { $this->setRoundcubemailInstallPath($repo); @@ -196,6 +224,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini return null; } + #[\Override] public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { $this->setRoundcubemailInstallPath($repo); @@ -237,6 +266,7 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $ return null; } + #[\Override] public function supports($packageType) { return $packageType === $this->composer_type; diff --git a/src/PluginInstaller.php b/src/PluginInstaller.php index e09fbe4..99356c0 100644 --- a/src/PluginInstaller.php +++ b/src/PluginInstaller.php @@ -6,11 +6,13 @@ class PluginInstaller extends ExtensionInstaller { protected $composer_type = 'roundcube-plugin'; + #[\Override] public function getVendorDir() { return $this->getRoundcubemailInstallPath() . \DIRECTORY_SEPARATOR . 'plugins'; } + #[\Override] protected function confirmInstall($package_name) { $config = $this->composer->getConfig()->get('roundcube'); @@ -24,6 +26,7 @@ protected function confirmInstall($package_name) return $answer; } + #[\Override] protected function getConfig($package_name, $config, $add) { $cur_config = !empty($config['plugins']) diff --git a/src/RoundcubeInstaller.php b/src/RoundcubeInstaller.php index a7f81fe..5f117c5 100644 --- a/src/RoundcubeInstaller.php +++ b/src/RoundcubeInstaller.php @@ -11,6 +11,7 @@ class RoundcubeInstaller implements PluginInterface private $extentions = [PluginInstaller::class, SkinInstaller::class]; private $installers = []; + #[\Override] public function activate(Composer $composer, IOInterface $io) { foreach ($this->extentions as $extension) { @@ -20,6 +21,7 @@ public function activate(Composer $composer, IOInterface $io) } } + #[\Override] public function deactivate(Composer $composer, IOInterface $io) { foreach ($this->installers as $installer) { @@ -27,5 +29,6 @@ public function deactivate(Composer $composer, IOInterface $io) } } + #[\Override] public function uninstall(Composer $composer, IOInterface $io) {} } diff --git a/src/SkinInstaller.php b/src/SkinInstaller.php index 4d9cee5..1e8c29b 100644 --- a/src/SkinInstaller.php +++ b/src/SkinInstaller.php @@ -6,11 +6,13 @@ class SkinInstaller extends ExtensionInstaller { protected $composer_type = 'roundcube-skin'; + #[\Override] public function getVendorDir() { return $this->getRoundcubemailInstallPath() . \DIRECTORY_SEPARATOR . 'skins'; } + #[\Override] protected function confirmInstall($package_name) { $config = $this->composer->getConfig()->get('roundcube'); @@ -24,6 +26,7 @@ protected function confirmInstall($package_name) return $answer; } + #[\Override] protected function getConfig($package_name, $config, $add) { $cur_config = !empty($config['skin'])