@@ -31,7 +31,7 @@ class ForbiddenException extends \Exception {}
31
31
* Handles communication with a FastCGI application
32
32
*
33
33
* @author Pierrick Charron <pierrick@adoy.net>
34
- * @version 1.0.0
34
+ * @version 1.0
35
35
*/
36
36
class Client
37
37
{
@@ -74,25 +74,25 @@ class Client
74
74
75
75
/**
76
76
* Socket
77
- * @var Resource
77
+ * @var resource
78
78
*/
79
79
private $ _sock = null ;
80
80
81
81
/**
82
82
* Host
83
- * @var String
83
+ * @var string
84
84
*/
85
85
private $ _host = null ;
86
86
87
87
/**
88
88
* Port
89
- * @var Integer
89
+ * @var int
90
90
*/
91
91
private $ _port = null ;
92
92
93
93
/**
94
94
* Keep Alive
95
- * @var Boolean
95
+ * @var bool
96
96
*/
97
97
private $ _keepAlive = false ;
98
98
@@ -112,43 +112,53 @@ class Client
112
112
113
113
/**
114
114
* Use persistent sockets to connect to backend
115
- * @var Boolean
115
+ * @var bool
116
116
*/
117
117
private $ _persistentSocket = false ;
118
118
119
119
/**
120
120
* Connect timeout in milliseconds
121
- * @var Integer
121
+ * @var int
122
122
*/
123
123
private $ _connectTimeout = 5000 ;
124
124
125
125
/**
126
126
* Read/Write timeout in milliseconds
127
- * @var Integer
127
+ * @var int
128
128
*/
129
129
private $ _readWriteTimeout = 5000 ;
130
130
131
131
/**
132
132
* Constructor
133
133
*
134
- * @param String $host Host of the FastCGI application
135
- * @param Integer $port Port of the FastCGI application
134
+ * @param string $host Host of the FastCGI application
135
+ * @param int $port Port of the FastCGI application
136
136
*/
137
137
public function __construct ($ host , $ port )
138
138
{
139
139
$ this ->_host = $ host ;
140
140
$ this ->_port = $ port ;
141
141
}
142
142
143
+ /**
144
+ * Get host.
145
+ *
146
+ * @return string
147
+ */
148
+ public function getHost ()
149
+ {
150
+ return $ this ->_host ;
151
+ }
152
+
143
153
/**
144
154
* Define whether or not the FastCGI application should keep the connection
145
155
* alive at the end of a request
146
156
*
147
- * @param Boolean $b true if the connection should stay alive, false otherwise
157
+ * @param bool $b true if the connection should stay alive, false otherwise
148
158
*/
149
159
public function setKeepAlive ($ b )
150
160
{
151
- $ this ->_keepAlive = (boolean )$ b ;
161
+ $ this ->_keepAlive = (bool )$ b ;
152
162
if (!$ this ->_keepAlive && $ this ->_sock ) {
153
163
fclose ($ this ->_sock );
154
164
}
@@ -157,7 +167,7 @@ public function setKeepAlive($b)
157
167
/**
158
168
* Get the keep alive status
159
169
*
160
- * @return Boolean true if the connection should stay alive, false otherwise
170
+ * @return bool true if the connection should stay alive, false otherwise
161
171
*/
162
172
public function getKeepAlive ()
163
173
{
@@ -168,12 +178,12 @@ public function getKeepAlive()
168
178
* Define whether or not PHP should attempt to re-use sockets opened by previous
169
179
* request for efficiency
170
180
*
171
- * @param Boolean $b true if persistent socket should be used, false otherwise
181
+ * @param bool $b true if persistent socket should be used, false otherwise
172
182
*/
173
183
public function setPersistentSocket ($ b )
174
184
{
175
185
$ was_persistent = ($ this ->_sock && $ this ->_persistentSocket );
176
- $ this ->_persistentSocket = (boolean )$ b ;
186
+ $ this ->_persistentSocket = (bool )$ b ;
177
187
if (!$ this ->_persistentSocket && $ was_persistent ) {
178
188
fclose ($ this ->_sock );
179
189
}
@@ -182,7 +192,7 @@ public function setPersistentSocket($b)
182
192
/**
183
193
* Get the pesistent socket status
184
194
*
185
- * @return Boolean true if the socket should be persistent, false otherwise
195
+ * @return bool true if the socket should be persistent, false otherwise
186
196
*/
187
197
public function getPersistentSocket ()
188
198
{
@@ -193,7 +203,7 @@ public function getPersistentSocket()
193
203
/**
194
204
* Set the connect timeout
195
205
*
196
- * @param Integer number of milliseconds before connect will timeout
206
+ * @param int number of milliseconds before connect will timeout
197
207
*/
198
208
public function setConnectTimeout ($ timeoutMs )
199
209
{
@@ -203,7 +213,7 @@ public function setConnectTimeout($timeoutMs)
203
213
/**
204
214
* Get the connect timeout
205
215
*
206
- * @return Integer number of milliseconds before connect will timeout
216
+ * @return int number of milliseconds before connect will timeout
207
217
*/
208
218
public function getConnectTimeout ()
209
219
{
@@ -213,7 +223,7 @@ public function getConnectTimeout()
213
223
/**
214
224
* Set the read/write timeout
215
225
*
216
- * @param Integer number of milliseconds before read or write call will timeout
226
+ * @param int number of milliseconds before read or write call will timeout
217
227
*/
218
228
public function setReadWriteTimeout ($ timeoutMs )
219
229
{
@@ -224,7 +234,7 @@ public function setReadWriteTimeout($timeoutMs)
224
234
/**
225
235
* Get the read timeout
226
236
*
227
- * @return Integer number of milliseconds before read will timeout
237
+ * @return int number of milliseconds before read will timeout
228
238
*/
229
239
public function getReadWriteTimeout ()
230
240
{
@@ -234,8 +244,8 @@ public function getReadWriteTimeout()
234
244
/**
235
245
* Helper to avoid duplicating milliseconds to secs/usecs in a few places
236
246
*
237
- * @param Integer millisecond timeout
238
- * @return Boolean
247
+ * @param int millisecond timeout
248
+ * @return bool
239
249
*/
240
250
private function set_ms_timeout ($ timeoutMs ) {
241
251
if (!$ this ->_sock ) {
@@ -270,9 +280,10 @@ private function connect()
270
280
/**
271
281
* Build a FastCGI packet
272
282
*
273
- * @param Integer $type Type of the packet
274
- * @param String $content Content of the packet
275
- * @param Integer $requestId RequestId
283
+ * @param int $type Type of the packet
284
+ * @param string $content Content of the packet
285
+ * @param int $requestId RequestId
286
+ * @return string
276
287
*/
277
288
private function buildPacket ($ type , $ content , $ requestId = 1 )
278
289
{
@@ -291,9 +302,9 @@ private function buildPacket($type, $content, $requestId = 1)
291
302
/**
292
303
* Build an FastCGI Name value pair
293
304
*
294
- * @param String $name Name
295
- * @param String $value Value
296
- * @return String FastCGI Name value pair
305
+ * @param string $name Name
306
+ * @param string $value Value
307
+ * @return string FastCGI Name value pair
297
308
*/
298
309
private function buildNvpair ($ name , $ value )
299
310
{
@@ -320,7 +331,7 @@ private function buildNvpair($name, $value)
320
331
/**
321
332
* Read a set of FastCGI Name value pairs
322
333
*
323
- * @param String $data Data containing the set of FastCGI NVPair
334
+ * @param string $data Data containing the set of FastCGI NVPair
324
335
* @return array of NVPair
325
336
*/
326
337
private function readNvpair ($ data , $ length = null )
@@ -359,7 +370,7 @@ private function readNvpair($data, $length = null)
359
370
/**
360
371
* Decode a FastCGI Packet
361
372
*
362
- * @param String $data String containing all the packet
373
+ * @param string $data string containing all the packet
363
374
* @return array
364
375
*/
365
376
private function decodePacketHeader ($ data )
@@ -401,10 +412,11 @@ private function readPacket()
401
412
}
402
413
403
414
/**
404
- * Get Informations on the FastCGI application
415
+ * Get Information on the FastCGI application
405
416
*
406
417
* @param array $requestedInfo information to retrieve
407
418
* @return array
419
+ * @throws \Exception
408
420
*/
409
421
public function getValues (array $ requestedInfo )
410
422
{
@@ -428,8 +440,11 @@ public function getValues(array $requestedInfo)
428
440
* Execute a request to the FastCGI application
429
441
*
430
442
* @param array $params Array of parameters
431
- * @param String $stdin Content
432
- * @return String
443
+ * @param string $stdin Content
444
+ * @return string
445
+ * @throws ForbiddenException
446
+ * @throws TimedOutException
447
+ * @throws \Exception
433
448
*/
434
449
public function request (array $ params , $ stdin )
435
450
{
@@ -439,18 +454,20 @@ public function request(array $params, $stdin)
439
454
440
455
/**
441
456
* Execute a request to the FastCGI application asyncronously
442
- *
457
+ *
443
458
* This sends request to application and returns the assigned ID for that request.
444
459
*
445
460
* You should keep this id for later use with wait_for_response(). Ids are chosen randomly
446
- * rather than seqentially to guard against false-positives when using persistent sockets.
447
- * In that case it is possible that a delayed response to a request made by a previous script
448
- * invocation comes back on this socket and is mistaken for response to request made with same ID
449
- * during this request.
461
+ * rather than sequentially to guard against false-positives when using persistent sockets.
462
+ * In that case it is possible that a delayed response to a request made by a previous script
463
+ * invocation comes back on this socket and is mistaken for response to request made with same
464
+ * ID during this request.
450
465
*
451
466
* @param array $params Array of parameters
452
- * @param String $stdin Content
453
- * @return Integer
467
+ * @param string $stdin Content
468
+ * @return int
469
+ * @throws TimedOutException
470
+ * @throws \Exception
454
471
*/
455
472
public function async_request (array $ params , $ stdin )
456
473
{
@@ -508,14 +525,17 @@ public function async_request(array $params, $stdin)
508
525
}
509
526
510
527
/**
511
- * Blocking call that waits for response to specific request
512
- *
513
- * @param Integer $requestId
514
- * @param Integer $timeoutMs [optional] the number of milliseconds to wait. Defaults to the ReadWriteTimeout value set.
515
- * @return string response body
528
+ * Blocking call that waits for response data of the specific request
529
+ *
530
+ * @param int $requestId
531
+ * @param int $timeoutMs [optional] the number of milliseconds to wait. Defaults to the ReadWriteTimeout value set.
532
+ * @return array response data
533
+ * @throws ForbiddenException
534
+ * @throws TimedOutException
535
+ * @throws \Exception
516
536
*/
517
- public function wait_for_response ($ requestId , $ timeoutMs = 0 ) {
518
-
537
+ public function wait_for_response_data ($ requestId , $ timeoutMs = 0 )
538
+ {
519
539
if (!isset ($ this ->_requests [$ requestId ])) {
520
540
throw new \Exception ('Invalid request id given ' );
521
541
}
@@ -524,7 +544,7 @@ public function wait_for_response($requestId, $timeoutMs = 0) {
524
544
if ($ this ->_requests [$ requestId ]['state ' ] == self ::REQ_STATE_OK
525
545
|| $ this ->_requests [$ requestId ]['state ' ] == self ::REQ_STATE_ERR
526
546
) {
527
- return $ this ->_requests [$ requestId ][ ' response ' ] ;
547
+ return $ this ->_requests [$ requestId ];
528
548
}
529
549
530
550
if ($ timeoutMs > 0 ) {
@@ -591,7 +611,22 @@ public function wait_for_response($requestId, $timeoutMs = 0) {
591
611
throw new \Exception ('Role value not known [UNKNOWN_ROLE] ' );
592
612
break ;
593
613
case self ::REQUEST_COMPLETE :
594
- return $ this ->_requests [$ requestId ][ ' response ' ] ;
614
+ return $ this ->_requests [$ requestId ];
595
615
}
596
616
}
617
+
618
+ /**
619
+ * Blocking call that waits for response to specific request
620
+ *
621
+ * @param int $requestId
622
+ * @param int $timeoutMs [optional] the number of milliseconds to wait. Defaults to the ReadWriteTimeout value set.
623
+ * @return string The response content.
624
+ * @throws ForbiddenException
625
+ * @throws TimedOutException
626
+ * @throws \Exception
627
+ */
628
+ public function wait_for_response ($ requestId , $ timeoutMs = 0 )
629
+ {
630
+ return $ this ->wait_for_response_data ($ requestId , $ timeoutMs )['response ' ];
631
+ }
597
632
}
0 commit comments