public static function uninstall($packages, $options) { $options = array_merge(self::$options, $options); $packages = is_array($packages) ? $packages : [$packages]; $count = count($packages); $packages = array_filter(array_map(function ($p) { if (is_string($p)) { $p = strtolower($p); $plugin = static::GPM()->getInstalledPlugin($p); $p = $plugin ?: static::GPM()->getInstalledTheme($p); } return $p instanceof Package ? $p : false; }, $packages)); if (!$options['skip_invalid'] && $count !== count($packages)) { return false; } foreach ($packages as $package) { $location = self::getGrav()['locator']->findResource($package->package_type . '://' . $package->slug); // Check destination Installer::isValidDestination($location); if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) { return false; } Installer::uninstall($location); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { return false; } } return true; }
/** * @param $slug * @param $package * * @return bool */ private function uninstallPackage($slug, $package, $is_dependency = false) { if (!$slug) { return false; } //check if there are packages that have this as a dependency. Abort and show list $dependent_packages = $this->gpm->getPackagesThatDependOnPackage($slug); if (count($dependent_packages) > ($is_dependency ? 1 : 0)) { $this->output->writeln(''); $this->output->writeln(''); $this->output->writeln("<red>Uninstallation failed.</red>"); $this->output->writeln(''); if (count($dependent_packages) > ($is_dependency ? 2 : 1)) { $this->output->writeln("The installed packages <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove those first."); } else { $this->output->writeln("The installed package <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove it first."); } $this->output->writeln(''); return false; } $locator = Grav::instance()['locator']; $path = $locator->findResource($package->package_type . '://' . $slug); Installer::uninstall($path); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Uninstalling package... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $message = Installer::getMessage(); if ($message) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- " . $message); } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Uninstalling package... <green>ok</green> "); if (isset($package->dependencies)) { $questionHelper = $this->getHelper('question'); foreach ($package->dependencies as $dependency) { if (is_array($dependency)) { $dependency = $dependency['name']; } if ($dependency === 'grav') { continue; } $dependencyPackage = $this->gpm->findPackage($dependency); $question = new ConfirmationQuestion(" | '- Delete dependency <cyan>" . $dependency . "</cyan> too? [y|N] ", false); $answer = $questionHelper->ask($this->input, $this->output, $question); if ($answer) { $this->output->writeln(" | '- <red>You decided to delete " . $dependency . ".</red>"); $uninstall = $this->uninstallPackage($dependency, $dependencyPackage, true); if (!$uninstall) { $this->output->writeln(" '- <red>Uninstallation failed or aborted.</red>"); $this->output->writeln(''); } else { $this->output->writeln(" '- <green>Success!</green> "); $this->output->writeln(''); } } } } return true; }
/** * @param $slug * @param $package * * @return bool */ private function uninstallPackage($slug, $package) { $path = self::getGrav()['locator']->findResource($package->package_type . '://' . $slug); Installer::uninstall($path); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Uninstalling package... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Uninstalling package... <green>ok</green> "); return true; }
/** * @param $slug * @param $package * * @return bool */ private function uninstallPackage($slug, $package, $is_dependency = false) { if (!$slug) { return false; } //check if there are packages that have this as a dependency. Abort and show list $dependent_packages = $this->gpm->getPackagesThatDependOnPackage($slug); if (count($dependent_packages) > ($is_dependency ? 1 : 0)) { $this->output->writeln(''); $this->output->writeln(''); $this->output->writeln("<red>Uninstallation failed.</red>"); $this->output->writeln(''); if (count($dependent_packages) > ($is_dependency ? 2 : 1)) { $this->output->writeln("The installed packages <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove those first."); } else { $this->output->writeln("The installed package <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove it first."); } $this->output->writeln(''); return false; } if (isset($package->dependencies)) { $dependencies = $package->dependencies; if ($is_dependency) { foreach ($dependencies as $key => $dependency) { if (in_array($dependency['name'], $this->dependencies)) { unset($dependencies[$key]); } } } else { if (count($dependencies) > 0) { $this->output->writeln(' `- Dependencies found...'); $this->output->writeln(''); } } $questionHelper = $this->getHelper('question'); foreach ($dependencies as $dependency) { $this->dependencies[] = $dependency['name']; if (is_array($dependency)) { $dependency = $dependency['name']; } if ($dependency === 'grav') { continue; } $dependencyPackage = $this->gpm->findPackage($dependency); $dependency_exists = $this->packageExists($dependency, $dependencyPackage); if ($dependency_exists == Installer::EXISTS) { $this->output->writeln("A dependency on <cyan>" . $dependencyPackage->name . "</cyan> [v" . $dependencyPackage->version . "] was found"); $question = new ConfirmationQuestion(" |- Uninstall <cyan>" . $dependencyPackage->name . "</cyan>? [y|N] ", false); $answer = $this->all_yes ? true : $questionHelper->ask($this->input, $this->output, $question); if ($answer) { $uninstall = $this->uninstallPackage($dependency, $dependencyPackage, true); if (!$uninstall) { $this->output->writeln(" '- <red>Uninstallation failed or aborted.</red>"); } else { $this->output->writeln(" '- <green>Success!</green> "); } $this->output->writeln(''); } else { $this->output->writeln(" '- <yellow>You decided not to uninstall " . $dependencyPackage->name . ".</yellow>"); $this->output->writeln(''); } } } } $locator = Grav::instance()['locator']; $path = $locator->findResource($package->package_type . '://' . $slug); Installer::uninstall($path); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { $this->output->writeln(" |- Uninstalling " . $package->name . " package... <red>error</red> "); $this->output->writeln(" | '- <yellow>" . Installer::lastErrorMsg() . "</yellow>"); return false; } $message = Installer::getMessage(); if ($message) { $this->output->writeln(" |- " . $message); } if (!$is_dependency && $this->dependencies) { $this->output->writeln("Finishing up uninstalling <cyan>" . $package->name . "</cyan>"); } $this->output->writeln(" |- Uninstalling " . $package->name . " package... <green>ok</green> "); return true; }