ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
MigrateCommand.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Setup\CLI;
22 
33 
37 class MigrateCommand extends Command
38 {
39  use HasAgent;
40  use ObjectiveHelper;
41 
42  protected static $defaultName = "migrate";
43 
47  protected array $preconditions;
48 
52  public function __construct(AgentFinder $agent_finder, array $preconditions)
53  {
55  $this->agent_finder = $agent_finder;
56  $this->preconditions = $preconditions;
57  }
58 
59  protected function configure(): void
60  {
61  $this->setDescription("Starts and manages migrations needed after an update of ILIAS");
62  $this->addOption("yes", "y", InputOption::VALUE_NONE, "Confirm every message of the installation.");
63  $this->addOption("run", "R", InputOption::VALUE_REQUIRED, "Run the migration with the name given.");
64  $this->addOption(
65  "steps",
66  "S",
67  InputOption::VALUE_REQUIRED,
68  "Run the selected migration with X steps. Pass " . Migration::INFINITE . " for all remaining steps."
69  );
71  }
72 
73  protected function execute(InputInterface $input, OutputInterface $output): int
74  {
75  $io = new IOWrapper($input, $output);
76  $io->printLicenseMessage();
77  $io->title("Trigger migrations in ILIAS");
78 
79  // Dispatching further sub-commands
80  if ($input->hasOption('run') && !empty($input->getOption('run'))) {
81  $this->runMigration($input, $io);
82  } else {
83  $this->listMigrations($input, $io);
84  }
85 
86  return 0;
87  }
88 
89  protected function runMigration(InputInterface $input, IOWrapper $io): void
90  {
91  $agent = $this->getRelevantAgent($input);
92 
93  $migration_name = $input->getOption('run');
94  $migrations = $agent->getMigrations();
95  if (!isset($migrations[$migration_name]) || !($migrations[$migration_name] instanceof Migration)) {
96  $io->error("Aborting Migration, did not find $migration_name.");
97  return;
98  }
99  $migration = $migrations[$migration_name];
100 
101  $steps = (int) $input->getOption('steps');
102 
103  switch ($steps) {
104  case Migration::INFINITE:
105  $io->text("Determined infinite steps to run.");
106  break;
107  case 0:
108  $steps = $migration->getDefaultAmountOfStepsPerRun();
109  $io->text("no --steps option found, fallback to default amount of steps of migration. ($steps)");
110  break;
111  default:
112  $io->text("Determined $steps step(s) to run.");
113  break;
114 
115  }
116  $objective = new Objective\MigrationObjective($migration, $steps);
117 
118  $env = new ArrayEnvironment([
120  ]);
121 
122  $preconditions = $migration->getPreconditions($env);
123  if ($preconditions !== []) {
124  $objective = new Objective\ObjectiveWithPreconditions(
125  $objective,
126  ...$preconditions
127  );
128  }
129  $steps_text = $steps === Migration::INFINITE ? 'all' : (string) $steps;
130  $io->inform("Preparing Environment for {$steps_text} steps in {$migration_name}");
131  try {
132  $this->achieveObjective($objective, $env, $io);
133  } catch (NoConfirmationException $e) {
134  $io->error("Aborting Migration, a necessary confirmation is missing:\n\n" . $e->getRequestedConfirmation());
135  }
136  }
137 
138  protected function listMigrations(InputInterface $input, IOWrapper $io): void
139  {
140  $agent = $this->getRelevantAgent($input);
141  $migrations = $agent->getMigrations();
142  $count = count($migrations);
143  if ($count === 0) {
144  $io->inform("There are currently no migrations to run.");
145  return;
146  }
147 
148  $env = new ArrayEnvironment([
150  ]);
151 
152  $io->inform("Found $count migrations:");
153  foreach ($migrations as $migration_key => $migration) {
154  $env = $this->prepareEnvironmentForMigration($env, $migration);
155  $migration->prepare($env);
156  $steps = $migration->getRemainingAmountOfSteps();
157  $status = $steps === 0 ? "[done]" : "[remaining steps: $steps]";
158  $io->text($migration_key . ": " . $migration->getLabel() . " " . $status);
159  }
160  $io->inform('Run them by passing --run <migration_id>, e.g. --run ' . $migration_key);
161  }
162 
163  protected function prepareEnvironmentForMigration(
164  Environment $environment,
165  Migration $migration
166  ): Environment {
167  $preconditions = $migration->getPreconditions($environment);
168  if ($preconditions !== []) {
169  $objective = new Objective\ObjectiveWithPreconditions(
170  new Objective\NullObjective(),
171  ...$preconditions
172  );
173 
174  $environment = $this->achieveObjective($objective, $environment);
175  }
176 
177  return $environment;
178  }
179 }
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:32
An objective is a desired state of the system that is supposed to be created by the setup...
Definition: Objective.php:30
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
execute(InputInterface $input, OutputInterface $output)
__construct(AgentFinder $agent_finder, array $preconditions)
A non-objective, nothing to do to achieve it...
getRelevantAgent(InputInterface $input)
Definition: HasAgent.php:40
prepareEnvironmentForMigration(Environment $environment, Migration $migration)
runMigration(InputInterface $input, IOWrapper $io)
$steps
Definition: latex.php:3
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
configureCommandForPlugins()
Definition: HasAgent.php:33
__construct(Container $dic, ilPlugin $plugin)
trait HasAgent
Add this to an Command that has an agent.
Definition: HasAgent.php:30
listMigrations(InputInterface $input, IOWrapper $io)
Signals that a necessary confirmation from the admin is missing.
inform(string $message)
Definition: IOWrapper.php:67
getPreconditions(Environment $environment)
Objectives the migration depend on.
array $preconditions
var Objective[]