1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* (c) Anton Medvedev <[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 | |||
11 | namespace Deployer\Support; |
||
12 | |||
13 | function array_flatten(array $array): array |
||
14 | { |
||
15 | $flatten = []; |
||
16 | array_walk_recursive($array, function ($value) use (&$flatten) { |
||
17 | $flatten[] = $value; |
||
18 | 16 | }); |
|
19 | return $flatten; |
||
20 | 16 | } |
|
21 | 16 | ||
22 | 16 | /** |
|
23 | * Recursively merge two config arrays with a specific behavior: |
||
24 | * |
||
25 | * 1. scalar values are overridden |
||
26 | * 2. array values are extended uniquely if all keys are numeric |
||
27 | * 3. all other array values are merged |
||
28 | */ |
||
29 | function array_merge_alternate(array $original, array $override): array |
||
30 | { |
||
31 | foreach ($override as $key => $value) { |
||
32 | if (isset($original[$key])) { |
||
33 | if (!is_array($original[$key])) { |
||
34 | if (is_numeric($key)) { |
||
35 | // Append scalar value |
||
36 | $original[] = $value; |
||
37 | } else { |
||
38 | // Override scalar value |
||
39 | 16 | $original[$key] = $value; |
|
40 | 7 | } |
|
41 | 7 | } elseif (array_keys($original[$key]) === range(0, count($original[$key]) - 1)) { |
|
42 | 7 | // Uniquely append to array with numeric keys |
|
43 | $original[$key] = array_unique(array_merge($original[$key], $value)); |
||
44 | 6 | } else { |
|
45 | // Merge all other arrays |
||
46 | $original[$key] = array_merge_alternate($original[$key], $value); |
||
47 | 7 | } |
|
48 | } else { |
||
49 | 2 | // Simply add new key/value |
|
50 | $original[$key] = $value; |
||
51 | 2 | } |
|
52 | } |
||
53 | |||
54 | 7 | return $original; |
|
55 | } |
||
56 | |||
57 | function env_stringify(array $array): string |
||
58 | 2 | { |
|
59 | return implode(' ', array_map( |
||
60 | function ($key, $value) { |
||
61 | return sprintf("%s=%s", $key, escapeshellarg((string) $value)); |
||
62 | 16 | }, |
|
63 | array_keys($array), |
||
64 | $array, |
||
65 | )); |
||
66 | } |
||
67 | |||
68 | function is_closure(mixed $var): bool |
||
69 | { |
||
70 | return is_object($var) && ($var instanceof \Closure); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | 5 | * Check if all elements satisfy predicate. |
|
75 | */ |
||
76 | function array_all(array $array, callable $predicate): bool |
||
77 | { |
||
78 | foreach ($array as $key => $value) { |
||
79 | if (!$predicate($value, $key)) { |
||
80 | return false; |
||
81 | } |
||
82 | } |
||
83 | return true; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Cleanup CRLF new line endings. |
||
88 | */ |
||
89 | function normalize_line_endings(string $string): string |
||
90 | { |
||
91 | return str_replace(["\r\n", "\r"], "\n", $string); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Expand leading tilde (~) symbol in given path. |
||
96 | */ |
||
97 | function parse_home_dir(string $path): string |
||
98 | { |
||
99 | if ('~' === $path || str_starts_with($path, '~/')) { |
||
100 | if (isset($_SERVER['HOME'])) { |
||
101 | 1 | $home = $_SERVER['HOME']; |
|
102 | } elseif (isset($_SERVER['HOMEDRIVE'], $_SERVER['HOMEPATH'])) { |
||
103 | 1 | $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
|
104 | 1 | } else { |
|
105 | 1 | return $path; |
|
106 | } |
||
107 | |||
108 | return $home . substr($path, 1); |
||
109 | } |
||
110 | |||
111 | return $path; |
||
112 | } |
||
113 | |||
114 | function find_line_number(string $source, string $string): int |
||
115 | { |
||
116 | $string = explode(PHP_EOL, $string)[0]; |
||
117 | $before = strstr($source, $string, true); |
||
118 | 39 | if (false !== $before) { |
|
119 | return count(explode(PHP_EOL, $before)); |
||
120 | } |
||
121 | return 1; |
||
122 | } |
||
123 | |||
124 | function colorize_host(string $alias): string |
||
125 | { |
||
126 | if (defined('NO_ANSI')) { |
||
127 | return $alias; |
||
128 | } |
||
129 | |||
130 | 13 | if (in_array($alias, ['localhost', 'local'], true)) { |
|
131 | 13 | return $alias; |
|
132 | 1 | } |
|
133 | |||
134 | if (getenv('COLORTERM') === 'truecolor') { |
||
135 | 13 | $hsv = function ($h, $s, $v) { |
|
136 | $r = $g = $b = $i = $f = $p = $q = $t = 0; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
137 | $i = floor($h * 6); |
||
138 | $f = $h * 6 - $i; |
||
139 | $p = $v * (1 - $s); |
||
140 | $q = $v * (1 - $f * $s); |
||
141 | $t = $v * (1 - (1 - $f) * $s); |
||
142 | switch ($i % 6) { |
||
143 | case 0: |
||
144 | $r = $v; |
||
145 | $g = $t; |
||
146 | $b = $p; |
||
147 | 33 | break; |
|
148 | case 1: |
||
149 | $r = $q; |
||
150 | $g = $v; |
||
151 | $b = $p; |
||
152 | break; |
||
153 | case 2: |
||
154 | $r = $p; |
||
155 | $g = $v; |
||
156 | $b = $t; |
||
157 | break; |
||
158 | 3 | case 3: |
|
159 | 3 | $r = $p; |
|
160 | 3 | $g = $q; |
|
161 | $b = $v; |
||
162 | break; |
||
163 | case 4: |
||
164 | $r = $t; |
||
165 | $g = $p; |
||
166 | $b = $v; |
||
167 | 3 | break; |
|
168 | case 5: |
||
169 | $r = $v; |
||
170 | 1 | $g = $p; |
|
171 | $b = $q; |
||
172 | break; |
||
173 | } |
||
174 | $r = round($r * 255); |
||
175 | $g = round($g * 255); |
||
176 | $b = round($b * 255); |
||
177 | return "\x1b[38;2;{$r};{$g};{$b}m"; |
||
178 | }; |
||
179 | $total = 100; |
||
180 | $colors = []; |
||
181 | for ($i = 0; $i < $total; $i++) { |
||
182 | $colors[] = $hsv($i / $total, .5, .9); |
||
183 | } |
||
184 | if ($alias === 'prod' || $alias === 'production') { |
||
185 | return "$colors[99]$alias\x1b[0m"; |
||
186 | } |
||
187 | if ($alias === 'beta') { |
||
188 | return "$colors[14]$alias\x1b[0m"; |
||
189 | } |
||
190 | $tag = $colors[abs(crc32($alias)) % count($colors)]; |
||
191 | return "$tag$alias\x1b[0m"; |
||
192 | } |
||
193 | |||
194 | $colors = [ |
||
195 | 'fg=cyan;options=bold', |
||
196 | 'fg=green;options=bold', |
||
197 | 'fg=yellow;options=bold', |
||
198 | 'fg=cyan', |
||
199 | 'fg=blue', |
||
200 | 'fg=yellow', |
||
201 | 'fg=magenta', |
||
202 | 'fg=blue;options=bold', |
||
203 | 'fg=green', |
||
204 | 'fg=magenta;options=bold', |
||
205 | 'fg=red;options=bold', |
||
206 | ]; |
||
207 | $tag = $colors[abs(crc32($alias)) % count($colors)]; |
||
208 | return "<$tag>$alias</>"; |
||
209 | } |
||
210 | |||
211 | function escape_shell_argument(string $argument): string |
||
212 | { |
||
213 | return "'" . str_replace("'", "'\\''", $argument) . "'"; |
||
214 | } |
||
215 | |||
216 | function deployer_root(): string |
||
217 | { |
||
218 | if (getenv('DEPLOYER_ROOT') !== false) { |
||
219 | return getenv('DEPLOYER_ROOT'); |
||
220 | } else { |
||
221 | if (defined('DEPLOYER_DEPLOY_FILE')) { |
||
222 | return dirname(DEPLOYER_DEPLOY_FILE); |
||
0 ignored issues
–
show
|
|||
223 | } else { |
||
224 | return getcwd(); |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 |