1 | <?php |
||
7 | final class Command |
||
8 | { |
||
9 | private function __construct() |
||
12 | |||
13 | /** |
||
14 | * Execute command with params. |
||
15 | * |
||
16 | * @param string $command |
||
17 | * @param array $params |
||
18 | * @param bool $escape True to escape each param using escapeshellarg |
||
19 | * |
||
20 | * @return bool|string |
||
21 | * |
||
22 | * @throws \Exception |
||
23 | */ |
||
24 | 15 | public static function exec($command, array $params = array(), $escape=true) |
|
25 | { |
||
26 | 15 | if (empty($command)) { |
|
27 | 3 | throw new \InvalidArgumentException('Command line is empty'); |
|
28 | } |
||
29 | |||
30 | 12 | $command = self::bindParams($command, $params, $escape); |
|
31 | |||
32 | 12 | exec($command, $output, $code); |
|
33 | |||
34 | 12 | if (count($output) === 0) { |
|
35 | 3 | $output = $code; |
|
36 | 3 | } else { |
|
37 | 9 | $output = implode(PHP_EOL, $output); |
|
38 | } |
||
39 | |||
40 | 12 | if ($code !== 0) { |
|
41 | 3 | throw new CommandException($command, $output, $code); |
|
42 | } |
||
43 | |||
44 | 9 | return $output; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Bind params to command. |
||
49 | * |
||
50 | * @param string $command |
||
51 | * @param array $params |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 12 | public static function bindParams($command, array $params, $escape) |
|
88 | } |
||
89 |