Skip to content

Commit

Permalink
Merge pull request #16 from phar-io/bugfix/uninstall-crash
Browse files Browse the repository at this point in the history
Make sure config is loaded
  • Loading branch information
sebastianfeldmann authored Dec 1, 2020
2 parents 3862ed4 + d214efe commit 0069122
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
29 changes: 20 additions & 9 deletions src/ConfiguredMediator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -58,24 +68,25 @@ 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);
}
}

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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Service/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ 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()
));

$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());
}
}
Expand Down

0 comments on commit 0069122

Please sign in to comment.