ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
BaseCommand.php
Go to the documentation of this file.
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
14use Symfony\Component\Console\Command\Command;
15use Symfony\Component\Console\Input\InputInterface;
16use Symfony\Component\Console\Input\InputArgument;
17use Symfony\Component\Console\Input\InputOption;
18use Symfony\Component\Console\Output\OutputInterface;
19
23abstract class BaseCommand extends Command
24{
25 protected static $defaultName = "install";
26
30 protected $lazy_agent;
31
35 protected $agent;
36
40 protected $config_reader;
41
45 protected $preconditions;
46
52 {
54 $this->lazy_agent = $lazy_agent;
55 $this->agent = null;
56 $this->config_reader = $config_reader;
57 $this->preconditions = $preconditions;
58 }
59
60 protected function getAgent() : Agent
61 {
62 if ($this->agent !== null) {
63 return $this->agent;
64 }
65 $this->agent = ($this->lazy_agent)();
66 return $this->agent;
67 }
68
69 public function configure()
70 {
71 $this
72 ->addArgument("config", InputArgument::REQUIRED, "Configuration file for the Setup")
73 ->addOption("config", null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, "Define fields in the configuration file that should be overwritten, e.g. \"a.b.c=foo\"", [])
74 ->addOption("yes", "y", InputOption::VALUE_NONE, "Confirm every message of the setup.");
75 }
76
77 public function execute(InputInterface $input, OutputInterface $output)
78 {
79 $io = new IOWrapper($input, $output, $this->shouldSayYes($input));
80
81 $this->printLicenseMessage($io, $input);
82
83 $this->printIntroMessage($io);
84
85 $config = $this->readAgentConfig($this->getAgent(), $input);
86 $environment = $this->buildEnvironment($this->getAgent(), $config, $io);
87 $goal = $this->getObjective($this->getAgent(), $config);
88 if (count($this->preconditions) > 0) {
90 $goal,
91 ...$this->preconditions
92 );
93 }
94 $goals = new ObjectiveIterator($environment, $goal);
95
96 try {
97 while ($goals->valid()) {
98 $current = $goals->current();
99 $io->startObjective($current->getLabel(), $current->isNotable());
100 try {
101 $environment = $current->achieve($environment);
102 $io->finishedLastObjective($current->getLabel(), $current->isNotable());
103 $goals->setEnvironment($environment);
104 } catch (UnachievableException $e) {
105 $goals->markAsFailed($current);
106 $io->error($e->getMessage());
107 $io->failedLastObjective($current->getLabel());
108 }
109 $goals->next();
110 }
111 $this->printOutroMessage($io);
112 } catch (NoConfirmationException $e) {
113 $io->error("Aborting Setup, a necessary confirmation is missing:\n\n" . $e->getRequestedConfirmation());
114 }
115 }
116
117 protected function shouldSayYes(InputInterface $input) : bool
118 {
119 return $input->getOption("yes") ?? false;
120 }
121
122 protected function printLicenseMessage(IOWrapper $io, InputInterface $input) : void
123 {
124 if ($this->shouldSayYes($input) || ($input->hasOption("no-interaction") && $input->getOption("no-interaction"))) {
125 return;
126 }
127 $io->text(
128 " ILIAS Copyright (C) 1998-2019 ILIAS Open Source e.V. - GPLv3\n\n" .
129 "This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n" .
130 "and you are welcome to redistribute it under certain conditions. Look\n" .
131 "into the LICENSE file for details."
132 );
133 }
134
135 abstract protected function printIntroMessage(IOWrapper $io);
136
137 abstract protected function printOutroMessage(IOWrapper $io);
138
139 protected function readAgentConfig(Agent $agent, InputInterface $input) : ?Config
140 {
141 if (!$agent->hasConfig()) {
142 return null;
143 }
144
145 $config_file = $input->getArgument("config");
146 $config_overwrites_raw = $input->getOption("config");
147 $config_overwrites = [];
148 foreach ($config_overwrites_raw as $o) {
149 list($k, $v) = explode("=", $o);
150 $config_overwrites[$k] = $v;
151 }
152 $config_content = $this->config_reader->readConfigFile($config_file, $config_overwrites);
153 $trafo = $this->agent->getArrayToConfigTransformation();
154 return $trafo->transform($config_content);
155 }
156
157 abstract protected function buildEnvironment(Agent $agent, ?Config $config, IOWrapper $io);
158
159 abstract protected function getObjective(Agent $agent, ?Config $config) : Objective;
160}
An exception for terminatinating execution or to throw for unit testing.
Command base class.
Definition: BaseCommand.php:24
printLicenseMessage(IOWrapper $io, InputInterface $input)
readAgentConfig(Agent $agent, InputInterface $input)
buildEnvironment(Agent $agent, ?Config $config, IOWrapper $io)
printIntroMessage(IOWrapper $io)
shouldSayYes(InputInterface $input)
$preconditions
var Objective[]
Definition: BaseCommand.php:45
__construct(callable $lazy_agent, ConfigReader $config_reader, array $preconditions)
Definition: BaseCommand.php:51
execute(InputInterface $input, OutputInterface $output)
Definition: BaseCommand.php:77
printOutroMessage(IOWrapper $io)
getObjective(Agent $agent, ?Config $config)
Read a json-formatted config from a file and overwrite some fields.
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:17
Signals that a necessary confirmation from the admin is missing.
Tries to enumerate all preconditions for the given objective, where the ones that can be achieved (i....
A wrapper around an objective that adds some preconditions.
Signals that some goal won't be achievable by actions of the system ever.
A agent is some component that performs part of the setup process.
Definition: Agent.php:14
A configuration for the setup.
Definition: Config.php:11
An environment holds resources to be used in the setup process.
Definition: Environment.php:12
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:15
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc