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