From f138d53dcbf349a613cb761f975e38f371174ab8 Mon Sep 17 00:00:00 2001 From: Sebastian Feldmann Date: Wed, 25 Nov 2020 15:12:08 +0100 Subject: [PATCH 1/2] Make sure config is loaded Since all external dependencies are removed before uninstall is called we have to make sure that everything required to handle the uninstallation is loaded upfront. This fixes issue #15 --- src/ConfiguredMediator.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/ConfiguredMediator.php b/src/ConfiguredMediator.php index e839168..288f585 100644 --- a/src/ConfiguredMediator.php +++ b/src/ConfiguredMediator.php @@ -14,6 +14,18 @@ abstract class ConfiguredMediator extends PluginBase { + /** @var \PharIo\ComposerDistributor\Config\Config */ + private $config; + + /** + * Config has to be loaded on instantiation because on uninstall all external dependencies are + * removed before `uninstall` is called and auto-loading any external phar-io dependencies then will fail. + */ + public function __construct() + { + $this->config = Loader::loadFile($this->getDistributorConfig()); + } + abstract protected function getDistributorConfig(): string; public function uninstall(Composer $composer, IOInterface $io) @@ -25,10 +37,8 @@ public function uninstall(Composer $composer, IOInterface $io) public function installOrUpdateFunction(PackageEvent $event): void { - $config = Loader::loadFile($this->getDistributorConfig()); - $installer = $this->createInstallerFromConfig($config, $event); - - $installer->install($config->phars()); + $installer = $this->createInstallerFromConfig($this->config, $event); + $installer->install($this->config->phars()); } private function createInstallerFromConfig(Config $config, PackageEvent $event): Installer @@ -58,11 +68,9 @@ private function createKeyDirectory(Config $config): KeyDirectory private function removePhars(): void { - $config = Loader::loadFile($this->getDistributorConfig()); $binDir = $this->composer->getConfig()->get('bin-dir'); - /** @var \PharIo\ComposerDistributor\File $phar */ - foreach ($config->phars() as $phar) { + foreach ($this->config->phars()->getList() as $phar) { $this->deleteFile($phar, $binDir); } } @@ -70,12 +78,15 @@ private function removePhars(): void private function deleteFile(File $phar, string $binDir): void { $pharLocation = $binDir . DIRECTORY_SEPARATOR . $phar->pharName(); + if (is_file($pharLocation)) { if (!is_writable($pharLocation)) { - $this->io->write(sprintf('can not remove phar \'%1$s\' (insufficient permissions)', $phar->pharName())); + $this->io->write( + sprintf(' Can not remove phar \'%1$s\' (insufficient permissions)', $phar->pharName()) + ); return; } - $this->io->write(sprintf('remove phar \'%1$s\'', $phar->pharName())); + $this->io->write(sprintf(' Removing phar \'%1$s\'', $phar->pharName())); unlink($pharLocation); } } From d214efe1f856bd28813068ea63786a3b9c1ad278 Mon Sep 17 00:00:00 2001 From: Sebastian Feldmann Date: Mon, 30 Nov 2020 14:56:07 +0100 Subject: [PATCH 2/2] Adopt composer list style output Make sure distributor output fits nicely into the Composer output. --- src/ConfiguredMediator.php | 4 ++-- src/Service/Installer.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ConfiguredMediator.php b/src/ConfiguredMediator.php index 288f585..e998bfd 100644 --- a/src/ConfiguredMediator.php +++ b/src/ConfiguredMediator.php @@ -82,11 +82,11 @@ private function deleteFile(File $phar, string $binDir): void if (is_file($pharLocation)) { if (!is_writable($pharLocation)) { $this->io->write( - sprintf(' Can not remove phar \'%1$s\' (insufficient permissions)', $phar->pharName()) + sprintf(' - Can not remove phar \'%1$s\' (insufficient permissions)', $phar->pharName()) ); return; } - $this->io->write(sprintf(' Removing phar \'%1$s\'', $phar->pharName())); + $this->io->write(sprintf(' - Removing phar \'%1$s\'', $phar->pharName())); unlink($pharLocation); } } diff --git a/src/Service/Installer.php b/src/Service/Installer.php index 37da8f3..19adfb6 100644 --- a/src/Service/Installer.php +++ b/src/Service/Installer.php @@ -58,7 +58,7 @@ public function install(FileList $fileList) : void foreach ($fileList->getList() as $file) { $this->io->write(sprintf( - ' Downloading artifact in version %2$s from %1$s', + ' - Downloading artifact in version %2$s from %1$s', $versionReplacer->replace($file->pharUrl()->toString()), $packageVersion->fullVersion() )); @@ -66,13 +66,13 @@ public function install(FileList $fileList) : void $pharLocation = $this->downloadPhar($versionReplacer, $file); if (!$file->signatureUrl()) { - $this->io->write(' No digital signature found! Use this file with care!'); + $this->io->write(' - No digital signature found! Use this file with care!'); continue; } $signatureLocation = $this->downloadSignature($versionReplacer, $file); $this->verifyPharWithSignature($pharLocation, $signatureLocation); - $this->io->write(' PHAR signature successfully verified'); + $this->io->write(' - PHAR signature successfully verified'); unlink($signatureLocation->getPathname()); } }