|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @subpackage Task |
|
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\Task; |
|
16
|
|
|
use Netresearch\Kite\Exception; |
|
17
|
|
|
use Netresearch\Kite\Task; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Run each task for each of an arrays element |
|
21
|
|
|
* |
|
22
|
|
|
* @category Netresearch |
|
23
|
|
|
* @package Netresearch\Kite |
|
24
|
|
|
* @subpackage Task |
|
25
|
|
|
* @author Christian Opitz <[email protected]> |
|
26
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
27
|
|
|
* @link http://www.netresearch.de |
|
28
|
|
|
*/ |
|
29
|
|
|
class IterateTask extends SubTask |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $defaultAs = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $defaultKey = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $array; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private $as; |
|
50
|
|
|
|
|
51
|
|
|
private $running = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Configure the options |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function configureVariables() |
|
59
|
|
|
{ |
|
60
|
|
|
return array( |
|
61
|
|
|
'array' => array( |
|
62
|
|
|
'type' => 'array', |
|
63
|
|
|
'required' => true, |
|
64
|
|
|
'label' => 'The array to iterate over' |
|
65
|
|
|
), |
|
66
|
|
|
'as' => array( |
|
67
|
|
|
'type' => 'string|array', |
|
68
|
|
|
'default' => $this->defaultAs, |
|
69
|
|
|
'label' => 'String with variable name to set the VALUEs to or array which\'s key to set the KEYs and which\'s value to set the VALUEs to' |
|
70
|
|
|
), |
|
71
|
|
|
'key' => array( |
|
72
|
|
|
'type' => 'string', |
|
73
|
|
|
'default' => $this->defaultKey, |
|
74
|
|
|
'label' => 'Variable name to set the KEYs to (ignored when "as" doesn\'t provide both' |
|
75
|
|
|
), |
|
76
|
|
|
'--' |
|
77
|
|
|
) + parent::configureVariables(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the array to iterate over |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getArray() |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->array === null) { |
|
88
|
|
|
$this->array = $this->get('array'); |
|
|
|
|
|
|
89
|
|
|
if (!is_array($this->array) && !$this->array instanceof \Traversable) { |
|
90
|
|
|
throw new Exception('Invalid array'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $this->array; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the as (key => value) config |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getAs() |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->as === null) { |
|
104
|
|
|
$as = $this->get('as', $this->defaultAs); |
|
105
|
|
|
if (is_array($as)) { |
|
106
|
|
|
$key = key($as); |
|
107
|
|
|
$value = $as[$key]; |
|
108
|
|
|
} else { |
|
109
|
|
|
$key = $this->get('key', $this->defaultKey); |
|
110
|
|
|
$value = $as; |
|
111
|
|
|
} |
|
112
|
|
|
$this->as = array(); |
|
113
|
|
|
foreach (array('key', 'value') as $name) { |
|
114
|
|
|
if ($$name) { |
|
115
|
|
|
if ($this->has($$name)) { |
|
116
|
|
|
throw new Exception('Variable ' . $$name . ' is already present'); |
|
117
|
|
|
} |
|
118
|
|
|
$this->as[$name] = $$name; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return $this->as; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Run the task |
|
127
|
|
|
* |
|
128
|
|
|
* @param Task $task The task |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed|null The task return value or null when if failed or dry run |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function runTask(Task $task) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($this->running) { |
|
135
|
|
|
return parent::runTask($task); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->running = true; |
|
139
|
|
|
|
|
140
|
|
|
$as = $this->getAs(); |
|
141
|
|
|
|
|
142
|
|
|
foreach ($this->getArray() as $key => $value) { |
|
143
|
|
|
foreach ($as as $type => $name) { |
|
144
|
|
|
$this->set($name, $$type); |
|
145
|
|
|
} |
|
146
|
|
|
try { |
|
147
|
|
|
parent::runTask($task); |
|
148
|
|
|
} catch (\Exception $exception) { |
|
149
|
|
|
break; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
if (isset($exception) && $exception instanceof Exception\BreakException) { |
|
|
|
|
|
|
154
|
|
|
$message = $exception->getMessage(); |
|
155
|
|
|
if ($message) { |
|
156
|
|
|
$this->console->output($task->expand($message)); |
|
157
|
|
|
} |
|
158
|
|
|
unset($exception); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (isset($key)) { |
|
162
|
|
|
foreach ($as as $name) { |
|
163
|
|
|
$this->remove($name); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$this->running = false; |
|
168
|
|
|
|
|
169
|
|
|
if (isset($exception)) { |
|
170
|
|
|
throw $exception; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
?> |
|
175
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..