ILIAS  release_8 Revision v8.24
class.ilDatabaseUpdateStepsExecutedObjective.php
Go to the documentation of this file.
1<?php
2
21
27{
28 public const STEP_METHOD_PREFIX = "step_";
29
31 protected string $steps_class;
32
34 {
35 $this->steps = $steps;
36 $this->steps_class = get_class($this->steps);
37 }
38
43 final public function getHash(): string
44 {
45 return hash(
46 "sha256",
47 self::class . $this->steps_class
48 );
49 }
50
51 final public function getLabel(): string
52 {
53 return "Database update steps in $this->steps_class.";
54 }
55
59 final public function isNotable(): bool
60 {
61 return true;
62 }
63
67 public function getPreconditions(Environment $environment): array
68 {
69 return [
73 ];
74 }
75
79 public function achieve(Environment $environment): Environment
80 {
81 $execution_log = $environment->getResource(ilDBStepExecutionDB::class);
82 $step_reader = $environment->getResource(ilDBStepReader::class);
83
84 $last_started_step = $execution_log->getLastStartedStep($this->steps_class);
85 $last_finished_step = $execution_log->getLastFinishedStep($this->steps_class);
86 if ($last_started_step !== $last_finished_step) {
87 $this->throwStepNotFinishedException($last_started_step, $last_finished_step);
88 throw new LogicException(
89 "ilDatabaseUpdateStepExecutionLog::throwStepNotFinishedException should throw an exception."
90 );
91 }
92
93 if ($last_finished_step === $step_reader->getLatestStepNumber($this->steps_class, self::STEP_METHOD_PREFIX)) {
94 return $environment;
95 }
96
97 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
98 $this->steps->prepare($db);
99
100 $steps = $step_reader->readStepNumbers($this->steps_class, self::STEP_METHOD_PREFIX);
101 foreach ($steps as $step) {
102 if ($step <= $last_finished_step) {
103 continue;
104 }
105 $execution_log->started($this->steps_class, $step);
106 $method = self::STEP_METHOD_PREFIX . $step;
107 $this->steps->$method();
108 $execution_log->finished($this->steps_class, $step);
109 }
110
111 return $environment;
112 }
113
117 public function isApplicable(Environment $environment): bool
118 {
119 $execution_log = $environment->getResource(ilDBStepExecutionDB::class);
120 $step_reader = $environment->getResource(ilDBStepReader::class);
121
122 return $execution_log->getLastFinishedStep($this->steps_class) !== $step_reader->getLatestStepNumber(
123 $this->steps_class,
124 self::STEP_METHOD_PREFIX
125 );
126 }
127
128 protected function throwStepNotFinishedException(int $started, int $finished): void
129 {
130 throw new RuntimeException(
131 "For update steps in $this->steps_class: step $started was started " .
132 "last, but step $finished was finished last. Aborting because of that " .
133 "mismatch."
134 );
135 }
136}
This class attempt to achieve a set of database update steps.
getHash()
The hash for the objective is calculated over the classname and the steps that are contained.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
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...