setName("apply");
$this->setDescription("Apply a hotfix from a file or the Internet");
$this->addOption("insecure", "I", InputOption::VALUE_NONE, "Disable signature checks. Don't use unless you know what you are doing!");
$this->addOption("preview", "p", InputOption::VALUE_NONE, "Only preview the hotfix script, don't apply anything");
$this->addArgument("hotfix", InputArgument::OPTIONAL, "The identifier or filename of the hotfix");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$fix = $input->getArgument("hotfix");
if (!$fix) {
$output->writeln("No hotfix specified.");
return;
}
//$loader = new Loader();
$output->writeln("Reading hotfix {$fix}...");
try {
$hotfix = HotfixLoader::load($fix);
//$hotfix = $loader->load($fix, $insecure);
} catch (\Exception $e) {
$output->writeln("Error: ".$e->getMessage()."");
return;
}
$output->writeln("");
$header = $hotfix->getHeader();
$output->writeln(" Hotfix: ".$header->getName()."");
$output->writeln(" Author: ".$header->getAuthor()."");
$info = explode("\n",wordwrap(trim($header->getInfo()), 60));
$info = "".join("\n ", $info)."";
$output->writeln(" Info: ".$info);
$output->writeln("");
if (!$this->checkSignature($hotfix, $output)) {
if (!$input->getOption('insecure')) {
$output->writeln("Hotfix can not be authenticated. Aborting!");
return;
}
}
if (!$this->checkRequirements($hotfix, $output)) {
$output->writeln("Error: This hotfix it not compatible with the current distribution>");
return;
}
$output->writeln("");
if ($input->getOption("preview")) {
$output->writeln("This is the script that will be executed:");
$body = $hotfix->getBody();
$body = " ".join("\n ", explode("\n",$body))."";
$output->writeln($body);
return;
}
$helper = $this->getHelper("question");
$question = new ConfirmationQuestion("Do you want to apply this hotfix? [y/N] ", false);
if (!$helper->ask($input, $output, $question)) {
return;
}
$output->writeln("\nApplying hotfix...\n");
try {
$hotfix->apply();
} catch (\Exception $e) {
$output->writeln("\nError: ".$e->getMessage()."");
}
$output->writeln("\nHotfix applied successfully");
}
private function checkSignature(Hotfix $hotfix, OutputInterface $output)
{
$signature = $hotfix->getSignature();
if ($signature->isValid()) {
$keyid = $signature->getKeyId();
$signer = $signature->getSigner();
$output->writeln("Hotfix has good signature from {$signer} (keyid {$keyid})");
return true;
} else {
$error = $signature->getError();
$output->writeln("Warning: {$error}");
return false;
}
}
private function checkRequirements(Hotfix $hotfix, OutputInterface $output)
{
$requires = $hotfix->getHeader()->getRequirements();
if (count($requires)>0) {
$engine = new ExpressionLanguage();
$facts = Facts::getSystemFacts()->getFacts();
while (true) {
$expr = array_shift($requires);
$ret = $engine->evaluate($expr, $facts);
if ($ret) {
//$output->writeln("Hotfix is compatible with the current distribution>");
break;
}
if (count($requires)==0) {
return false;
}
}
}
return true;
}
}