@@ -13,22 +13,22 @@ private function __construct()
13
13
/**
14
14
* Execute command with params.
15
15
*
16
- * @param string $commandLine
16
+ * @param string $command
17
17
* @param array $params
18
18
*
19
19
* @return bool|string
20
20
*
21
21
* @throws \Exception
22
22
*/
23
- public static function exec ($ commandLine , array $ params = array ())
23
+ public static function exec ($ command , array $ params = array ())
24
24
{
25
- if (empty ($ commandLine )) {
25
+ if (empty ($ command )) {
26
26
throw new \Exception ('Command line is empty ' );
27
27
}
28
28
29
- $ commandLine = self ::bindParams ($ commandLine , $ params );
29
+ $ command = self ::bindParams ($ command , $ params );
30
30
31
- exec ($ commandLine , $ output , $ code );
31
+ exec ($ command , $ output , $ code );
32
32
33
33
if (count ($ output ) === 0 ) {
34
34
$ output = $ code ;
@@ -37,7 +37,7 @@ public static function exec($commandLine, array $params = array())
37
37
}
38
38
39
39
if ($ code !== 0 ) {
40
- throw new \Exception ($ output . ' Command line: ' . $ commandLine );
40
+ throw new \Exception ($ output . ' Command line: ' . $ command );
41
41
}
42
42
43
43
return $ output ;
@@ -46,38 +46,26 @@ public static function exec($commandLine, array $params = array())
46
46
/**
47
47
* Bind params to command.
48
48
*
49
- * @param string $commandLine
49
+ * @param string $command
50
50
* @param array $params
51
51
*
52
52
* @return string
53
53
*/
54
- public static function bindParams ($ commandLine , array $ params )
54
+ public static function bindParams ($ command , array $ params )
55
55
{
56
- if (count ($ params ) > 0 ) {
57
- $ wrapper = function ($ string ) {
58
- return '{ ' . $ string . '} ' ;
59
- };
60
- $ converter = function ($ var ) {
61
- if (is_array ($ var )) {
62
- $ var = implode (' ' , $ var );
63
- }
64
-
65
- return $ var ;
66
- };
56
+ $ wrappers = array ();
57
+ $ converters = array ();
58
+ foreach ($ params as $ key => $ value ) {
67
59
68
- $ commandLine = str_replace (
69
- array_map (
70
- $ wrapper ,
71
- array_keys ($ params )
72
- ),
73
- array_map (
74
- $ converter ,
75
- array_values ($ params )
76
- ),
77
- $ commandLine
78
- );
60
+ // Escaped
61
+ $ wrappers [] = '{ ' . $ key . '} ' ;
62
+ $ converters [] = escapeshellarg (is_array ($ value ) ? implode (' ' , $ value ) : $ value );
63
+
64
+ // Unescaped
65
+ $ wrappers [] = '{! ' . $ key . '!} ' ;
66
+ $ converters [] = is_array ($ value ) ? implode (' ' , $ value ) : $ value ;
79
67
}
80
68
81
- return $ commandLine ;
69
+ return str_replace ( $ wrappers , $ converters , $ command ) ;
82
70
}
83
71
}
0 commit comments