ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
9
27abstract class ilDatabaseUpdateSteps implements Objective
28{
29 const STEP_METHOD_PREFIX = "step_";
30
34 protected $steps = null;
35
39 protected $base;
40
47 public function __construct(
49 ) {
50 $this->base = $base;
51 }
52
61 public function getAdditionalPreconditionsForStep(int $num) : array
62 {
63 return [];
64 }
65
70 final public function getHash() : string
71 {
72 return hash(
73 "sha256",
74 get_class($this)
75 );
76 }
77
78 final public function getLabel() : string
79 {
80 return "Database update steps in " . get_class($this);
81 }
82
86 final public function isNotable() : bool
87 {
88 return true;
89 }
90
94 final public function getPreconditions(Environment $environment) : array
95 {
96 $log = $environment->getResource(\ilDatabaseUpdateStepExecutionLog::class);
97
98 if ($log) {
99 $finished = $log->getLastFinishedStep(get_class($this));
100 } else {
101 $finished = 0;
102 }
103
104 return [$this->getStep($this->getLatestStepNum(), $finished)];
105 }
106
110 final public function achieve(Environment $environment) : Environment
111 {
112 return $environment;
113 }
114
121 final public function getStep(int $num, int $finished = 0) : ilDatabaseUpdateStep
122 {
123 $cur = $this->base;
124 foreach ($this->getSteps() as $s) {
125 if ($s <= $finished) {
126 continue;
127 } elseif ($s <= $num) {
128 $cur = new ilDatabaseUpdateStep($this, $s, $cur, ...$this->getAdditionalPreconditionsForStep($s));
129 } else {
130 break;
131 }
132 }
133
134 return $cur;
135 }
136
140 final public function getLatestStepNum() : int
141 {
142 $this->getSteps();
143 return end($this->steps);
144 }
145
151 final protected function getSteps() : array
152 {
153 if (!is_null($this->steps)) {
154 return $this->steps;
155 }
156
157 $this->steps = [];
158
159 foreach (get_class_methods(static::class) as $method) {
160 if (stripos($method, self::STEP_METHOD_PREFIX) !== 0) {
161 continue;
162 }
163
164 $number = substr($method, strlen(self::STEP_METHOD_PREFIX));
165
166 if (!preg_match("/^[1-9]\d*$/", $number)) {
167 throw new \LogicException("Method $method seems to be a step but has an odd looking number");
168 }
169
170 $this->steps[(int) $number] = (int) $number;
171 }
172
173 asort($this->steps);
174
175 return $this->steps;
176 }
177}
base()
Definition: base.php:4
An exception for terminatinating execution or to throw for unit testing.
A callable objective wraps a callable into an objective.
A non-objective, nothing to do to achieve it...
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.
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