@@ -24,6 +24,10 @@ class Shell
24
24
const STDOUT_DESCRIPTOR_KEY = 1 ;
25
25
const STDERR_DESCRIPTOR_KEY = 2 ;
26
26
27
+ const STATE_READY = 'ready ' ;
28
+ const STATE_STARTED = 'started ' ;
29
+ const STATE_TERMINATED = 'terminated ' ;
30
+
27
31
/** @var string Command to be executed */
28
32
protected $ command ;
29
33
@@ -42,8 +46,11 @@ class Shell
42
46
/** @var resource The actual process resource returned from proc_open */
43
47
protected $ process = null ;
44
48
49
+ /** @var string Current state of the shell execution */
50
+ protected $ state = self ::STATE_READY ;
51
+
45
52
/** @var string Status of the process as returned from proc_get_status */
46
- protected $ status = null ;
53
+ protected $ processStatus = null ;
47
54
48
55
public function __construct (string $ command , string $ input = null )
49
56
{
@@ -71,12 +78,16 @@ protected function setInput()
71
78
\fwrite ($ this ->pipes [self ::STDIN_DESCRIPTOR_KEY ], $ this ->input );
72
79
}
73
80
74
- protected function updateStatus ()
81
+ protected function updateProcessStatus ()
75
82
{
76
- $ this ->status = \proc_get_status ($ this ->process );
83
+ if (self ::STATE_STARTED !== $ this ->state ) {
84
+ return ;
85
+ }
86
+
87
+ $ this ->processStatus = \proc_get_status ($ this ->process );
77
88
78
- if ($ this ->status ['running ' ] === false && $ this ->exitCode === null ) {
79
- $ this ->exitCode = $ this ->status ['exitcode ' ];
89
+ if ($ this ->processStatus ['running ' ] === false && $ this ->exitCode === null ) {
90
+ $ this ->exitCode = $ this ->processStatus ['exitcode ' ];
80
91
}
81
92
}
82
93
@@ -101,15 +112,17 @@ public function execute()
101
112
throw new RuntimeException ('Bad program could not be started. ' );
102
113
}
103
114
115
+ $ this ->state = self ::STATE_STARTED ;
116
+
104
117
$ this ->setInput ();
105
- $ this ->updateStatus ();
118
+ $ this ->updateProcessStatus ();
106
119
}
107
120
108
- public function getStatus ()
121
+ public function getState ()
109
122
{
110
- $ this ->updateStatus ();
123
+ $ this ->updateProcessStatus ();
111
124
112
- return $ this ->status ;
125
+ return $ this ->state ;
113
126
}
114
127
115
128
public function getOutput ()
@@ -124,21 +137,25 @@ public function getErrorOutput()
124
137
125
138
public function getExitCode ()
126
139
{
127
- $ this ->updateStatus ();
140
+ $ this ->updateProcessStatus ();
128
141
129
142
return $ this ->exitCode ;
130
143
}
131
144
132
145
public function isRunning ()
133
146
{
134
- $ this ->updateStatus ();
147
+ if (self ::STATE_STARTED !== $ this ->state ) {
148
+ return false ;
149
+ }
150
+
151
+ $ this ->updateProcessStatus ();
135
152
136
- return $ this ->status ['running ' ];
153
+ return $ this ->processStatus ['running ' ];
137
154
}
138
155
139
156
public function getProcessId ()
140
157
{
141
- return $ this ->isRunning () ? $ this ->status ['pid ' ] : null ;
158
+ return $ this ->isRunning () ? $ this ->processStatus ['pid ' ] : null ;
142
159
}
143
160
144
161
public function stop ()
@@ -149,7 +166,8 @@ public function stop()
149
166
\proc_close ($ this ->process );
150
167
}
151
168
152
- $ this ->exitCode = $ this ->status ['exitcode ' ];
169
+ $ this ->state = self ::STATE_TERMINATED ;
170
+ $ this ->exitCode = $ this ->processStatus ['exitcode ' ];
153
171
154
172
return $ this ->exitCode ;
155
173
}
0 commit comments