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
|
8 |
|
public function __construct(Step $step, $services) |
40
|
|
|
{ |
41
|
|
|
// quick validation: script |
42
|
8 |
|
$parsed = $this->parseServices($services); |
43
|
|
|
|
44
|
6 |
|
$this->step = $step; |
45
|
6 |
|
$this->services = array_flip($parsed); |
46
|
6 |
|
} |
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 |
|
return $file->getDefinitions()->getServices()->getByNames($this->getServiceNames()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array|string[] |
78
|
|
|
*/ |
79
|
2 |
|
public function getServiceNames() |
80
|
|
|
{ |
81
|
2 |
|
$standard = $this->services; |
82
|
2 |
|
unset($standard['docker']); |
83
|
|
|
|
84
|
2 |
|
return array_keys($standard); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return null|File |
89
|
|
|
*/ |
90
|
3 |
|
public function getFile() |
91
|
|
|
{ |
92
|
3 |
|
return $this->step->getFile(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* parse services |
97
|
|
|
* |
98
|
|
|
* @param null|array|mixed $services |
99
|
|
|
* |
100
|
|
|
* @return string[] |
101
|
|
|
*/ |
102
|
8 |
|
private function parseServices($services) |
103
|
|
|
{ |
104
|
8 |
|
if (!is_array($services)) { |
105
|
1 |
|
throw new ParseException("'services' requires a list of services"); |
106
|
|
|
} |
107
|
|
|
|
108
|
7 |
|
$reservoir = array(); |
109
|
7 |
|
foreach ($services as $service) { |
110
|
5 |
|
if (!is_string($service)) { |
111
|
1 |
|
throw new ParseException("'services' service name string expected"); |
112
|
|
|
} |
113
|
|
|
|
114
|
5 |
|
'' === ($service = trim($service)) || $reservoir[] = $service; |
115
|
|
|
} |
116
|
|
|
|
117
|
6 |
|
return $reservoir; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|