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