ILIAS  release_7 Revision v7.30-3-g800a261c036
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
6
11use Symfony\Component\Console\Command\Command;
12use Symfony\Component\Console\Input\InputInterface;
13use Symfony\Component\Console\Input\InputOption;
14use Symfony\Component\Console\Output\OutputInterface;
15use Symfony\Component\Console\Input\InputArgument;
18use ILIAS\Refinery\Factory as Refinery;
20
24class AchieveCommand extends Command
25{
26 use HasAgent;
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,
49 Refinery $refinery
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}
$result
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
An agent that is just a collection of some other agents.
Achieves an objective.
execute(InputInterface $input, OutputInterface $output)
parseAgentMethod(string $agent_method)
__construct(AgentFinder $agent_finder, ConfigReader $config_reader, array $preconditions, Refinery $refinery)
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.
A wrapper around an objective that adds some preconditions.
An environment holds resources to be used in the setup process.
Definition: Environment.php:12
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addAgentConfigsToEnvironment(Agent $agent, Config $config, Environment $environment)
trait HasAgent
Add this to an Command that has an agent.
Definition: HasAgent.php:15
getRelevantAgent(InputInterface $input)
Definition: HasAgent.php:28
readAgentConfig(Agent $agent, InputInterface $input)
trait HasConfigReader
Add this to an Command that has an config reader.