Skip to content

Commit

Permalink
Make sure config is loaded
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sebastianfeldmann committed Nov 25, 2020
1 parent 3862ed4 commit f138d53
Showing 1 changed file with 20 additions and 9 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

0 comments on commit f138d53

Please sign in to comment.