ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
AchieveCommand.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 /* Copyright (c) 2021 - Daniel Weise <daniel.weise@concepts-and-training.de> - Extended GPL, see LICENSE */
4 
5 namespace ILIAS\Setup\CLI;
6 
20 
24 class AchieveCommand extends Command
25 {
26  use HasAgent;
27  use HasConfigReader;
28  use ObjectiveHelper;
29 
30  protected static $defaultName = "achieve";
31 
35  protected $preconditions;
36 
40  protected $refinery;
41 
45  public function __construct(
46  AgentFinder $agent_finder,
47  ConfigReader $config_reader,
48  array $preconditions,
50  ) {
52  $this->agent_finder = $agent_finder;
53  $this->config_reader = $config_reader;
54  $this->preconditions = $preconditions;
55  $this->refinery = $refinery;
56  }
57 
58  public function configure()
59  {
60  $this->setDescription("Achieve a named objective from an agent.");
61  $this->addArgument("objective", InputArgument::REQUIRED, "Objective to be execute from an agent. Format: \$AGENT::\$OBJECTIVE");
62  $this->addArgument("config", InputArgument::OPTIONAL, "Configuration file for the installation");
63  $this->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\"", []);
64  $this->addOption("yes", "y", InputOption::VALUE_NONE, "Confirm every message of the update.");
65  }
66 
67  public function execute(InputInterface $input, OutputInterface $output)
68  {
69  $objective_name = $input->getArgument('objective');
70 
71  $io = new IOWrapper($input, $output);
72  $io->printLicenseMessage();
73  $io->title("Achieve objective: $objective_name");
74 
75  $agent = $this->getRelevantAgent($input);
76 
77  if ($input->getArgument("config")) {
78  $config = $this->readAgentConfig($agent, $input);
79  } else {
80  $config = null;
81  }
82 
83  $objective = $agent->getNamedObjective($objective_name, $config);
84 
85  if (count($this->preconditions) > 0) {
86  $objective = new ObjectiveWithPreconditions(
87  $objective,
88  ...$this->preconditions
89  );
90  }
91 
92  $environment = new ArrayEnvironment([
94  ]);
95  if ($config) {
96  $environment = $this->addAgentConfigsToEnvironment($agent, $config, $environment);
97  }
98 
99  try {
100  $this->achieveObjective($objective, $environment, $io);
101  $io->success("Achieved objective '$objective_name'. Thanks and have fun!");
102  } catch (NoConfirmationException $e) {
103  $io->error("Aborted the attempt to achieve '$objective_name', a necessary confirmation is missing:\n\n" . $e->getRequestedConfirmation());
104  }
105  }
106 
107  protected function parseAgentMethod(string $agent_method) : ?array
108  {
109  $result = [];
110  if (!preg_match('/^\s*(\w+)::(\w+)\s*$/', $agent_method, $result)) {
111  return null;
112  }
113 
114  return [$result[1], $result[2]];
115  }
116 }
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:16
$result
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
__construct(AgentFinder $agent_finder, ConfigReader $config_reader, array $preconditions, Refinery $refinery)
readAgentConfig(Agent $agent, InputInterface $input)
A wrapper around an objective that adds some preconditions.
addAgentConfigsToEnvironment(Agent $agent, Config $config, Environment $environment)
execute(InputInterface $input, OutputInterface $output)
getRelevantAgent(InputInterface $input)
Definition: HasAgent.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
parseAgentMethod(string $agent_method)
__construct(Container $dic, ilPlugin $plugin)
trait HasAgent
Add this to an Command that has an agent.
Definition: HasAgent.php:15
Signals that a necessary confirmation from the admin is missing.
trait HasConfigReader
Add this to an Command that has an config reader.
Achieves an objective.