ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilDatabaseUpdateSteps.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
7
25abstract class ilDatabaseUpdateSteps implements Objective
26{
27 const STEP_METHOD_PREFIX = "step_";
28
32 protected $steps = null;
33
37 protected $base;
38
45 public function __construct(
47 ) {
48 $this->base = $base;
49 }
50
59 public function getAdditionalPreconditionsForStep(int $num) : array
60 {
61 return [];
62 }
63
68 final public function getHash() : string
69 {
70 return hash(
71 "sha256",
72 get_class($this)
73 );
74 }
75
76 final public function getLabel() : string
77 {
78 return "Database update steps in " . get_class($this);
79 }
80
84 final public function isNotable() : bool
85 {
86 return true;
87 }
88
92 final public function getPreconditions(Environment $environment) : array
93 {
94 $log = $environment->getResource(\ilDatabaseUpdateStepExecutionLog::class);
95
96 if ($log) {
97 $finished = $log->getLastFinishedStep(get_class($this));
98 } else {
99 $finished = 0;
100 }
101
102 return [$this->getStep($this->getLatestStepNum(), $finished)];
103 }
104
108 final public function achieve(Environment $environment) : Environment
109 {
110 return $environment;
111 }
112
116 final public function isApplicable(Environment $environment) : bool
117 {
118 return true;
119 }
120
127 final public function getStep(int $num, int $finished = 0) : ilDatabaseUpdateStep
128 {
129 $cur = $this->base;
130 foreach ($this->getSteps() as $s) {
131 if ($s <= $finished) {
132 continue;
133 } elseif ($s <= $num) {
134 $cur = new ilDatabaseUpdateStep($this, $s, $cur, ...$this->getAdditionalPreconditionsForStep($s));
135 } else {
136 break;
137 }
138 }
139
140 return $cur;
141 }
142
146 final public function getLatestStepNum() : int
147 {
148 $this->getSteps();
149 return end($this->steps);
150 }
151
157 final protected function getSteps() : array
158 {
159 if (!is_null($this->steps)) {
160 return $this->steps;
161 }
162
163 $this->steps = [];
164
165 foreach (get_class_methods(static::class) as $method) {
166 if (stripos($method, self::STEP_METHOD_PREFIX) !== 0) {
167 continue;
168 }
169
170 $number = substr($method, strlen(self::STEP_METHOD_PREFIX));
171
172 if (!preg_match("/^[1-9]\d*$/", $number)) {
173 throw new \LogicException("Method $method seems to be a step but has an odd looking number");
174 }
175
176 $this->steps[(int) $number] = (int) $number;
177 }
178
179 asort($this->steps);
180
181 return $this->steps;
182 }
183}
base()
Definition: base.php:4
An exception for terminatinating execution or to throw for unit testing.
This encapsulate one database update step which is a method on some ilDatabaseUpdateSteps-object.
This base-class simplifies the creation of (consecutive) database updates.
getHash()
The hash for the objective is calculated over the classname and the steps that are contained.
getSteps()
Get the numbers of the steps in this class.
getLatestStepNum()
Get the number of latest database step in this class.
isApplicable(Environment $environment)
@inheritDoc
getAdditionalPreconditionsForStep(int $num)
Get preconditions for steps.
achieve(Environment $environment)
@inheritdocs
getStep(int $num, int $finished=0)
Get a database update step.
getPreconditions(Environment $environment)
@inheritdocs
getLabel()
Get a label that describes this objective.
An environment holds resources to be used in the setup process.
Definition: Environment.php:12
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:15
$log
Definition: result.php:15