|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Ktomk\Pipelines\Runner\Reference; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Pipelines |
|
12
|
|
|
* |
|
13
|
|
|
* @package Ktomk\Pipelines\File |
|
14
|
|
|
*/ |
|
15
|
|
|
class Pipelines |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $array; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var null|File |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $file; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $references; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Pipelines constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $array |
|
36
|
|
|
* @param null|File $file |
|
37
|
|
|
*/ |
|
38
|
18 |
|
public function __construct(array $array, File $file = null) |
|
39
|
|
|
{ |
|
40
|
18 |
|
$this->file = $file; |
|
41
|
|
|
|
|
42
|
18 |
|
$this->references = $this->parsePipelineReferences($array); |
|
43
|
|
|
|
|
44
|
14 |
|
$this->array = $array; |
|
45
|
14 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws InvalidArgumentException |
|
49
|
|
|
* @throws ParseException |
|
50
|
|
|
* |
|
51
|
|
|
* @return null|Pipeline |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function getDefault() |
|
54
|
|
|
{ |
|
55
|
2 |
|
return PipelinesReferences::byId($this, 'default'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* returns the id of the default pipeline in file or null if there is none |
|
60
|
|
|
* |
|
61
|
|
|
* @return null|string |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function getIdDefault() |
|
64
|
|
|
{ |
|
65
|
2 |
|
return PipelinesReferences::idDefault($this); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function getPipelineIds() |
|
72
|
|
|
{ |
|
73
|
3 |
|
return array_keys($this->references); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $id |
|
78
|
|
|
* |
|
79
|
|
|
* @throws InvalidArgumentException |
|
80
|
|
|
* @throws ParseException |
|
81
|
|
|
* |
|
82
|
|
|
* @return null|Pipeline |
|
83
|
|
|
*/ |
|
84
|
8 |
|
public function getById($id) |
|
85
|
|
|
{ |
|
86
|
8 |
|
return PipelinesReferences::byId($this, $id); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* get id of a pipeline |
|
91
|
|
|
* |
|
92
|
|
|
* @param Pipeline $pipeline |
|
93
|
|
|
* |
|
94
|
|
|
* @return null|string |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function getId(Pipeline $pipeline) |
|
97
|
|
|
{ |
|
98
|
2 |
|
return PipelinesReferences::id($this, $pipeline); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Searches a reference |
|
103
|
|
|
* |
|
104
|
|
|
* @param Reference $reference |
|
105
|
|
|
* |
|
106
|
|
|
* @throws InvalidArgumentException |
|
107
|
|
|
* @throws ParseException |
|
108
|
|
|
* |
|
109
|
|
|
* @return null|Pipeline |
|
110
|
|
|
*/ |
|
111
|
7 |
|
public function searchReference(Reference $reference) |
|
112
|
|
|
{ |
|
113
|
7 |
|
if (null === $type = $reference->getPipelinesType()) { |
|
114
|
2 |
|
return $this->getDefault(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
5 |
|
return $this->searchTypeReference($type, $reference->getName()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Searches a reference within type, returns found one, if |
|
122
|
|
|
* none is found, the default pipeline or null if there is |
|
123
|
|
|
* no default pipeline. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $type of pipeline, can be branches, tags or bookmarks |
|
126
|
|
|
* @param null|string $reference |
|
127
|
|
|
* |
|
128
|
|
|
* @throws InvalidArgumentException |
|
129
|
|
|
* @throws ParseException |
|
130
|
|
|
* |
|
131
|
|
|
* @return null|Pipeline |
|
132
|
|
|
*/ |
|
133
|
8 |
|
public function searchTypeReference($type, $reference) |
|
134
|
|
|
{ |
|
135
|
8 |
|
$id = $this->searchIdByTypeReference($type, $reference); |
|
136
|
|
|
|
|
137
|
7 |
|
return null !== $id ? $this->getById($id) : null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Searches the pipeline that matches the reference |
|
142
|
|
|
* |
|
143
|
|
|
* @param Reference $reference |
|
144
|
|
|
* |
|
145
|
|
|
* @throws InvalidArgumentException |
|
146
|
|
|
* |
|
147
|
|
|
* @return null|string id if found, null otherwise |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function searchIdByReference(Reference $reference) |
|
150
|
|
|
{ |
|
151
|
1 |
|
if (null === $reference->getType()) { |
|
152
|
1 |
|
return $this->getIdDefault(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
return $this->searchIdByTypeReference( |
|
156
|
1 |
|
$reference->getPipelinesType(), |
|
157
|
1 |
|
$reference->getName() |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return File |
|
163
|
|
|
*/ |
|
164
|
1 |
|
public function getFile() |
|
165
|
|
|
{ |
|
166
|
1 |
|
if ($this->file instanceof File) { |
|
167
|
1 |
|
return $this->file; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
throw new \BadMethodCallException('Unassociated node'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @throws InvalidArgumentException |
|
175
|
|
|
* @throws ParseException |
|
176
|
|
|
* |
|
177
|
|
|
* @return array|Pipeline[] |
|
178
|
|
|
*/ |
|
179
|
2 |
|
public function getPipelines() |
|
180
|
|
|
{ |
|
181
|
2 |
|
$pipelines = array(); |
|
182
|
|
|
|
|
183
|
2 |
|
foreach ($this->getPipelineIds() as $id) { |
|
184
|
2 |
|
if (!ReferenceTypes::isValidId($id)) { |
|
185
|
1 |
|
throw new ParseException(sprintf("invalid pipeline id '%s'", $id)); |
|
186
|
|
|
} |
|
187
|
1 |
|
$pipelines[$id] = $this->getById($id); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
return $pipelines; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Index all pipelines as references array map by id |
|
195
|
|
|
* |
|
196
|
|
|
* a reference is array($section (or default), $pattern (or null for default), &$arrayFileParseData) |
|
197
|
|
|
* references are keyed by their pipeline id |
|
198
|
|
|
* |
|
199
|
|
|
* @param array $array |
|
200
|
|
|
* |
|
201
|
|
|
* @throws ParseException |
|
202
|
|
|
* |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
18 |
|
private function parsePipelineReferences(array &$array) |
|
206
|
|
|
{ |
|
207
|
18 |
|
$this->parseValidatePipelines($array); |
|
208
|
|
|
|
|
209
|
16 |
|
$references = $this->referencesDefault($array); |
|
210
|
|
|
|
|
211
|
15 |
|
$references = $this->referencesAddSections($references, $array); |
|
212
|
|
|
|
|
213
|
14 |
|
return $references; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* quick validation of pipeline sections (default pipeline + sections) |
|
218
|
|
|
* |
|
219
|
|
|
* there must be at least one pipeline in the file |
|
220
|
|
|
* |
|
221
|
|
|
* NOTE: the check is incomplete, as it assumes any section contains at |
|
222
|
|
|
* least one pipeline which is unchecked |
|
223
|
|
|
* |
|
224
|
|
|
* @param array $array |
|
225
|
|
|
* |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
18 |
|
private function parseValidatePipelines(array $array) |
|
229
|
|
|
{ |
|
230
|
18 |
|
$sections = ReferenceTypes::getSections(); |
|
231
|
18 |
|
$count = 0; |
|
232
|
18 |
|
foreach ($sections as $section) { |
|
233
|
18 |
|
if (isset($array[$section])) { |
|
234
|
12 |
|
$count++; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
18 |
|
if (!$count && !isset($array['default'])) { |
|
238
|
2 |
|
$middle = implode(', ', array_slice($sections, 0, -1)); |
|
239
|
|
|
|
|
240
|
2 |
|
throw new ParseException("'pipelines' requires at least a default, ${middle} or custom section"); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* create references by default pipeline |
|
246
|
|
|
* |
|
247
|
|
|
* @param array $array |
|
248
|
|
|
* |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
|
|
private function referencesDefault(array &$array) |
|
252
|
|
|
{ |
|
253
|
16 |
|
$references = array(); |
|
254
|
|
|
|
|
255
|
16 |
|
$default = 'default'; |
|
256
|
|
|
|
|
257
|
16 |
|
if (!isset($array[$default])) { |
|
258
|
5 |
|
return $references; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
11 |
|
if (!is_array($array[$default])) { |
|
262
|
1 |
|
throw new ParseException("'${default}' requires a list of steps"); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
10 |
|
$references[$default] = array( |
|
266
|
10 |
|
$default, |
|
267
|
|
|
null, |
|
268
|
10 |
|
&$array[$default], |
|
269
|
|
|
); |
|
270
|
|
|
|
|
271
|
10 |
|
return $references; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* add section pipelines to references |
|
276
|
|
|
* |
|
277
|
|
|
* @param array $references |
|
278
|
|
|
* @param array $array |
|
279
|
|
|
* |
|
280
|
|
|
* @return array |
|
281
|
|
|
*/ |
|
282
|
|
|
private function referencesAddSections(array $references, array &$array) |
|
283
|
|
|
{ |
|
284
|
|
|
// reference section pipelines |
|
285
|
15 |
|
$sections = ReferenceTypes::getSections(); |
|
286
|
|
|
|
|
287
|
15 |
|
foreach ($array as $section => $refs) { |
|
288
|
15 |
|
if (!in_array($section, $sections, true)) { |
|
289
|
10 |
|
continue; // not a section for references |
|
290
|
|
|
} |
|
291
|
12 |
|
if (!is_array($refs)) { |
|
292
|
1 |
|
throw new ParseException("'${section}' requires a list"); |
|
293
|
|
|
} |
|
294
|
11 |
|
foreach ($refs as $pattern => $pipeline) { |
|
295
|
11 |
|
$references["${section}/${pattern}"] = array( |
|
296
|
11 |
|
(string)$section, |
|
297
|
11 |
|
(string)$pattern, |
|
298
|
11 |
|
&$array[$section][$pattern], |
|
299
|
|
|
); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
14 |
|
return $references; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @param null|string $type |
|
308
|
|
|
* @param null|string $reference |
|
309
|
|
|
* |
|
310
|
|
|
* @throws InvalidArgumentException |
|
311
|
|
|
* |
|
312
|
|
|
* @return null|string |
|
313
|
|
|
*/ |
|
314
|
|
|
private function searchIdByTypeReference($type, $reference) |
|
315
|
|
|
{ |
|
316
|
5 |
|
return PipelinesReferences::idByTypeReference($this, $type, $reference); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|