1 | <?php |
||
17 | abstract class Cli |
||
18 | { |
||
19 | /** |
||
20 | * List of console color codes. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $ansiCodes = [ |
||
25 | 'bold' => 1, |
||
26 | 'fg-black' => 30, |
||
27 | 'fg-red' => 31, |
||
28 | 'fg-green' => 32, |
||
29 | 'fg-yellow' => 33, |
||
30 | 'fg-cyan' => 36, |
||
31 | 'fg-white' => 37, |
||
32 | 'bg-red' => 41, |
||
33 | 'bg-green' => 42, |
||
34 | 'bg-yellow' => 43 |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Optional command locations |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private static $optionalCommandLocations = [ |
||
43 | 'mongodump' => [], |
||
44 | 'mysqldump' => [ |
||
45 | '/usr/local/mysql/bin', // Mac OS X |
||
46 | '/usr/mysql/bin', // Linux |
||
47 | ], |
||
48 | 'tar' => [], |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * Adds a new 'path' to the list of optional command locations. |
||
53 | * |
||
54 | * @param string $command |
||
55 | * @param string $path |
||
56 | */ |
||
57 | 1 | public static function addCommandLocation($command, $path) |
|
61 | |||
62 | /** |
||
63 | * Returns the list of optional 'mysqldump' locations. |
||
64 | * |
||
65 | * @param string $command |
||
66 | * @return array |
||
67 | */ |
||
68 | 216 | public static function getCommandLocations($command) : array |
|
72 | |||
73 | /** |
||
74 | * Detect a given command's location. |
||
75 | * |
||
76 | * @param string $cmd The command to locate |
||
77 | * @param string $path Directory where the command should be |
||
78 | * @param array $optionalLocations Some fallback locations where to search for the command |
||
79 | * @return string Absolute path to detected command including command itself |
||
80 | * @throws \RuntimeException |
||
81 | */ |
||
82 | public static function detectCmdLocation(string $cmd, string $path = '', array $optionalLocations = []) : string |
||
83 | { |
||
84 | $detectionSteps = [ |
||
85 | 219 | function($cmd) use ($path) { |
|
86 | 219 | if (!empty($path)) { |
|
87 | 213 | return self::detectCmdLocationInPath($cmd, $path); |
|
88 | } |
||
89 | 6 | return ''; |
|
90 | 219 | }, |
|
91 | 219 | function($cmd) { |
|
92 | 6 | return self::detectCmdLocationWithWhich($cmd); |
|
93 | 219 | }, |
|
94 | 219 | function($cmd) { |
|
95 | 2 | $paths = explode(PATH_SEPARATOR, self::getEnvPath()); |
|
96 | 2 | return self::detectCmdLocationInPaths($cmd, $paths); |
|
97 | 219 | }, |
|
98 | 219 | function($cmd) use ($optionalLocations) { |
|
99 | 2 | return self::detectCmdLocationInPaths($cmd, $optionalLocations); |
|
100 | 219 | } |
|
101 | ]; |
||
102 | |||
103 | 219 | foreach ($detectionSteps as $step) { |
|
104 | 219 | $bin = $step($cmd); |
|
105 | 218 | if (!empty($bin)) { |
|
106 | 218 | return $bin; |
|
107 | } |
||
108 | } |
||
109 | |||
110 | 1 | throw new RuntimeException(sprintf('\'%s\' was nowhere to be found please specify the correct path', $cmd)); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Detect a command in a given path. |
||
115 | * |
||
116 | * @param string $cmd |
||
117 | * @param string $path |
||
118 | * @return string |
||
119 | * @throws \RuntimeException |
||
120 | */ |
||
121 | 213 | public static function detectCmdLocationInPath(string $cmd, string $path) : string |
|
122 | { |
||
123 | 213 | $command = $path . DIRECTORY_SEPARATOR . $cmd; |
|
124 | 213 | $bin = self::isExecutable($command); |
|
125 | 213 | if (empty($bin)) { |
|
126 | 1 | throw new RuntimeException(sprintf('wrong path specified for \'%s\': %s', $cmd, $path)); |
|
127 | } |
||
128 | 212 | return $bin; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Detect command location using which cli command. |
||
133 | * |
||
134 | * @param string $cmd |
||
135 | * @return string |
||
136 | */ |
||
137 | 6 | public static function detectCmdLocationWithWhich($cmd) : string |
|
148 | |||
149 | /** |
||
150 | * Check path list for executable command. |
||
151 | * |
||
152 | * @param string $cmd |
||
153 | * @param array $paths |
||
154 | * @return string |
||
155 | */ |
||
156 | 2 | public static function detectCmdLocationInPaths($cmd, array $paths) : string |
|
157 | { |
||
158 | 2 | foreach ($paths as $path) { |
|
159 | 2 | $command = $path . DIRECTORY_SEPARATOR . $cmd; |
|
160 | 2 | $bin = self::isExecutable($command); |
|
161 | 2 | if (!empty($bin)) { |
|
162 | 2 | return $bin; |
|
163 | } |
||
164 | } |
||
165 | 2 | return ''; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Return local $PATH variable. |
||
170 | * |
||
171 | * @return string |
||
172 | * @throws \RuntimeException |
||
173 | */ |
||
174 | 3 | public static function getEnvPath() : string |
|
184 | |||
185 | /** |
||
186 | * Returns the executable command if the command is executable, null otherwise. |
||
187 | * Search for $command.exe on Windows systems. |
||
188 | * |
||
189 | * @param string $command |
||
190 | * @return string |
||
191 | */ |
||
192 | 219 | public static function isExecutable($command) : string |
|
206 | |||
207 | /** |
||
208 | * Formats a buffer with a specified ANSI color sequence if colors are enabled. |
||
209 | * |
||
210 | * @author Sebastian Bergmann <[email protected]> |
||
211 | * @param string $color |
||
212 | * @param string $buffer |
||
213 | * @return string |
||
214 | */ |
||
215 | 3 | public static function formatWithColor(string $color, string $buffer) : string |
|
234 | |||
235 | /** |
||
236 | * Fills up a text buffer with '*' to consume 72 chars. |
||
237 | * |
||
238 | * @param string $buffer |
||
239 | * @param int $length |
||
240 | * @return string |
||
241 | */ |
||
242 | 14 | public static function formatWithAsterisk(string $buffer, int $length = 75) : string |
|
246 | |||
247 | /** |
||
248 | * Can command pipe operator be used. |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | 3 | public static function canPipe() : bool |
|
256 | |||
257 | /** |
||
258 | * Removes a directory that is not empty. |
||
259 | * |
||
260 | * @param string $dir |
||
261 | */ |
||
262 | 1 | public static function removeDir(string $dir) |
|
276 | } |
||
277 |