@@ -11,13 +11,13 @@ |
||
11 | 11 | |
12 | 12 | use Symfony\Polyfill\Intl\Normalizer as p; |
13 | 13 | |
14 | -if (\PHP_VERSION_ID >= 80000) { |
|
15 | - return require __DIR__.'/bootstrap80.php'; |
|
14 | +if ( \PHP_VERSION_ID >= 80000 ) { |
|
15 | + return require __DIR__ . '/bootstrap80.php'; |
|
16 | 16 | } |
17 | 17 | |
18 | -if (!function_exists('normalizer_is_normalized')) { |
|
19 | - function normalizer_is_normalized($string, $form = p\Normalizer::FORM_C) { return p\Normalizer::isNormalized($string, $form); } |
|
18 | +if ( ! function_exists( 'normalizer_is_normalized' ) ) { |
|
19 | + function normalizer_is_normalized( $string, $form = p\Normalizer::FORM_C ) { return p\Normalizer::isNormalized( $string, $form ); } |
|
20 | 20 | } |
21 | -if (!function_exists('normalizer_normalize')) { |
|
22 | - function normalizer_normalize($string, $form = p\Normalizer::FORM_C) { return p\Normalizer::normalize($string, $form); } |
|
21 | +if ( ! function_exists( 'normalizer_normalize' ) ) { |
|
22 | + function normalizer_normalize( $string, $form = p\Normalizer::FORM_C ) { return p\Normalizer::normalize( $string, $form ); } |
|
23 | 23 | } |
@@ -36,81 +36,81 @@ discard block |
||
36 | 36 | private static $D; |
37 | 37 | private static $KD; |
38 | 38 | private static $cC; |
39 | - private static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4]; |
|
39 | + private static $ulenMask = [ "\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4 ]; |
|
40 | 40 | private static $ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"; |
41 | 41 | |
42 | - public static function isNormalized(string $s, int $form = self::FORM_C) |
|
42 | + public static function isNormalized( string $s, int $form = self::FORM_C ) |
|
43 | 43 | { |
44 | - if (!\in_array($form, [self::NFD, self::NFKD, self::NFC, self::NFKC])) { |
|
44 | + if ( ! \in_array( $form, [ self::NFD, self::NFKD, self::NFC, self::NFKC ] ) ) { |
|
45 | 45 | return false; |
46 | 46 | } |
47 | - if (!isset($s[strspn($s, self::$ASCII)])) { |
|
47 | + if ( ! isset( $s[ strspn( $s, self::$ASCII ) ] ) ) { |
|
48 | 48 | return true; |
49 | 49 | } |
50 | - if (self::NFC == $form && preg_match('//u', $s) && !preg_match('/[^\x00-\x{2FF}]/u', $s)) { |
|
50 | + if ( self::NFC == $form && preg_match( '//u', $s ) && ! preg_match( '/[^\x00-\x{2FF}]/u', $s ) ) { |
|
51 | 51 | return true; |
52 | 52 | } |
53 | 53 | |
54 | - return self::normalize($s, $form) === $s; |
|
54 | + return self::normalize( $s, $form ) === $s; |
|
55 | 55 | } |
56 | 56 | |
57 | - public static function normalize(string $s, int $form = self::FORM_C) |
|
57 | + public static function normalize( string $s, int $form = self::FORM_C ) |
|
58 | 58 | { |
59 | - if (!preg_match('//u', $s)) { |
|
59 | + if ( ! preg_match( '//u', $s ) ) { |
|
60 | 60 | return false; |
61 | 61 | } |
62 | 62 | |
63 | - switch ($form) { |
|
63 | + switch ( $form ) { |
|
64 | 64 | case self::NFC: $C = true; $K = false; break; |
65 | 65 | case self::NFD: $C = false; $K = false; break; |
66 | 66 | case self::NFKC: $C = true; $K = true; break; |
67 | 67 | case self::NFKD: $C = false; $K = true; break; |
68 | 68 | default: |
69 | - if (\defined('Normalizer::NONE') && \Normalizer::NONE == $form) { |
|
69 | + if ( \defined( 'Normalizer::NONE' ) && \Normalizer::NONE == $form ) { |
|
70 | 70 | return $s; |
71 | 71 | } |
72 | 72 | |
73 | - if (80000 > \PHP_VERSION_ID) { |
|
73 | + if ( 80000 > \PHP_VERSION_ID ) { |
|
74 | 74 | return false; |
75 | 75 | } |
76 | 76 | |
77 | - throw new \ValueError('normalizer_normalize(): Argument #2 ($form) must be a a valid normalization form'); |
|
77 | + throw new \ValueError( 'normalizer_normalize(): Argument #2 ($form) must be a a valid normalization form' ); |
|
78 | 78 | } |
79 | 79 | |
80 | - if ('' === $s) { |
|
80 | + if ( '' === $s ) { |
|
81 | 81 | return ''; |
82 | 82 | } |
83 | 83 | |
84 | - if ($K && null === self::$KD) { |
|
85 | - self::$KD = self::getData('compatibilityDecomposition'); |
|
84 | + if ( $K && null === self::$KD ) { |
|
85 | + self::$KD = self::getData( 'compatibilityDecomposition' ); |
|
86 | 86 | } |
87 | 87 | |
88 | - if (null === self::$D) { |
|
89 | - self::$D = self::getData('canonicalDecomposition'); |
|
90 | - self::$cC = self::getData('combiningClass'); |
|
88 | + if ( null === self::$D ) { |
|
89 | + self::$D = self::getData( 'canonicalDecomposition' ); |
|
90 | + self::$cC = self::getData( 'combiningClass' ); |
|
91 | 91 | } |
92 | 92 | |
93 | - if (null !== $mbEncoding = (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) ? mb_internal_encoding() : null) { |
|
94 | - mb_internal_encoding('8bit'); |
|
93 | + if ( null !== $mbEncoding = ( 2 /* MB_OVERLOAD_STRING */ & (int)ini_get( 'mbstring.func_overload' ) ) ? mb_internal_encoding() : null ) { |
|
94 | + mb_internal_encoding( '8bit' ); |
|
95 | 95 | } |
96 | 96 | |
97 | - $r = self::decompose($s, $K); |
|
97 | + $r = self::decompose( $s, $K ); |
|
98 | 98 | |
99 | - if ($C) { |
|
100 | - if (null === self::$C) { |
|
101 | - self::$C = self::getData('canonicalComposition'); |
|
99 | + if ( $C ) { |
|
100 | + if ( null === self::$C ) { |
|
101 | + self::$C = self::getData( 'canonicalComposition' ); |
|
102 | 102 | } |
103 | 103 | |
104 | - $r = self::recompose($r); |
|
104 | + $r = self::recompose( $r ); |
|
105 | 105 | } |
106 | - if (null !== $mbEncoding) { |
|
107 | - mb_internal_encoding($mbEncoding); |
|
106 | + if ( null !== $mbEncoding ) { |
|
107 | + mb_internal_encoding( $mbEncoding ); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | return $r; |
111 | 111 | } |
112 | 112 | |
113 | - private static function recompose($s) |
|
113 | + private static function recompose( $s ) |
|
114 | 114 | { |
115 | 115 | $ASCII = self::$ASCII; |
116 | 116 | $compMap = self::$C; |
@@ -119,49 +119,49 @@ discard block |
||
119 | 119 | |
120 | 120 | $result = $tail = ''; |
121 | 121 | |
122 | - $i = $s[0] < "\x80" ? 1 : $ulenMask[$s[0] & "\xF0"]; |
|
123 | - $len = \strlen($s); |
|
122 | + $i = $s[ 0 ] < "\x80" ? 1 : $ulenMask[ $s[ 0 ] & "\xF0" ]; |
|
123 | + $len = \strlen( $s ); |
|
124 | 124 | |
125 | - $lastUchr = substr($s, 0, $i); |
|
126 | - $lastUcls = isset($combClass[$lastUchr]) ? 256 : 0; |
|
125 | + $lastUchr = substr( $s, 0, $i ); |
|
126 | + $lastUcls = isset( $combClass[ $lastUchr ] ) ? 256 : 0; |
|
127 | 127 | |
128 | - while ($i < $len) { |
|
129 | - if ($s[$i] < "\x80") { |
|
128 | + while ( $i < $len ) { |
|
129 | + if ( $s[ $i ] < "\x80" ) { |
|
130 | 130 | // ASCII chars |
131 | 131 | |
132 | - if ($tail) { |
|
132 | + if ( $tail ) { |
|
133 | 133 | $lastUchr .= $tail; |
134 | 134 | $tail = ''; |
135 | 135 | } |
136 | 136 | |
137 | - if ($j = strspn($s, $ASCII, $i + 1)) { |
|
138 | - $lastUchr .= substr($s, $i, $j); |
|
137 | + if ( $j = strspn( $s, $ASCII, $i + 1 ) ) { |
|
138 | + $lastUchr .= substr( $s, $i, $j ); |
|
139 | 139 | $i += $j; |
140 | 140 | } |
141 | 141 | |
142 | 142 | $result .= $lastUchr; |
143 | - $lastUchr = $s[$i]; |
|
143 | + $lastUchr = $s[ $i ]; |
|
144 | 144 | $lastUcls = 0; |
145 | 145 | ++$i; |
146 | 146 | continue; |
147 | 147 | } |
148 | 148 | |
149 | - $ulen = $ulenMask[$s[$i] & "\xF0"]; |
|
150 | - $uchr = substr($s, $i, $ulen); |
|
149 | + $ulen = $ulenMask[ $s[ $i ] & "\xF0" ]; |
|
150 | + $uchr = substr( $s, $i, $ulen ); |
|
151 | 151 | |
152 | - if ($lastUchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $lastUchr |
|
152 | + if ( $lastUchr < "\xE1\x84\x80" || "\xE1\x84\x92" < $lastUchr |
|
153 | 153 | || $uchr < "\xE1\x85\xA1" || "\xE1\x85\xB5" < $uchr |
154 | - || $lastUcls) { |
|
154 | + || $lastUcls ) { |
|
155 | 155 | // Table lookup and combining chars composition |
156 | 156 | |
157 | - $ucls = $combClass[$uchr] ?? 0; |
|
157 | + $ucls = $combClass[ $uchr ] ?? 0; |
|
158 | 158 | |
159 | - if (isset($compMap[$lastUchr.$uchr]) && (!$lastUcls || $lastUcls < $ucls)) { |
|
160 | - $lastUchr = $compMap[$lastUchr.$uchr]; |
|
161 | - } elseif ($lastUcls = $ucls) { |
|
159 | + if ( isset( $compMap[ $lastUchr . $uchr ] ) && ( ! $lastUcls || $lastUcls < $ucls ) ) { |
|
160 | + $lastUchr = $compMap[ $lastUchr . $uchr ]; |
|
161 | + } elseif ( $lastUcls = $ucls ) { |
|
162 | 162 | $tail .= $uchr; |
163 | 163 | } else { |
164 | - if ($tail) { |
|
164 | + if ( $tail ) { |
|
165 | 165 | $lastUchr .= $tail; |
166 | 166 | $tail = ''; |
167 | 167 | } |
@@ -172,29 +172,29 @@ discard block |
||
172 | 172 | } else { |
173 | 173 | // Hangul chars |
174 | 174 | |
175 | - $L = \ord($lastUchr[2]) - 0x80; |
|
176 | - $V = \ord($uchr[2]) - 0xA1; |
|
175 | + $L = \ord( $lastUchr[ 2 ] ) - 0x80; |
|
176 | + $V = \ord( $uchr[ 2 ] ) - 0xA1; |
|
177 | 177 | $T = 0; |
178 | 178 | |
179 | - $uchr = substr($s, $i + $ulen, 3); |
|
179 | + $uchr = substr( $s, $i + $ulen, 3 ); |
|
180 | 180 | |
181 | - if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") { |
|
182 | - $T = \ord($uchr[2]) - 0xA7; |
|
181 | + if ( "\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82" ) { |
|
182 | + $T = \ord( $uchr[ 2 ] ) - 0xA7; |
|
183 | 183 | 0 > $T && $T += 0x40; |
184 | 184 | $ulen += 3; |
185 | 185 | } |
186 | 186 | |
187 | - $L = 0xAC00 + ($L * 21 + $V) * 28 + $T; |
|
188 | - $lastUchr = \chr(0xE0 | $L >> 12).\chr(0x80 | $L >> 6 & 0x3F).\chr(0x80 | $L & 0x3F); |
|
187 | + $L = 0xAC00 + ( $L * 21 + $V ) * 28 + $T; |
|
188 | + $lastUchr = \chr( 0xE0 | $L >> 12 ) . \chr( 0x80 | $L >> 6 & 0x3F ) . \chr( 0x80 | $L & 0x3F ); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | $i += $ulen; |
192 | 192 | } |
193 | 193 | |
194 | - return $result.$lastUchr.$tail; |
|
194 | + return $result . $lastUchr . $tail; |
|
195 | 195 | } |
196 | 196 | |
197 | - private static function decompose($s, $c) |
|
197 | + private static function decompose( $s, $c ) |
|
198 | 198 | { |
199 | 199 | $result = ''; |
200 | 200 | |
@@ -202,106 +202,106 @@ discard block |
||
202 | 202 | $decompMap = self::$D; |
203 | 203 | $combClass = self::$cC; |
204 | 204 | $ulenMask = self::$ulenMask; |
205 | - if ($c) { |
|
205 | + if ( $c ) { |
|
206 | 206 | $compatMap = self::$KD; |
207 | 207 | } |
208 | 208 | |
209 | - $c = []; |
|
209 | + $c = [ ]; |
|
210 | 210 | $i = 0; |
211 | - $len = \strlen($s); |
|
211 | + $len = \strlen( $s ); |
|
212 | 212 | |
213 | - while ($i < $len) { |
|
214 | - if ($s[$i] < "\x80") { |
|
213 | + while ( $i < $len ) { |
|
214 | + if ( $s[ $i ] < "\x80" ) { |
|
215 | 215 | // ASCII chars |
216 | 216 | |
217 | - if ($c) { |
|
218 | - ksort($c); |
|
219 | - $result .= implode('', $c); |
|
220 | - $c = []; |
|
217 | + if ( $c ) { |
|
218 | + ksort( $c ); |
|
219 | + $result .= implode( '', $c ); |
|
220 | + $c = [ ]; |
|
221 | 221 | } |
222 | 222 | |
223 | - $j = 1 + strspn($s, $ASCII, $i + 1); |
|
224 | - $result .= substr($s, $i, $j); |
|
223 | + $j = 1 + strspn( $s, $ASCII, $i + 1 ); |
|
224 | + $result .= substr( $s, $i, $j ); |
|
225 | 225 | $i += $j; |
226 | 226 | continue; |
227 | 227 | } |
228 | 228 | |
229 | - $ulen = $ulenMask[$s[$i] & "\xF0"]; |
|
230 | - $uchr = substr($s, $i, $ulen); |
|
229 | + $ulen = $ulenMask[ $s[ $i ] & "\xF0" ]; |
|
230 | + $uchr = substr( $s, $i, $ulen ); |
|
231 | 231 | $i += $ulen; |
232 | 232 | |
233 | - if ($uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr) { |
|
233 | + if ( $uchr < "\xEA\xB0\x80" || "\xED\x9E\xA3" < $uchr ) { |
|
234 | 234 | // Table lookup |
235 | 235 | |
236 | - if ($uchr !== $j = $compatMap[$uchr] ?? ($decompMap[$uchr] ?? $uchr)) { |
|
236 | + if ( $uchr !== $j = $compatMap[ $uchr ] ?? ( $decompMap[ $uchr ] ?? $uchr ) ) { |
|
237 | 237 | $uchr = $j; |
238 | 238 | |
239 | - $j = \strlen($uchr); |
|
240 | - $ulen = $uchr[0] < "\x80" ? 1 : $ulenMask[$uchr[0] & "\xF0"]; |
|
239 | + $j = \strlen( $uchr ); |
|
240 | + $ulen = $uchr[ 0 ] < "\x80" ? 1 : $ulenMask[ $uchr[ 0 ] & "\xF0" ]; |
|
241 | 241 | |
242 | - if ($ulen != $j) { |
|
242 | + if ( $ulen != $j ) { |
|
243 | 243 | // Put trailing chars in $s |
244 | 244 | |
245 | 245 | $j -= $ulen; |
246 | 246 | $i -= $j; |
247 | 247 | |
248 | - if (0 > $i) { |
|
249 | - $s = str_repeat(' ', -$i).$s; |
|
248 | + if ( 0 > $i ) { |
|
249 | + $s = str_repeat( ' ', -$i ) . $s; |
|
250 | 250 | $len -= $i; |
251 | 251 | $i = 0; |
252 | 252 | } |
253 | 253 | |
254 | - while ($j--) { |
|
255 | - $s[$i + $j] = $uchr[$ulen + $j]; |
|
254 | + while ( $j-- ) { |
|
255 | + $s[ $i + $j ] = $uchr[ $ulen + $j ]; |
|
256 | 256 | } |
257 | 257 | |
258 | - $uchr = substr($uchr, 0, $ulen); |
|
258 | + $uchr = substr( $uchr, 0, $ulen ); |
|
259 | 259 | } |
260 | 260 | } |
261 | - if (isset($combClass[$uchr])) { |
|
261 | + if ( isset( $combClass[ $uchr ] ) ) { |
|
262 | 262 | // Combining chars, for sorting |
263 | 263 | |
264 | - if (!isset($c[$combClass[$uchr]])) { |
|
265 | - $c[$combClass[$uchr]] = ''; |
|
264 | + if ( ! isset( $c[ $combClass[ $uchr ] ] ) ) { |
|
265 | + $c[ $combClass[ $uchr ] ] = ''; |
|
266 | 266 | } |
267 | - $c[$combClass[$uchr]] .= $uchr; |
|
267 | + $c[ $combClass[ $uchr ] ] .= $uchr; |
|
268 | 268 | continue; |
269 | 269 | } |
270 | 270 | } else { |
271 | 271 | // Hangul chars |
272 | 272 | |
273 | - $uchr = unpack('C*', $uchr); |
|
274 | - $j = (($uchr[1] - 224) << 12) + (($uchr[2] - 128) << 6) + $uchr[3] - 0xAC80; |
|
273 | + $uchr = unpack( 'C*', $uchr ); |
|
274 | + $j = ( ( $uchr[ 1 ] - 224 ) << 12 ) + ( ( $uchr[ 2 ] - 128 ) << 6 ) + $uchr[ 3 ] - 0xAC80; |
|
275 | 275 | |
276 | - $uchr = "\xE1\x84".\chr(0x80 + (int) ($j / 588)) |
|
277 | - ."\xE1\x85".\chr(0xA1 + (int) (($j % 588) / 28)); |
|
276 | + $uchr = "\xE1\x84" . \chr( 0x80 + (int)( $j / 588 ) ) |
|
277 | + ."\xE1\x85" . \chr( 0xA1 + (int)( ( $j % 588 ) / 28 ) ); |
|
278 | 278 | |
279 | - if ($j %= 28) { |
|
279 | + if ( $j %= 28 ) { |
|
280 | 280 | $uchr .= $j < 25 |
281 | - ? ("\xE1\x86".\chr(0xA7 + $j)) |
|
282 | - : ("\xE1\x87".\chr(0x67 + $j)); |
|
281 | + ? ( "\xE1\x86" . \chr( 0xA7 + $j ) ) |
|
282 | + : ( "\xE1\x87" . \chr( 0x67 + $j ) ); |
|
283 | 283 | } |
284 | 284 | } |
285 | - if ($c) { |
|
286 | - ksort($c); |
|
287 | - $result .= implode('', $c); |
|
288 | - $c = []; |
|
285 | + if ( $c ) { |
|
286 | + ksort( $c ); |
|
287 | + $result .= implode( '', $c ); |
|
288 | + $c = [ ]; |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | $result .= $uchr; |
292 | 292 | } |
293 | 293 | |
294 | - if ($c) { |
|
295 | - ksort($c); |
|
296 | - $result .= implode('', $c); |
|
294 | + if ( $c ) { |
|
295 | + ksort( $c ); |
|
296 | + $result .= implode( '', $c ); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | return $result; |
300 | 300 | } |
301 | 301 | |
302 | - private static function getData($file) |
|
302 | + private static function getData( $file ) |
|
303 | 303 | { |
304 | - if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) { |
|
304 | + if ( file_exists( $file = __DIR__ . '/Resources/unidata/' . $file . '.php' ) ) { |
|
305 | 305 | return require $file; |
306 | 306 | } |
307 | 307 |
@@ -11,9 +11,9 @@ |
||
11 | 11 | |
12 | 12 | use Symfony\Polyfill\Intl\Normalizer as p; |
13 | 13 | |
14 | -if (!function_exists('normalizer_is_normalized')) { |
|
15 | - function normalizer_is_normalized(?string $string, ?int $form = p\Normalizer::FORM_C): bool { return p\Normalizer::isNormalized((string) $string, (int) $form); } |
|
14 | +if ( ! function_exists( 'normalizer_is_normalized' ) ) { |
|
15 | + function normalizer_is_normalized( ?string $string, ?int $form = p\Normalizer::FORM_C ): bool { return p\Normalizer::isNormalized( (string)$string, (int)$form ); } |
|
16 | 16 | } |
17 | -if (!function_exists('normalizer_normalize')) { |
|
18 | - function normalizer_normalize(?string $string, ?int $form = p\Normalizer::FORM_C): string|false { return p\Normalizer::normalize((string) $string, (int) $form); } |
|
17 | +if ( ! function_exists( 'normalizer_normalize' ) ) { |
|
18 | + function normalizer_normalize( ?string $string, ?int $form = p\Normalizer::FORM_C ): string | false { return p\Normalizer::normalize( (string)$string, (int)$form ); } |
|
19 | 19 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -return array ( |
|
3 | +return array( |
|
4 | 4 | 'À' => 'À', |
5 | 5 | 'Á' => 'Á', |
6 | 6 | 'Â' => 'Â', |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -return array ( |
|
3 | +return array( |
|
4 | 4 | ' ' => ' ', |
5 | 5 | '¨' => ' ̈', |
6 | 6 | 'ª' => 'a', |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -return array ( |
|
3 | +return array( |
|
4 | 4 | 'À' => 'À', |
5 | 5 | 'Á' => 'Á', |
6 | 6 | 'Â' => 'Â', |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -return array ( |
|
3 | +return array( |
|
4 | 4 | '̀' => 230, |
5 | 5 | '́' => 230, |
6 | 6 | '̂' => 230, |
@@ -34,53 +34,53 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @return Process The process that ran |
36 | 36 | */ |
37 | - public function run(OutputInterface $output, $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process |
|
37 | + public function run( OutputInterface $output, $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE ): Process |
|
38 | 38 | { |
39 | - if (!class_exists(Process::class)) { |
|
40 | - throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".'); |
|
39 | + if ( ! class_exists( Process::class ) ) { |
|
40 | + throw new \LogicException( 'The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".' ); |
|
41 | 41 | } |
42 | 42 | |
43 | - if ($output instanceof ConsoleOutputInterface) { |
|
43 | + if ( $output instanceof ConsoleOutputInterface ) { |
|
44 | 44 | $output = $output->getErrorOutput(); |
45 | 45 | } |
46 | 46 | |
47 | - $formatter = $this->getHelperSet()->get('debug_formatter'); |
|
47 | + $formatter = $this->getHelperSet()->get( 'debug_formatter' ); |
|
48 | 48 | |
49 | - if ($cmd instanceof Process) { |
|
50 | - $cmd = [$cmd]; |
|
49 | + if ( $cmd instanceof Process ) { |
|
50 | + $cmd = [ $cmd ]; |
|
51 | 51 | } |
52 | 52 | |
53 | - if (!\is_array($cmd)) { |
|
54 | - throw new \TypeError(sprintf('The "command" argument of "%s()" must be an array or a "%s" instance, "%s" given.', __METHOD__, Process::class, get_debug_type($cmd))); |
|
53 | + if ( ! \is_array( $cmd ) ) { |
|
54 | + throw new \TypeError( sprintf( 'The "command" argument of "%s()" must be an array or a "%s" instance, "%s" given.', __METHOD__, Process::class, get_debug_type( $cmd ) ) ); |
|
55 | 55 | } |
56 | 56 | |
57 | - if (\is_string($cmd[0] ?? null)) { |
|
58 | - $process = new Process($cmd); |
|
59 | - $cmd = []; |
|
60 | - } elseif (($cmd[0] ?? null) instanceof Process) { |
|
61 | - $process = $cmd[0]; |
|
62 | - unset($cmd[0]); |
|
57 | + if ( \is_string( $cmd[ 0 ] ?? null ) ) { |
|
58 | + $process = new Process( $cmd ); |
|
59 | + $cmd = [ ]; |
|
60 | + } elseif ( ( $cmd[ 0 ] ?? null ) instanceof Process ) { |
|
61 | + $process = $cmd[ 0 ]; |
|
62 | + unset( $cmd[ 0 ] ); |
|
63 | 63 | } else { |
64 | - throw new \InvalidArgumentException(sprintf('Invalid command provided to "%s()": the command should be an array whose first element is either the path to the binary to run or a "Process" object.', __METHOD__)); |
|
64 | + throw new \InvalidArgumentException( sprintf( 'Invalid command provided to "%s()": the command should be an array whose first element is either the path to the binary to run or a "Process" object.', __METHOD__ ) ); |
|
65 | 65 | } |
66 | 66 | |
67 | - if ($verbosity <= $output->getVerbosity()) { |
|
68 | - $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine()))); |
|
67 | + if ( $verbosity <= $output->getVerbosity() ) { |
|
68 | + $output->write( $formatter->start( spl_object_hash( $process ), $this->escapeString( $process->getCommandLine() ) ) ); |
|
69 | 69 | } |
70 | 70 | |
71 | - if ($output->isDebug()) { |
|
72 | - $callback = $this->wrapCallback($output, $process, $callback); |
|
71 | + if ( $output->isDebug() ) { |
|
72 | + $callback = $this->wrapCallback( $output, $process, $callback ); |
|
73 | 73 | } |
74 | 74 | |
75 | - $process->run($callback, $cmd); |
|
75 | + $process->run( $callback, $cmd ); |
|
76 | 76 | |
77 | - if ($verbosity <= $output->getVerbosity()) { |
|
78 | - $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode()); |
|
79 | - $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful())); |
|
77 | + if ( $verbosity <= $output->getVerbosity() ) { |
|
78 | + $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf( '%s Command did not run successfully', $process->getExitCode() ); |
|
79 | + $output->write( $formatter->stop( spl_object_hash( $process ), $message, $process->isSuccessful() ) ); |
|
80 | 80 | } |
81 | 81 | |
82 | - if (!$process->isSuccessful() && null !== $error) { |
|
83 | - $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error))); |
|
82 | + if ( ! $process->isSuccessful() && null !== $error ) { |
|
83 | + $output->writeln( sprintf( '<error>%s</error>', $this->escapeString( $error ) ) ); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | return $process; |
@@ -102,12 +102,12 @@ discard block |
||
102 | 102 | * |
103 | 103 | * @see run() |
104 | 104 | */ |
105 | - public function mustRun(OutputInterface $output, $cmd, string $error = null, callable $callback = null): Process |
|
105 | + public function mustRun( OutputInterface $output, $cmd, string $error = null, callable $callback = null ): Process |
|
106 | 106 | { |
107 | - $process = $this->run($output, $cmd, $error, $callback); |
|
107 | + $process = $this->run( $output, $cmd, $error, $callback ); |
|
108 | 108 | |
109 | - if (!$process->isSuccessful()) { |
|
110 | - throw new ProcessFailedException($process); |
|
109 | + if ( ! $process->isSuccessful() ) { |
|
110 | + throw new ProcessFailedException( $process ); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | return $process; |
@@ -116,26 +116,26 @@ discard block |
||
116 | 116 | /** |
117 | 117 | * Wraps a Process callback to add debugging output. |
118 | 118 | */ |
119 | - public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null): callable |
|
119 | + public function wrapCallback( OutputInterface $output, Process $process, callable $callback = null ): callable |
|
120 | 120 | { |
121 | - if ($output instanceof ConsoleOutputInterface) { |
|
121 | + if ( $output instanceof ConsoleOutputInterface ) { |
|
122 | 122 | $output = $output->getErrorOutput(); |
123 | 123 | } |
124 | 124 | |
125 | - $formatter = $this->getHelperSet()->get('debug_formatter'); |
|
125 | + $formatter = $this->getHelperSet()->get( 'debug_formatter' ); |
|
126 | 126 | |
127 | - return function ($type, $buffer) use ($output, $process, $callback, $formatter) { |
|
128 | - $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type)); |
|
127 | + return function( $type, $buffer ) use ( $output, $process, $callback, $formatter ) { |
|
128 | + $output->write( $formatter->progress( spl_object_hash( $process ), $this->escapeString( $buffer ), Process::ERR === $type ) ); |
|
129 | 129 | |
130 | - if (null !== $callback) { |
|
131 | - $callback($type, $buffer); |
|
130 | + if ( null !== $callback ) { |
|
131 | + $callback( $type, $buffer ); |
|
132 | 132 | } |
133 | 133 | }; |
134 | 134 | } |
135 | 135 | |
136 | - private function escapeString(string $str): string |
|
136 | + private function escapeString( string $str ): string |
|
137 | 137 | { |
138 | - return str_replace('<', '\\<', $str); |
|
138 | + return str_replace( '<', '\\<', $str ); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -37,25 +37,25 @@ discard block |
||
37 | 37 | * @param int $indicatorChangeInterval Change interval in milliseconds |
38 | 38 | * @param array|null $indicatorValues Animated indicator characters |
39 | 39 | */ |
40 | - public function __construct(OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null) |
|
40 | + public function __construct( OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null ) |
|
41 | 41 | { |
42 | 42 | $this->output = $output; |
43 | 43 | |
44 | - if (null === $format) { |
|
44 | + if ( null === $format ) { |
|
45 | 45 | $format = $this->determineBestFormat(); |
46 | 46 | } |
47 | 47 | |
48 | - if (null === $indicatorValues) { |
|
49 | - $indicatorValues = ['-', '\\', '|', '/']; |
|
48 | + if ( null === $indicatorValues ) { |
|
49 | + $indicatorValues = [ '-', '\\', '|', '/' ]; |
|
50 | 50 | } |
51 | 51 | |
52 | - $indicatorValues = array_values($indicatorValues); |
|
52 | + $indicatorValues = array_values( $indicatorValues ); |
|
53 | 53 | |
54 | - if (2 > \count($indicatorValues)) { |
|
55 | - throw new InvalidArgumentException('Must have at least 2 indicator value characters.'); |
|
54 | + if ( 2 > \count( $indicatorValues ) ) { |
|
55 | + throw new InvalidArgumentException( 'Must have at least 2 indicator value characters.' ); |
|
56 | 56 | } |
57 | 57 | |
58 | - $this->format = self::getFormatDefinition($format); |
|
58 | + $this->format = self::getFormatDefinition( $format ); |
|
59 | 59 | $this->indicatorChangeInterval = $indicatorChangeInterval; |
60 | 60 | $this->indicatorValues = $indicatorValues; |
61 | 61 | $this->startTime = time(); |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | /** |
65 | 65 | * Sets the current indicator message. |
66 | 66 | */ |
67 | - public function setMessage(?string $message) |
|
67 | + public function setMessage( ?string $message ) |
|
68 | 68 | { |
69 | 69 | $this->message = $message; |
70 | 70 | |
@@ -74,10 +74,10 @@ discard block |
||
74 | 74 | /** |
75 | 75 | * Starts the indicator output. |
76 | 76 | */ |
77 | - public function start(string $message) |
|
77 | + public function start( string $message ) |
|
78 | 78 | { |
79 | - if ($this->started) { |
|
80 | - throw new LogicException('Progress indicator already started.'); |
|
79 | + if ( $this->started ) { |
|
80 | + throw new LogicException( 'Progress indicator already started.' ); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | $this->message = $message; |
@@ -94,17 +94,17 @@ discard block |
||
94 | 94 | */ |
95 | 95 | public function advance() |
96 | 96 | { |
97 | - if (!$this->started) { |
|
98 | - throw new LogicException('Progress indicator has not yet been started.'); |
|
97 | + if ( ! $this->started ) { |
|
98 | + throw new LogicException( 'Progress indicator has not yet been started.' ); |
|
99 | 99 | } |
100 | 100 | |
101 | - if (!$this->output->isDecorated()) { |
|
101 | + if ( ! $this->output->isDecorated() ) { |
|
102 | 102 | return; |
103 | 103 | } |
104 | 104 | |
105 | 105 | $currentTime = $this->getCurrentTimeInMilliseconds(); |
106 | 106 | |
107 | - if ($currentTime < $this->indicatorUpdateTime) { |
|
107 | + if ( $currentTime < $this->indicatorUpdateTime ) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | |
@@ -119,15 +119,15 @@ discard block |
||
119 | 119 | * |
120 | 120 | * @param $message |
121 | 121 | */ |
122 | - public function finish(string $message) |
|
122 | + public function finish( string $message ) |
|
123 | 123 | { |
124 | - if (!$this->started) { |
|
125 | - throw new LogicException('Progress indicator has not yet been started.'); |
|
124 | + if ( ! $this->started ) { |
|
125 | + throw new LogicException( 'Progress indicator has not yet been started.' ); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | $this->message = $message; |
129 | 129 | $this->display(); |
130 | - $this->output->writeln(''); |
|
130 | + $this->output->writeln( '' ); |
|
131 | 131 | $this->started = false; |
132 | 132 | } |
133 | 133 | |
@@ -136,13 +136,13 @@ discard block |
||
136 | 136 | * |
137 | 137 | * @return string|null A format string |
138 | 138 | */ |
139 | - public static function getFormatDefinition(string $name) |
|
139 | + public static function getFormatDefinition( string $name ) |
|
140 | 140 | { |
141 | - if (!self::$formats) { |
|
141 | + if ( ! self::$formats ) { |
|
142 | 142 | self::$formats = self::initFormats(); |
143 | 143 | } |
144 | 144 | |
145 | - return self::$formats[$name] ?? null; |
|
145 | + return self::$formats[ $name ] ?? null; |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | /** |
@@ -150,13 +150,13 @@ discard block |
||
150 | 150 | * |
151 | 151 | * This method also allow you to override an existing placeholder. |
152 | 152 | */ |
153 | - public static function setPlaceholderFormatterDefinition(string $name, callable $callable) |
|
153 | + public static function setPlaceholderFormatterDefinition( string $name, callable $callable ) |
|
154 | 154 | { |
155 | - if (!self::$formatters) { |
|
155 | + if ( ! self::$formatters ) { |
|
156 | 156 | self::$formatters = self::initPlaceholderFormatters(); |
157 | 157 | } |
158 | 158 | |
159 | - self::$formatters[$name] = $callable; |
|
159 | + self::$formatters[ $name ] = $callable; |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
@@ -164,33 +164,33 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @return callable|null A PHP callable |
166 | 166 | */ |
167 | - public static function getPlaceholderFormatterDefinition(string $name) |
|
167 | + public static function getPlaceholderFormatterDefinition( string $name ) |
|
168 | 168 | { |
169 | - if (!self::$formatters) { |
|
169 | + if ( ! self::$formatters ) { |
|
170 | 170 | self::$formatters = self::initPlaceholderFormatters(); |
171 | 171 | } |
172 | 172 | |
173 | - return self::$formatters[$name] ?? null; |
|
173 | + return self::$formatters[ $name ] ?? null; |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | private function display() |
177 | 177 | { |
178 | - if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) { |
|
178 | + if ( OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity() ) { |
|
179 | 179 | return; |
180 | 180 | } |
181 | 181 | |
182 | - $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) { |
|
183 | - if ($formatter = self::getPlaceholderFormatterDefinition($matches[1])) { |
|
184 | - return $formatter($this); |
|
182 | + $this->overwrite( preg_replace_callback( "{%([a-z\-_]+)(?:\:([^%]+))?%}i", function( $matches ) { |
|
183 | + if ( $formatter = self::getPlaceholderFormatterDefinition( $matches[ 1 ] ) ) { |
|
184 | + return $formatter( $this ); |
|
185 | 185 | } |
186 | 186 | |
187 | - return $matches[0]; |
|
188 | - }, $this->format ?? '')); |
|
187 | + return $matches[ 0 ]; |
|
188 | + }, $this->format ?? '' ) ); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | private function determineBestFormat(): string |
192 | 192 | { |
193 | - switch ($this->output->getVerbosity()) { |
|
193 | + switch ( $this->output->getVerbosity() ) { |
|
194 | 194 | // OutputInterface::VERBOSITY_QUIET: display is disabled anyway |
195 | 195 | case OutputInterface::VERBOSITY_VERBOSE: |
196 | 196 | return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi'; |
@@ -205,35 +205,35 @@ discard block |
||
205 | 205 | /** |
206 | 206 | * Overwrites a previous message to the output. |
207 | 207 | */ |
208 | - private function overwrite(string $message) |
|
208 | + private function overwrite( string $message ) |
|
209 | 209 | { |
210 | - if ($this->output->isDecorated()) { |
|
211 | - $this->output->write("\x0D\x1B[2K"); |
|
212 | - $this->output->write($message); |
|
210 | + if ( $this->output->isDecorated() ) { |
|
211 | + $this->output->write( "\x0D\x1B[2K" ); |
|
212 | + $this->output->write( $message ); |
|
213 | 213 | } else { |
214 | - $this->output->writeln($message); |
|
214 | + $this->output->writeln( $message ); |
|
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
218 | 218 | private function getCurrentTimeInMilliseconds(): float |
219 | 219 | { |
220 | - return round(microtime(true) * 1000); |
|
220 | + return round( microtime( true ) * 1000 ); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | private static function initPlaceholderFormatters(): array |
224 | 224 | { |
225 | 225 | return [ |
226 | - 'indicator' => function (self $indicator) { |
|
227 | - return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)]; |
|
226 | + 'indicator' => function( self $indicator ) { |
|
227 | + return $indicator->indicatorValues[ $indicator->indicatorCurrent % \count( $indicator->indicatorValues ) ]; |
|
228 | 228 | }, |
229 | - 'message' => function (self $indicator) { |
|
229 | + 'message' => function( self $indicator ) { |
|
230 | 230 | return $indicator->message; |
231 | 231 | }, |
232 | - 'elapsed' => function (self $indicator) { |
|
233 | - return Helper::formatTime(time() - $indicator->startTime); |
|
232 | + 'elapsed' => function( self $indicator ) { |
|
233 | + return Helper::formatTime( time() - $indicator->startTime ); |
|
234 | 234 | }, |
235 | - 'memory' => function () { |
|
236 | - return Helper::formatMemory(memory_get_usage(true)); |
|
235 | + 'memory' => function() { |
|
236 | + return Helper::formatMemory( memory_get_usage( true ) ); |
|
237 | 237 | }, |
238 | 238 | ]; |
239 | 239 | } |