|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File\Pipeline; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\File\Definitions\Service; |
|
8
|
|
|
use Ktomk\Pipelines\File\File; |
|
9
|
|
|
use Ktomk\Pipelines\File\ParseException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class StepServices |
|
13
|
|
|
* |
|
14
|
|
|
* Services entry in a step |
|
15
|
|
|
* |
|
16
|
|
|
* @package Ktomk\Pipelines\File\File |
|
17
|
|
|
*/ |
|
18
|
|
|
class StepServices |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Step |
|
22
|
|
|
*/ |
|
23
|
|
|
private $step; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
* @psalm-var array<string, int> |
|
28
|
|
|
*/ |
|
29
|
|
|
private $services; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* StepServices constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Step $step |
|
35
|
|
|
* @param null|array|mixed $services |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
7 |
|
public function __construct(Step $step, $services) |
|
40
|
|
|
{ |
|
41
|
|
|
// quick validation: script |
|
42
|
7 |
|
$parsed = $this->parseServices($services); |
|
43
|
|
|
|
|
44
|
5 |
|
$this->step = $step; |
|
45
|
5 |
|
$this->services = array_flip($parsed); |
|
46
|
5 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $service |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function has($service) |
|
54
|
|
|
{ |
|
55
|
1 |
|
return isset($this->services[$service]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* get definitions of all step services |
|
60
|
|
|
* |
|
61
|
|
|
* get all service definitions of the step services, docker is not |
|
62
|
|
|
* of interest here as only looking for standard services, might |
|
63
|
|
|
* be subject to change. |
|
64
|
|
|
* |
|
65
|
|
|
* @return Service[] |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function getDefinitions() |
|
68
|
|
|
{ |
|
69
|
2 |
|
if (null === $file = $this->getFile()) { |
|
70
|
1 |
|
return array(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$standard = $this->services; |
|
74
|
1 |
|
unset($standard['docker']); |
|
75
|
1 |
|
$standard = array_keys($standard); |
|
76
|
|
|
|
|
77
|
1 |
|
return $file->getDefinitions()->getServices()->getByNames($standard); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return null|File |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function getFile() |
|
84
|
|
|
{ |
|
85
|
3 |
|
return $this->step->getFile(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* parse services |
|
90
|
|
|
* |
|
91
|
|
|
* @param null|array|mixed $services |
|
92
|
|
|
* |
|
93
|
|
|
* @return string[] |
|
94
|
|
|
*/ |
|
95
|
7 |
|
private function parseServices($services) |
|
96
|
|
|
{ |
|
97
|
7 |
|
if (!is_array($services)) { |
|
98
|
1 |
|
throw new ParseException("'services' requires a list of services"); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
$reservoir = array(); |
|
102
|
6 |
|
foreach ($services as $service) { |
|
103
|
4 |
|
if (!is_string($service)) { |
|
104
|
1 |
|
throw new ParseException("'services' service name string expected"); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
'' === ($service = trim($service)) || $reservoir[] = $service; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
5 |
|
return $reservoir; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|