@@ -31,7 +31,7 @@ class ForbiddenException extends \Exception {}
3131 * Handles communication with a FastCGI application
3232 *
3333 * @author Pierrick Charron <pierrick@adoy.net>
34- * @version 1.0.0
34+ * @version 1.0
3535 */
3636class Client
3737{
@@ -74,25 +74,25 @@ class Client
7474
7575 /**
7676 * Socket
77- * @var Resource
77+ * @var resource
7878 */
7979 private $ _sock = null ;
8080
8181 /**
8282 * Host
83- * @var String
83+ * @var string
8484 */
8585 private $ _host = null ;
8686
8787 /**
8888 * Port
89- * @var Integer
89+ * @var int
9090 */
9191 private $ _port = null ;
9292
9393 /**
9494 * Keep Alive
95- * @var Boolean
95+ * @var bool
9696 */
9797 private $ _keepAlive = false ;
9898
@@ -112,43 +112,53 @@ class Client
112112
113113 /**
114114 * Use persistent sockets to connect to backend
115- * @var Boolean
115+ * @var bool
116116 */
117117 private $ _persistentSocket = false ;
118118
119119 /**
120120 * Connect timeout in milliseconds
121- * @var Integer
121+ * @var int
122122 */
123123 private $ _connectTimeout = 5000 ;
124124
125125 /**
126126 * Read/Write timeout in milliseconds
127- * @var Integer
127+ * @var int
128128 */
129129 private $ _readWriteTimeout = 5000 ;
130130
131131 /**
132132 * Constructor
133133 *
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
136136 */
137137 public function __construct ($ host , $ port )
138138 {
139139 $ this ->_host = $ host ;
140140 $ this ->_port = $ port ;
141141 }
142142
143+ /**
144+ * Get host.
145+ *
146+ * @return string
147+ */
148+ public function getHost ()
149+ {
150+ return $ this ->_host ;
151+ }
152+ 143153 /**
144154 * Define whether or not the FastCGI application should keep the connection
145155 * alive at the end of a request
146156 *
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
148158 */
149159 public function setKeepAlive ($ b )
150160 {
151- $ this ->_keepAlive = (boolean )$ b ;
161+ $ this ->_keepAlive = (bool )$ b ;
152162 if (!$ this ->_keepAlive && $ this ->_sock ) {
153163 fclose ($ this ->_sock );
154164 }
@@ -157,7 +167,7 @@ public function setKeepAlive($b)
157167 /**
158168 * Get the keep alive status
159169 *
160- * @return Boolean true if the connection should stay alive, false otherwise
170+ * @return bool true if the connection should stay alive, false otherwise
161171 */
162172 public function getKeepAlive ()
163173 {
@@ -168,12 +178,12 @@ public function getKeepAlive()
168178 * Define whether or not PHP should attempt to re-use sockets opened by previous
169179 * request for efficiency
170180 *
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
172182 */
173183 public function setPersistentSocket ($ b )
174184 {
175185 $ was_persistent = ($ this ->_sock && $ this ->_persistentSocket );
176- $ this ->_persistentSocket = (boolean )$ b ;
186+ $ this ->_persistentSocket = (bool )$ b ;
177187 if (!$ this ->_persistentSocket && $ was_persistent ) {
178188 fclose ($ this ->_sock );
179189 }
@@ -182,7 +192,7 @@ public function setPersistentSocket($b)
182192 /**
183193 * Get the pesistent socket status
184194 *
185- * @return Boolean true if the socket should be persistent, false otherwise
195+ * @return bool true if the socket should be persistent, false otherwise
186196 */
187197 public function getPersistentSocket ()
188198 {
@@ -193,7 +203,7 @@ public function getPersistentSocket()
193203 /**
194204 * Set the connect timeout
195205 *
196- * @param Integer number of milliseconds before connect will timeout
206+ * @param int number of milliseconds before connect will timeout
197207 */
198208 public function setConnectTimeout ($ timeoutMs )
199209 {
@@ -203,7 +213,7 @@ public function setConnectTimeout($timeoutMs)
203213 /**
204214 * Get the connect timeout
205215 *
206- * @return Integer number of milliseconds before connect will timeout
216+ * @return int number of milliseconds before connect will timeout
207217 */
208218 public function getConnectTimeout ()
209219 {
@@ -213,7 +223,7 @@ public function getConnectTimeout()
213223 /**
214224 * Set the read/write timeout
215225 *
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
217227 */
218228 public function setReadWriteTimeout ($ timeoutMs )
219229 {
@@ -224,7 +234,7 @@ public function setReadWriteTimeout($timeoutMs)
224234 /**
225235 * Get the read timeout
226236 *
227- * @return Integer number of milliseconds before read will timeout
237+ * @return int number of milliseconds before read will timeout
228238 */
229239 public function getReadWriteTimeout ()
230240 {
@@ -234,8 +244,8 @@ public function getReadWriteTimeout()
234244 /**
235245 * Helper to avoid duplicating milliseconds to secs/usecs in a few places
236246 *
237- * @param Integer millisecond timeout
238- * @return Boolean
247+ * @param int millisecond timeout
248+ * @return bool
239249 */
240250 private function set_ms_timeout ($ timeoutMs ) {
241251 if (!$ this ->_sock ) {
@@ -270,9 +280,10 @@ private function connect()
270280 /**
271281 * Build a FastCGI packet
272282 *
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
276287 */
277288 private function buildPacket ($ type , $ content , $ requestId = 1 )
278289 {
@@ -291,9 +302,9 @@ private function buildPacket($type, $content, $requestId = 1)
291302 /**
292303 * Build an FastCGI Name value pair
293304 *
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
297308 */
298309 private function buildNvpair ($ name , $ value )
299310 {
@@ -320,7 +331,7 @@ private function buildNvpair($name, $value)
320331 /**
321332 * Read a set of FastCGI Name value pairs
322333 *
323- * @param String $data Data containing the set of FastCGI NVPair
334+ * @param string $data Data containing the set of FastCGI NVPair
324335 * @return array of NVPair
325336 */
326337 private function readNvpair ($ data , $ length = null )
@@ -359,7 +370,7 @@ private function readNvpair($data, $length = null)
359370 /**
360371 * Decode a FastCGI Packet
361372 *
362- * @param String $data String containing all the packet
373+ * @param string $data string containing all the packet
363374 * @return array
364375 */
365376 private function decodePacketHeader ($ data )
@@ -401,10 +412,11 @@ private function readPacket()
401412 }
402413
403414 /**
404- * Get Informations on the FastCGI application
415+ * Get Information on the FastCGI application
405416 *
406417 * @param array $requestedInfo information to retrieve
407418 * @return array
419+ * @throws \Exception
408420 */
409421 public function getValues (array $ requestedInfo )
410422 {
@@ -428,8 +440,11 @@ public function getValues(array $requestedInfo)
428440 * Execute a request to the FastCGI application
429441 *
430442 * @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
433448 */
434449 public function request (array $ params , $ stdin )
435450 {
@@ -439,18 +454,20 @@ public function request(array $params, $stdin)
439454
440455 /**
441456 * Execute a request to the FastCGI application asyncronously
442- *
457+ *
443458 * This sends request to application and returns the assigned ID for that request.
444459 *
445460 * 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.
450465 *
451466 * @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
454471 */
455472 public function async_request (array $ params , $ stdin )
456473 {
@@ -508,14 +525,17 @@ public function async_request(array $params, $stdin)
508525 }
509526
510527 /**
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
516536 */
517- public function wait_for_response ($ requestId , $ timeoutMs = 0 ) {
518- 537+ public function wait_for_response_data ($ requestId , $ timeoutMs = 0 )
538+ {
519539 if (!isset ($ this ->_requests [$ requestId ])) {
520540 throw new \Exception ('Invalid request id given ' );
521541 }
@@ -524,7 +544,7 @@ public function wait_for_response($requestId, $timeoutMs = 0) {
524544 if ($ this ->_requests [$ requestId ]['state ' ] == self ::REQ_STATE_OK
525545 || $ this ->_requests [$ requestId ]['state ' ] == self ::REQ_STATE_ERR
526546 ) {
527- return $ this ->_requests [$ requestId ][ ' response ' ] ;
547+ return $ this ->_requests [$ requestId ];
528548 }
529549
530550 if ($ timeoutMs > 0 ) {
@@ -591,7 +611,22 @@ public function wait_for_response($requestId, $timeoutMs = 0) {
591611 throw new \Exception ('Role value not known [UNKNOWN_ROLE] ' );
592612 break ;
593613 case self ::REQUEST_COMPLETE :
594- return $ this ->_requests [$ requestId ][ ' response ' ] ;
614+ return $ this ->_requests [$ requestId ];
595615 }
596616 }
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+ }
597632}
0 commit comments