1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the phpflo\phpflo-fbp package. |
4
|
|
|
* |
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace PhpFlo\Fbp; |
13
|
|
|
|
14
|
|
|
use PhpFlo\Common\FbpDefinitionsInterface; |
15
|
|
|
use PhpFlo\Common\Exception\DumperException; |
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class FbpDumper |
20
|
|
|
* |
21
|
|
|
* @package PhpFlo\Fbp |
22
|
|
|
* @author Marc Aschmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class FbpDumper implements FbpDefinitionsInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $processes; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $definition |
33
|
|
|
* @return string json |
34
|
|
|
*/ |
35
|
2 |
|
public static function toJson(array $definition): string |
36
|
|
|
{ |
37
|
2 |
|
return json_encode($definition, JSON_PRETTY_PRINT); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $definition |
42
|
|
|
* @param int $inline level until inlining starts |
43
|
|
|
* @return string yaml |
44
|
|
|
*/ |
45
|
2 |
|
public static function toYaml(array $definition, int $inline = 3): string |
46
|
|
|
{ |
47
|
2 |
|
return Yaml::dump($definition, $inline); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $definition |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
public static function toFbp(array $definition): string |
55
|
|
|
{ |
56
|
1 |
|
return self::createFbp($definition); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $definition |
61
|
|
|
* @return string |
62
|
|
|
* @throws DumperException |
63
|
|
|
*/ |
64
|
2 |
|
private static function createFbp(array $definition): string |
65
|
|
|
{ |
66
|
2 |
|
$fbp = []; |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
|
70
|
2 |
|
if (self::hasElement(self::PROPERTIES_LABEL, $definition)) { |
71
|
2 |
|
foreach($definition[self::PROPERTIES_LABEL] as $name => $element) { |
72
|
2 |
|
if (!empty($element)) { |
73
|
2 |
|
$fbp[] = '# ' . (string)$element; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// first check for process definitions |
79
|
2 |
|
if (self::hasElement(self::PROCESSES_LABEL, $definition)) { |
80
|
2 |
|
self::$processes = $definition[self::PROCESSES_LABEL]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// handle initializer |
84
|
2 |
|
if (!empty($definition[self::INITIALIZERS_LABEL])) { |
85
|
1 |
|
foreach ($definition[self::INITIALIZERS_LABEL] as $initializer) { |
86
|
1 |
|
if (empty($initializer[self::DATA_LABEL])) { |
87
|
|
|
self::throwDumperException('no_definition', self::DATA_LABEL); |
88
|
|
|
} |
89
|
1 |
|
if (empty($initializer[self::TARGET_LABEL])) { |
90
|
|
|
self::throwDumperException('no_definition', self::TARGET_LABEL); |
91
|
|
|
} |
92
|
1 |
|
array_push( |
93
|
|
|
$fbp, |
94
|
1 |
|
self::connectPorts( |
95
|
1 |
|
$initializer[self::DATA_LABEL], |
96
|
1 |
|
self::examineProcess(self::TARGET_LABEL, $initializer[self::TARGET_LABEL]) |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
foreach ($definition[self::CONNECTIONS_LABEL] as $connection) { |
103
|
2 |
|
array_push($fbp, self::examineConnectionTouple($connection)); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return implode(self::FILE_LINEFEED, $fbp); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
throw new DumperException( |
109
|
|
|
"Unexpected dumper error \"{$e->getMessage()}\" in {$e->getFile()} on Line {$e->getLine()}" |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Look for all needed fields and build a port -> port connection. |
116
|
|
|
* |
117
|
|
|
* @param array $connectionTouple |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
1 |
|
private static function examineConnectionTouple(array $connectionTouple): string |
121
|
|
|
{ |
122
|
1 |
|
self::hasElement(self::SOURCE_LABEL, $connectionTouple); |
123
|
1 |
|
self::hasElement(self::TARGET_LABEL, $connectionTouple); |
124
|
|
|
|
125
|
1 |
|
return self::connectPorts( |
126
|
1 |
|
self::examineProcess(self::SOURCE_LABEL, $connectionTouple[self::SOURCE_LABEL]), |
127
|
1 |
|
self::examineProcess(self::TARGET_LABEL, $connectionTouple[self::TARGET_LABEL]) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $type |
133
|
|
|
* @param array $processPart |
134
|
|
|
* @throws DumperException |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
2 |
|
private static function examineProcess(string $type, array $processPart): string |
138
|
|
|
{ |
139
|
2 |
|
self::hasElement(self::PROCESS_LABEL, $processPart); |
140
|
2 |
|
self::hasElement(self::PORT_LABEL, $processPart); |
141
|
|
|
|
142
|
2 |
|
$inport = ''; |
143
|
2 |
|
$outport = ''; |
144
|
2 |
|
$process = $processPart[self::PROCESS_LABEL]; |
145
|
2 |
|
$port = $processPart[self::PORT_LABEL]; |
146
|
|
|
|
147
|
2 |
|
if (self::hasElement($process, self::$processes, false)) { |
148
|
2 |
|
$meta = "(" . self::$processes[$process][self::COMPONENT_LABEL] . ")"; |
149
|
|
|
} else { |
150
|
|
|
self::throwDumperException('process', $process); |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
if (self::SOURCE_LABEL == $type) { |
154
|
2 |
|
$outport = " {$port}"; |
155
|
|
|
} else { |
156
|
2 |
|
$inport = "{$port} "; |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
return "{$inport}{$process}{$meta}{$outport}"; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $needle |
164
|
|
|
* @param array $haystack |
165
|
|
|
* @param bool $triggerException |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
2 |
|
private static function hasElement( |
169
|
|
|
string $needle, |
170
|
|
|
array $haystack, |
171
|
|
|
bool $triggerException = true |
172
|
|
|
): bool { |
173
|
2 |
|
if (empty($haystack[$needle])) { |
174
|
|
|
if ($triggerException) { |
175
|
|
|
self::throwDumperException('elmeent', $needle); |
176
|
|
|
} else { |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $sourcePort |
186
|
|
|
* @param string $targetPort |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
1 |
|
private static function connectPorts(string $sourcePort, string $targetPort): string |
190
|
|
|
{ |
191
|
1 |
|
return implode( |
192
|
1 |
|
" " . self::SOURCE_TARGET_SEPARATOR . " ", |
193
|
|
|
[ |
194
|
1 |
|
$sourcePort, |
195
|
1 |
|
$targetPort |
196
|
|
|
] |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $type |
202
|
|
|
* @param string $value |
203
|
|
|
* @throws DumperException |
204
|
|
|
*/ |
205
|
|
|
private static function throwDumperException(string $type, string $value) |
206
|
|
|
{ |
207
|
|
|
switch ($type) { |
208
|
|
|
case 'element': |
209
|
|
|
throw new DumperException("Element has no {$value}"); |
210
|
|
|
break; |
|
|
|
|
211
|
|
|
case 'process': |
212
|
|
|
throw new DumperException("{$value} is not defined in " . self::PROCESSES_LABEL); |
213
|
|
|
break; |
|
|
|
|
214
|
|
|
case 'no_definition': |
215
|
|
|
throw new DumperException("Defintion has " . |
216
|
|
|
self::INITIALIZERS_LABEL . " but no {$value} node" |
217
|
|
|
); |
218
|
|
|
break; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: