10
10
11
11
class Local {
12
12
13
- private $ handle = NULL ;
14
- private $ pipes = array ();
15
- private $ loghandle = NULL ;
16
13
public $ pid = NULL ;
17
14
18
15
public function __construct () {
@@ -25,11 +22,30 @@ public function __destruct() {
25
22
}
26
23
27
24
public function isRunning () {
28
- if (is_null ( $ this ->handle ) )
25
+ if ($ this ->pid == NULL )
29
26
return False ;
30
-
31
- $ status = proc_get_status ($ this ->handle );
32
- return is_null ($ status ["running " ]) ? false : $ status ["running " ];
27
+ if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' )
28
+ {
29
+ $ processes = explode ( "\n" , shell_exec ( "tasklist.exe " ));
30
+ foreach ( $ processes as $ process )
31
+ {
32
+ if ( strpos ( "Image Name " , $ process ) === 0 || strpos ( "=== " , $ process ) === 0 )
33
+ continue ;
34
+ $ matches = false ;
35
+ preg_match ( "/(.*?)\s+(\d+).*$/ " , $ process , $ matches );
36
+ $ this ->pid = $ matches [ 2 ];
37
+ return True ;
38
+ }
39
+ return False ;
40
+ }
41
+ else {
42
+ $ return_message = shell_exec ("ps - " . "$ this ->pid " . "| wc -l " );
43
+ if (intval ($ return_message ) > 1 )
44
+ {
45
+ return True ;
46
+ }
47
+ return False ;
48
+ }
33
49
}
34
50
35
51
public function add_args ($ arg_key , $ value = NULL ) {
@@ -82,78 +98,49 @@ public function start($arguments) {
82
98
$ this ->binary = new LocalBinary ();
83
99
$ this ->binary_path = $ this ->binary ->binary_path ();
84
100
85
- $ descriptorspec = array (
86
- 0 => array ("pipe " , "r " ),
87
- 1 => array ("pipe " , "w " ),
88
- 2 => array ("pipe " , "w " )
89
- );
90
-
91
- $ call = $ this ->command ();
101
+ $ call = $ this ->start_command ();
92
102
if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' )
93
103
system ('echo "" > ' . '$this->logfile ' );
94
104
else
95
105
system ("echo \"\" > ' $ this ->logfile ' " );
96
-
97
- $ this ->handle = proc_open ($ call , $ descriptorspec , $ this ->pipes );
98
- $ status = proc_get_status ($ this ->handle );
99
- $ this ->pid = $ status ['pid ' ];
100
-
101
- $ this ->loghandle = fopen ($ this ->logfile ,"r " );
102
- while (true ) {
103
- $ buffer = fread ($ this ->loghandle , 1024 );
104
- if (preg_match ("/Error:[^ \n]+/i " , $ buffer , $ match )) {
105
- $ this ->stop ();
106
- throw new LocalException ($ match [0 ]);
107
- break ;
108
- }
109
- elseif (preg_match ("/\bPress Ctrl-C to exit\b/i " , $ buffer , $ match )){
110
- fclose ($ this ->loghandle );
111
- break ;
112
- }
113
-
114
- //flush();
115
- sleep (1 );
106
+ $ call = $ call . "2>&1 " ;
107
+ $ return_message = shell_exec ($ call );
108
+ $ data = json_decode ($ return_message ,true );
109
+ if ($ data ["state " ] != "connected " ) {
110
+ throw new LocalException ($ data ['message ' ]);
116
111
}
112
+ $ this ->pid = $ data ['pid ' ];
117
113
}
118
114
119
115
public function stop () {
120
- fclose ($ this ->loghandle );
121
- if (is_null ($ this ->handle ))
122
- return ;
123
- else {
124
- while (fgets ($ this ->pipes [0 ]));
125
- fclose ($ this ->pipes [0 ]);
126
- fclose ($ this ->pipes [1 ]);
127
- fclose ($ this ->pipes [2 ]);
128
-
129
- if (strtoupper (substr (PHP_OS , 0 , 3 )) !== 'WIN ' ) {
130
- exec ('kill -15 ' . $ this ->pid );
131
- } else {
132
- $ binary_pid = proc_get_status ($ this ->handle )['pid ' ];
133
- $ wmic_output = shell_exec ('wmic process where (ParentProcessId= ' . $ binary_pid . ') get Caption,ProcessId ' );
134
- preg_match_all ('!\d+! ' , $ wmic_output , $ possible_pids );
135
- if (!empty ($ possible_pids [0 ][0 ])) {
136
- exec ("taskkill /F /PID " . $ possible_pids [0 ][0 ]);
137
- }
138
- }
139
-
140
- proc_terminate ($ this ->handle );
141
- while ($ this ->isRunning ())
142
- sleep (1 );
143
- }
116
+ $ call = $ this ->stop_command ();
117
+ shell_exec ("$ call " );
118
+ }
119
+
120
+ public function start_command () {
121
+ $ exec = "exec " ;
122
+ // TODO to test on windows
123
+ if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' )
124
+ $ exec = "call " ;
125
+
126
+ $ user_args = join (' ' , $ this ->user_args );
127
+ $ command = "$ exec $ this ->binary_path -d start -logFile ' $ this ->logfile ' $ this ->folder_flag $ this ->key $ this ->folder_path $ this ->force_local_flag $ this ->local_identifier_flag $ this ->only_flag $ this ->only_automate_flag $ this ->proxy_host $ this ->proxy_port $ this ->proxy_user $ this ->proxy_pass $ this ->force_proxy_flag $ this ->force_flag $ this ->verbose_flag $ this ->hosts $ user_args " ;
128
+ $ command = preg_replace ('/\s+/S ' , " " , $ command );
129
+ return $ command ;
144
130
}
145
131
146
- public function command () {
132
+ public function stop_command () {
147
133
$ exec = "exec " ;
148
134
// TODO to test on windows
149
135
if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' )
150
136
$ exec = "call " ;
151
137
152
138
$ user_args = join (' ' , $ this ->user_args );
153
- $ command = "$ exec $ this ->binary_path -logFile ' $ this -> logfile ' $ this ->folder_flag $ this -> key $ this -> folder_path $ this -> force_local_flag $ this -> local_identifier_flag $ this -> only_flag $ this -> only_automate_flag $ this -> proxy_host $ this -> proxy_port $ this -> proxy_user $ this -> proxy_pass $ this -> force_proxy_flag $ this -> force_flag $ this -> verbose_flag $ this -> hosts $ user_args " ;
139
+ $ command = "$ exec $ this ->binary_path -d stop $ this ->local_identifier_flag " ;
154
140
$ command = preg_replace ('/\s+/S ' , " " , $ command );
155
141
return $ command ;
156
142
}
143
+
157
144
}
158
145
159
146
?>
0 commit comments