This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wizard.php
223 lines (197 loc) · 6.23 KB
/
Wizard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
/**
* Copyright Zikula.
*
* This work is contributed to the Zikula under one or more
* Contributor Agreements and licensed to You under the following license:
*
* @license MIT.
* @package Zikula
* @author Craig Heydenburg
*
* Please see the LICENSE file distributed with this source code for further
* information regarding copyright and licensing.
*/
namespace Zikula\Component\Wizard;
use InvalidArgumentException;
use Symfony\Component\Config\Exception\LoaderLoadException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
/**
* Class Wizard
*/
class Wizard
{
/**
* @var StageContainerInterface
*/
private $stageContainer;
/**
* @var array
*/
private $stagesByName = [];
/**
* @var array
*/
private $stageOrder = [];
/**
* @var string
*/
private $defaultStage;
/**
* @var string
*/
private $currentStageName;
/**
* @var YamlFileLoader
*/
private $yamlFileLoader;
/**
* @var string
*/
private $warning = '';
/**
* Constructor.
*
* @throws LoaderLoadException
*/
public function __construct(StageContainerInterface $stageContainer, string $path)
{
$this->stageContainer = $stageContainer;
if (!empty($path)) {
$this->loadStagesFromYaml($path);
} else {
throw new LoaderLoadException('No stage definition file provided.');
}
}
/**
* Load the stage definitions from $path
*
* @throws LoaderLoadException
*/
public function loadStagesFromYaml(string $path): void
{
if (!file_exists($path)) {
throw new LoaderLoadException('Stage definition file cannot be found.');
}
$pathInfo = pathinfo($path);
if (!in_array($pathInfo['extension'], ['yml', 'yaml'])) {
throw new LoaderLoadException('Stage definition file must include .yml extension.');
}
// empty the stages
$this->stagesByName = [];
if (!isset($this->yamlFileLoader)) {
$this->yamlFileLoader = new YamlFileLoader(new FileLocator($pathInfo['dirname']));
}
$this->yamlFileLoader->load($pathInfo['basename']);
$stages = $this->yamlFileLoader->getContent();
$stages = $stages['stages'];
foreach ($stages as $key => $stageArray) {
$this->stagesByName[$key] = $stageArray['class'];
$this->stageOrder[$stageArray['order']] = $key;
if (isset($stageArray['default'])) {
$this->defaultStage = $key;
}
}
}
/**
* Get the stage that is the first necessary stage
*/
public function getCurrentStage(string $name): StageInterface
{
// compute the stageClass from Request parameter
$stageClass = $this->getStageClassName($name);
// loop each stage until finds the first that is necessary
do {
$useCurrentStage = false;
/** @var StageInterface $currentStage */
if (!isset($currentStage)) {
$currentStage = $this->getStage($stageClass);
}
$this->currentStageName = $currentStage->getName();
try {
$isNecessary = $currentStage->isNecessary();
} catch (AbortStageException $e) {
$this->warning = $e->getMessage();
$isNecessary = true;
}
if ($isNecessary) {
$useCurrentStage = true;
} else {
$currentStage = $this->getNextStage();
}
} while (false === $useCurrentStage);
return $currentStage;
}
/**
* Get an instance of the previous stage
*/
public function getPreviousStage(): StageInterface
{
return $this->getSequentialStage('prev');
}
/**
* Get an instance of the next stage
*/
public function getNextStage(): StageInterface
{
return $this->getSequentialStage('next');
}
/**
* Get either previous or next stage
*/
private function getSequentialStage(string $direction): ?StageInterface
{
$dir = in_array($direction, ['prev', 'next']) ? $direction : 'next';
ksort($this->stageOrder);
// forward the array pointer to the current index
while (current($this->stageOrder) !== $this->currentStageName && null !== key($this->stageOrder)) {
next($this->stageOrder);
}
$key = $dir($this->stageOrder);
if (null !== $key && false !== $key) {
return $this->getStage($this->stagesByName[$key]);
}
return null;
}
/**
* Get stage from stageContainer
*/
private function getStage(string $stageClass): StageInterface
{
if ($this->stageContainer->has($stageClass)) {
return $this->stageContainer->get($stageClass);
}
throw new FileNotFoundException('Error: Could not find requested stage class.');
}
/**
* Has the wizard been halted?
*/
public function isHalted(): bool
{
return !empty($this->warning);
}
/**
* Get any warning currently set
*/
public function getWarning(): string
{
return 'WARNING: The Wizard was halted for the following reason. This must be corrected before you can continue. ' . $this->warning;
}
/**
* Match the stage and return the stage classname or default.
*
* @throws InvalidArgumentException
*/
private function getStageClassName(string $name): string
{
if (!empty($this->stagesByName[$name])) {
return $this->stagesByName[$name];
}
if (!empty($this->defaultStage) && !empty($this->stagesByName[$this->defaultStage])) {
return $this->stagesByName[$this->defaultStage];
}
throw new InvalidArgumentException('The request stage could not be found and there is no default stage defined.');
}
}