1 | <?php |
||
2 | |||
3 | namespace CyberLine\SystemdState; |
||
4 | |||
5 | use CyberLine\SystemdState\Model\ExecCommand; |
||
6 | |||
7 | class Parser |
||
8 | { |
||
9 | private static $arrayTypes = [ |
||
10 | 'After', |
||
11 | 'Before', |
||
12 | 'Requires', |
||
13 | 'Conflicts', |
||
14 | 'WantedBy', |
||
15 | 'Names', |
||
16 | 'Wants', |
||
17 | 'RequiredBy', |
||
18 | 'RequiredByOverridable', |
||
19 | 'RequiresMountsFor', |
||
20 | 'ReadWritePaths', |
||
21 | 'ReadOnlyPaths', |
||
22 | 'DropInPaths', |
||
23 | 'ConflictedBy', |
||
24 | 'Documentation', |
||
25 | 'BindsTo', |
||
26 | 'Triggers', |
||
27 | 'Features', |
||
28 | 'UnitPath', |
||
29 | 'BoundBy', |
||
30 | ]; |
||
31 | |||
32 | private static $execTypes = [ |
||
33 | 'ExecReload', |
||
34 | 'ExecStartPre', |
||
35 | 'ExecStart', |
||
36 | 'ExecStartPost', |
||
37 | 'ExecStop', |
||
38 | 'ExecStopPost', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @param array $values |
||
43 | * @return array|bool|ExecCommand|\DateTimeImmutable|int|mixed|null |
||
44 | */ |
||
45 | 32 | public static function parseValueByContent(array $values) |
|
46 | { |
||
47 | 32 | if (!array_key_exists(1, $values)) { |
|
48 | 1 | return null; |
|
49 | } |
||
50 | |||
51 | 31 | $key = $values[0]; |
|
52 | |||
53 | 31 | if (null !== ($value = self::checkByKey($key, $values))) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
54 | 24 | return $value; |
|
55 | } |
||
56 | |||
57 | 9 | if (null !== ($value = self::checkByValue($values[1]))) { |
|
58 | 5 | return $value; |
|
59 | } |
||
60 | |||
61 | 5 | return $values[1]; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $key |
||
66 | * @param array $values |
||
67 | * @return array|ExecCommand|\DateTimeImmutable|null |
||
68 | */ |
||
69 | 31 | private static function checkByKey(string $key, array $values) |
|
70 | { |
||
71 | 31 | if ($key === 'Environment') { |
|
72 | 1 | return self::parseEnvironmentLine($values); |
|
73 | } |
||
74 | |||
75 | 31 | if (in_array($key, self::$execTypes, true)) { |
|
76 | 2 | array_shift($values); |
|
77 | 2 | return self::parseExecLine(implode(' ', $values)); |
|
78 | } |
||
79 | |||
80 | 30 | if (preg_match('/Timestamp$/', $key)) { |
|
81 | 1 | return \date_create_immutable_from_format('* Y-m-d H:i:s e', $values[1]); |
|
82 | } |
||
83 | |||
84 | 29 | if (in_array($key, self::$arrayTypes, true)) { |
|
85 | 20 | return explode(' ', $values[1]); |
|
86 | } |
||
87 | |||
88 | 9 | return null; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $value |
||
93 | * @return bool|int|null |
||
94 | */ |
||
95 | 9 | private static function checkByValue(string $value) |
|
96 | { |
||
97 | 9 | if ($value === '0') { |
|
98 | 2 | return 0; |
|
99 | } |
||
100 | |||
101 | 8 | if ($value === 'yes') { |
|
102 | 1 | return true; |
|
103 | } |
||
104 | |||
105 | 7 | if ($value === 'no') { |
|
106 | 2 | return false; |
|
107 | } |
||
108 | |||
109 | 6 | if ($value < PHP_INT_MAX && preg_match('/^[1-9]\d*$/', $value)) { |
|
110 | 1 | return (int)$value; |
|
111 | } |
||
112 | |||
113 | 5 | return null; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param string $line |
||
118 | * @return ExecCommand |
||
119 | */ |
||
120 | 2 | private static function parseExecLine(string $line): ExecCommand |
|
121 | { |
||
122 | 2 | preg_match('/^{[ ](?<command>.*)[ ]}$/', $line, $matches); |
|
123 | |||
124 | 2 | $execCommand = new ExecCommand; |
|
125 | 2 | if (!array_key_exists('command', $matches)) { |
|
126 | 1 | $execCommand->path = $line; |
|
127 | 1 | return $execCommand; |
|
128 | } |
||
129 | |||
130 | 1 | $explode = array_map('trim', explode(';', $matches['command'])); |
|
131 | array_walk($explode, function (string $value, int $key, ExecCommand $execCommand) { |
||
132 | 1 | $split = explode('=', $value); |
|
133 | 1 | $array = self::parseExecDetail($split); |
|
134 | 1 | if (count($array)) { |
|
135 | 1 | $execCommand->{$array[0]} = $array[1]; |
|
136 | } |
||
137 | 1 | }, $execCommand); |
|
138 | |||
139 | 1 | return $execCommand; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param array $values |
||
144 | * @return array |
||
145 | */ |
||
146 | 1 | private static function parseExecDetail(array $values): array |
|
147 | { |
||
148 | 1 | $index = $values[0]; |
|
149 | |||
150 | 1 | if (in_array($index, ['path', 'pid', 'code', 'status'])) { |
|
151 | 1 | return [$index, self::parseValueByContent($values)]; |
|
152 | } |
||
153 | |||
154 | 1 | if ($index === 'argv[]') { |
|
155 | 1 | $explode = explode(' ', $values[1]); |
|
156 | 1 | array_shift($explode); |
|
157 | |||
158 | 1 | return ['argv', implode(' ', $explode),]; |
|
159 | } |
||
160 | |||
161 | 1 | if ($index === 'start_time') { |
|
162 | 1 | return ['startTime', $values[1]]; |
|
163 | } |
||
164 | |||
165 | 1 | if ($index === 'stop_time') { |
|
166 | 1 | return ['stopTime', $values[1]]; |
|
167 | } |
||
168 | |||
169 | 1 | if ($index === 'ignore_errors') { |
|
170 | 1 | return ['ignoreErrors', self::parseValueByContent($values)]; |
|
171 | } |
||
172 | |||
173 | return []; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param array $values |
||
178 | * @return array |
||
179 | */ |
||
180 | 1 | private static function parseEnvironmentLine(array $values): array |
|
181 | { |
||
182 | 1 | array_shift($values); |
|
183 | 1 | $environments = []; |
|
184 | |||
185 | 1 | foreach (explode(' ', implode('=', $values)) as $environment) { |
|
186 | 1 | $explode = explode('=', $environment); |
|
187 | 1 | $environments[$explode[0]] = self::parseValueByContent($explode); |
|
188 | } |
||
189 | |||
190 | 1 | return $environments; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Used by PHPUnit |
||
195 | * |
||
196 | * @return array |
||
197 | * @codeCoverageIgnore |
||
198 | */ |
||
199 | public static function getArrayTypes(): array |
||
200 | { |
||
201 | return self::$arrayTypes; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Used by PHPUnit |
||
206 | * |
||
207 | * @return array |
||
208 | * @codeCoverageIgnore |
||
209 | */ |
||
210 | public static function getExecTypes(): array |
||
211 | { |
||
212 | return self::$execTypes; |
||
213 | } |
||
214 | } |
||
215 |