@@ -13,22 +13,22 @@ private function __construct()
1313 /**
1414 * Execute command with params.
1515 *
16- * @param string $commandLine
16+ * @param string $command
1717 * @param array $params
1818 *
1919 * @return bool|string
2020 *
2121 * @throws \Exception
2222 */
23- public static function exec ($ commandLine , array $ params = array ())
23+ public static function exec ($ command , array $ params = array ())
2424 {
25- if (empty ($ commandLine )) {
25+ if (empty ($ command )) {
2626 throw new \Exception ('Command line is empty ' );
2727 }
2828
29- $ commandLine = self ::bindParams ($ commandLine , $ params );
29+ $ command = self ::bindParams ($ command , $ params );
3030
31- exec ($ commandLine , $ output , $ code );
31+ exec ($ command , $ output , $ code );
3232
3333 if (count ($ output ) === 0 ) {
3434 $ output = $ code ;
@@ -37,7 +37,7 @@ public static function exec($commandLine, array $params = array())
3737 }
3838
3939 if ($ code !== 0 ) {
40- throw new \Exception ($ output . ' Command line: ' . $ commandLine );
40+ throw new \Exception ($ output . ' Command line: ' . $ command );
4141 }
4242
4343 return $ output ;
@@ -46,38 +46,26 @@ public static function exec($commandLine, array $params = array())
4646 /**
4747 * Bind params to command.
4848 *
49- * @param string $commandLine
49+ * @param string $command
5050 * @param array $params
5151 *
5252 * @return string
5353 */
54- public static function bindParams ($ commandLine , array $ params )
54+ public static function bindParams ($ command , array $ params )
5555 {
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 ) {
6759
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 ;
7967 }
8068
81- return $ commandLine ;
69+ return str_replace ( $ wrappers , $ converters , $ command ) ;
8270 }
8371}
0 commit comments