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