1<?php
2/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3
4namespace ILIAS\Setup\CLI;
5
6use ILIAS\Setup\Agent;
7use ILIAS\Setup\AgentCollection;
8use ILIAS\Setup\ArrayEnvironment;
9use ILIAS\Setup\Config;
10use ILIAS\Setup\Environment;
11use ILIAS\Setup\Objective;
12use ILIAS\Setup\ObjectiveCollection;
13use ILIAS\Setup\AchievementTracker;
14use Symfony\Component\Console\Input\InputInterface;
15use Symfony\Component\Console\Output\OutputInterface;
16use Symfony\Component\Console\Input\InputOption;
17
18/**
19 * Update command.
20 */
21class UpdateCommand extends BaseCommand
22{
23    protected static $defaultName = "update";
24
25    public function configure()
26    {
27        parent::configure();
28        $this
29            ->setDescription("Updates an existing ILIAS installation")
30            ->addOption("ignore-db-update-messages", null, InputOption::VALUE_NONE, "Ignore messages from the database update steps.");
31    }
32
33    protected function printIntroMessage(IOWrapper $io)
34    {
35        $io->title("Updating ILIAS");
36    }
37
38    protected function printOutroMessage(IOWrapper $io)
39    {
40        $io->success("Update complete. Thanks and have fun!");
41    }
42
43    protected function buildEnvironment(Agent $agent, ?Config $config, IOWrapper $io) : Environment
44    {
45        $environment = new ArrayEnvironment([
46            Environment::RESOURCE_ADMIN_INTERACTION => $io,
47            // TODO: This needs to be implemented correctly...
48            Environment::RESOURCE_ACHIEVEMENT_TRACKER => new class implements AchievementTracker {
49                public function trackAchievementOf(Objective $objective) : void
50                {
51                }
52                public function isAchieved(Objective $objective) : bool
53                {
54                    return false;
55                }
56            }
57        ]);
58
59        if ($agent instanceof AgentCollection && $config) {
60            foreach ($config->getKeys() as $k) {
61                $environment = $environment->withConfigFor($k, $config->getConfig($k));
62            }
63        }
64
65        return $environment;
66    }
67
68    public function execute(InputInterface $input, OutputInterface $output)
69    {
70        // ATTENTION: This is a hack to get around the usage of the echo/exit pattern in
71        // the setup for the command line version of the setup. Do not use this.
72        if ($input->hasOption("ignore-db-update-messages") && $input->getOption("ignore-db-update-messages")) {
73            define("ILIAS_SETUP_IGNORE_DB_UPDATE_STEP_MESSAGES", true);
74        }
75        return parent::execute($input, $output);
76    }
77
78    protected function getObjective(Agent $agent, ?Config $config) : Objective
79    {
80        return new ObjectiveCollection(
81            "Update ILIAS",
82            false,
83            $agent->getUpdateObjective($config)
84        );
85    }
86}
87