1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* See class comment |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category Netresearch |
8
|
|
|
* @package Netresearch\Kite |
9
|
|
|
* @subpackage Workflow |
10
|
|
|
* @author Christian Opitz <[email protected]> |
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
12
|
|
|
* @link http://www.netresearch.de |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Workflow; |
16
|
|
|
use Netresearch\Kite\Service\Factory; |
17
|
|
|
use Netresearch\Kite\Task; |
18
|
|
|
|
19
|
|
|
use Netresearch\Kite\Workflow; |
20
|
|
|
use Netresearch\Kite\Exception; |
21
|
|
|
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Run a task for each stage until the selected stage |
26
|
|
|
* |
27
|
|
|
* @category Netresearch |
28
|
|
|
* @package Netresearch\Kite |
29
|
|
|
* @subpackage Workflow |
30
|
|
|
* @author Christian Opitz <[email protected]> |
31
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
32
|
|
|
* @link http://www.netresearch.de |
33
|
|
|
*/ |
34
|
|
|
class StageSelect extends Workflow |
35
|
|
|
{ |
36
|
|
|
protected $message; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Task |
40
|
|
|
*/ |
41
|
|
|
protected $task; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Configures the arguments/options |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
protected function configureVariables() |
49
|
|
|
{ |
50
|
|
|
return array( |
51
|
|
|
'stage' => array( |
52
|
|
|
'type' => 'string', |
53
|
|
|
'argument' => true, |
54
|
|
|
'label' => 'Preselect a stage - otherwise you\'ll be asked' |
55
|
|
|
), |
56
|
|
|
'stages' => array( |
57
|
|
|
'type' => 'array', |
58
|
|
|
'required' => true, |
59
|
|
|
'label' => 'Array of stages - keys are the stages names and the values are arrays which\'s contain variables that will be set when the according stage was selected' |
60
|
|
|
), |
61
|
|
|
'sliding' => array( |
62
|
|
|
'type' => 'bool', |
63
|
|
|
'label' => 'Whether all stages until the selected should be used' |
64
|
|
|
), |
65
|
|
|
'task' => array( |
66
|
|
|
'type' => 'array', |
67
|
|
|
'required' => true, |
68
|
|
|
'label' => 'The task to invoke for each selected stage' |
69
|
|
|
), |
70
|
|
|
'message' => array( |
71
|
|
|
'type' => 'string', |
72
|
|
|
'label' => 'Message to output before each executed stage - %s will be replaced with stage name' |
73
|
|
|
), |
74
|
|
|
'question' => array( |
75
|
|
|
'type' => 'string', |
76
|
|
|
'default' => 'Select stage', |
77
|
|
|
'label' => 'Question to ask before stage select' |
78
|
|
|
), |
79
|
|
|
'--' |
80
|
|
|
) + parent::configureVariables(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Don't show message before all stages but before each stage |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function preview() |
89
|
|
|
{ |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Override to create the tasks from the according options |
94
|
|
|
* |
95
|
|
|
* @param string $name Variable name |
96
|
|
|
* @param mixed $value Variable value |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function offsetSet($name, $value) |
101
|
|
|
{ |
102
|
|
|
|
103
|
|
|
if ($name === 'task') { |
104
|
|
|
$this->task = $value; |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
parent::offsetSet($name, $value); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Called from parent task as soon as task is ready to run - which doesn't |
113
|
|
|
* necessarely mean that it'll be run. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function initialize() |
118
|
|
|
{ |
119
|
|
|
parent::initialize(); |
120
|
|
|
|
121
|
|
|
$this->task = $this->prepare()->sub($this->expand($this->task)); |
122
|
|
|
$this->job->addVariablesFromTask($this->task); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Assemble this workflow |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function assemble() |
132
|
|
|
{ |
133
|
|
|
$this->callback( |
134
|
|
|
function () { |
135
|
|
|
$stages = $this->get('stages'); |
136
|
|
|
$stageOptions = array(); |
137
|
|
|
// Begin keys from 1 |
138
|
|
|
foreach (array_keys($stages) as $i => $stage) { |
139
|
|
|
$stageOptions[$i + 1] = $stage; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$selectedStage = $this->get('stage'); |
143
|
|
|
if ($selectedStage !== null) { |
144
|
|
|
if (!in_array($selectedStage, $stageOptions, true)) { |
145
|
|
|
throw new Exception('Invalid stage'); |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
if (count($stageOptions) === 1) { |
149
|
|
|
$selectedStage = $stageOptions[1]; |
150
|
|
|
} else { |
151
|
|
|
$selectedStage = $this->choose($this->get('question'), $stageOptions); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->console->output("Selected stage <comment>$selectedStage</comment>", OutputInterface::VERBOSITY_VERBOSE); |
156
|
|
|
|
157
|
|
|
$selectedStages = array(); |
158
|
|
|
if ($this->get('sliding')) { |
159
|
|
|
foreach ($stageOptions as $stage) { |
160
|
|
|
$selectedStages[] = $stage; |
161
|
|
|
if ($stage === $selectedStage) { |
162
|
|
|
break; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} else { |
166
|
|
|
$selectedStages[] = $selectedStage; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$message = $this->get('message'); |
170
|
|
|
foreach ($selectedStages as $stage) { |
171
|
|
|
if ($message) { |
172
|
|
|
$this->console->output( |
173
|
|
|
sprintf($message, "<comment>$stage</comment>") |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
$task = clone $this->task; |
177
|
|
|
foreach ($stages[$stage] as $key => $value) { |
178
|
|
|
// Avoid variables overriding parent variables, by prefixing with this |
179
|
|
|
$task->set('this.' . $key, $value); |
180
|
|
|
} |
181
|
|
|
$this->addTask($task); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
?> |
188
|
|
|
|