|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\File\Artifacts; |
|
8
|
|
|
use Ktomk\Pipelines\File\Image; |
|
9
|
|
|
use Ktomk\Pipelines\File\ParseException; |
|
10
|
|
|
|
|
11
|
|
|
class Step |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $step; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Pipeline |
|
20
|
|
|
*/ |
|
21
|
|
|
private $pipeline; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Step constructor. |
|
25
|
|
|
* @param Pipeline $pipeline |
|
26
|
|
|
* @param array $step |
|
27
|
|
|
*/ |
|
28
|
13 |
|
public function __construct(Pipeline $pipeline, array $step) |
|
29
|
|
|
{ |
|
30
|
|
|
// quick validation: image name |
|
31
|
13 |
|
File::validateImage($step); |
|
32
|
|
|
|
|
33
|
|
|
// quick validation: script |
|
34
|
12 |
|
$this->parseScript($step); |
|
35
|
|
|
|
|
36
|
9 |
|
$this->pipeline = $pipeline; |
|
37
|
9 |
|
$this->step = $step; |
|
38
|
9 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return null|Artifacts |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function getArtifacts() |
|
44
|
|
|
{ |
|
45
|
3 |
|
return isset($this->step['artifacts']) |
|
46
|
1 |
|
? new Artifacts($this->step['artifacts']) |
|
47
|
3 |
|
: null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return Image |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function getImage() |
|
54
|
|
|
{ |
|
55
|
3 |
|
return isset($this->step['image']) |
|
56
|
1 |
|
? new Image($this->step['image']) |
|
57
|
3 |
|
: $this->pipeline->getFile()->getImage(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return null|string |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function getName() |
|
64
|
|
|
{ |
|
65
|
3 |
|
return isset($this->step['name']) |
|
66
|
1 |
|
? (string)$this->step['name'] |
|
67
|
3 |
|
: null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array|string[] |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function getScript() |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $this->step['script']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Specify data which should be serialized to JSON |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function jsonSerialize() |
|
84
|
|
|
{ |
|
85
|
1 |
|
$image = $this->getImage(); |
|
86
|
1 |
|
$image = null === $image ? "" : $image->jsonSerialize(); |
|
87
|
|
|
|
|
88
|
|
|
return array( |
|
89
|
1 |
|
'name' => $this->getName(), |
|
90
|
1 |
|
'image' => $image, |
|
91
|
1 |
|
'script' => $this->getScript(), |
|
92
|
1 |
|
'artifacts' => $this->getArtifacts(), |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Parse a step script section |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $step |
|
100
|
|
|
*/ |
|
101
|
12 |
|
private function parseScript(array $step) |
|
102
|
|
|
{ |
|
103
|
12 |
|
if (!isset($step['script'])) { |
|
104
|
1 |
|
ParseException::__("'step' requires a script"); |
|
105
|
|
|
} |
|
106
|
11 |
|
if (!count($step['script']) || !is_array($step['script'])) { |
|
107
|
1 |
|
ParseException::__("'script' requires a list of commands"); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
10 |
|
foreach ($step['script'] as $index => $line) { |
|
111
|
10 |
|
if (!is_scalar($line) && !is_null($line)) { |
|
112
|
1 |
|
ParseException::__( |
|
113
|
1 |
|
sprintf( |
|
114
|
1 |
|
"'script' requires a list of commands, step #%d is not a command", |
|
115
|
10 |
|
$index |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
9 |
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|