cvs: pear /IO_Async Async.php

From: Date: Sun, 26 Aug 2001 16:41:29 +0000
Subject: cvs: pear /IO_Async Async.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-665@lists.php.net to get a copy of this message
ssb Sun Aug 26 12:41:29 2001 EDT Modified files: /pear/IO_Async Async.php Log: * implemented _urlEncodeArray * documented more methods * fixed max_buffer_size stuff in readEvent Index: pear/IO_Async/Async.php diff -u pear/IO_Async/Async.php:1.1 pear/IO_Async/Async.php:1.2 --- pear/IO_Async/Async.php:1.1 Sun Aug 26 12:13:08 2001 +++ pear/IO_Async/Async.php Sun Aug 26 12:41:29 2001 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: Async.php,v 1.1 2001/08/26 16:13:08 ssb Exp $ +// $Id: Async.php,v 1.2 2001/08/26 16:41:29 ssb Exp $ // TODO: // - it's not exactly IO until we can O, today we only I :-) @@ -275,10 +275,18 @@ return $this->registerSocket($socket); } + /** + * Read an event from the socket and process the result according to + * this object's `mode' option. + * + * @return void + * + * @access public + */ function readEvent() { if (!is_resource($this->_fp)) { - return $this->raiseError("IO_Async::readLine: no socket opened", IO_ASYNC_ERROR_NO_SOCKET); + return $this->raiseError("IO_Async::readEvent: no socket opened", IO_ASYNC_ERROR_NO_SOCKET); } switch ($this->_mode) { case IO_ASYNC_CALLBACK_LINE: { @@ -287,7 +295,6 @@ $line .= fgets($this->_fp, $this->_buffer_size); } while (substr($line, -1) != "\n"); $this->_runCallback(&$line); - break; } case IO_ASYNC_CALLBACK_BLOCK: { $data = fread($this->_fp, $this->_buffer_size); @@ -295,16 +302,26 @@ break; } case IO_ASYNC_BUFFER_ALL: { - $this->_buffer .= fread($this->_fp, $this->_buffer_size); + if ($this->_max_buffer_size != -1 && + strlen($this->_buffer) + $this->_buffer_size > $this->_max_buffer_size) { + $trunclen = $this->_max_buffer_size - strlen($this->_buffer); + $tmp = fread($this->_fp, $this->_buffer_size); + $this->_buffer .= substr($tmp, 0, $trunclen); + return false; // signal that we're done + } else { + $this->_buffer .= fread($this->_fp, $this->_buffer_size); + } break; } + } + return !feof($this->_fp); } /** * Set the socket read buffer size. You can only increase the buffer * size. * - * @param int $new_size the new buffer size + * @param int $new_size the new buffer size * * @return bool true if the buffer size was changed, false if you * specified a smaller buffer @@ -320,6 +337,15 @@ return false; } + /** + * Run a user-defined callback. + * + * @param string $data read socket data that is passed to the callback + * + * @return void + * + * @access private + */ function _runCallback(&$data) { if (function_exists($this->_callback)) { @@ -327,21 +353,53 @@ } } - function readEvent() + /** + * Url-encode an array of names and values "the PHP way". + * + * @param array $arr array of values to encode + * + * @param string $prefix name of an array we'll return + * indices for (default '') + * + * @param bool $toplevel whether this is the outer call (default true) + * + * @return string an URL-encoding string ready for using in an HTTP + * GET request or in a POST body + * + * @access private + */ + function _uriEncodeArray(&$arr, $prefix = '', $toplevel = true) { - switch ($this->_mode) { - case IO_ASYNC_BUFFER_ALL: { -. - break; + $ret = array(); + if (!is_array($arr)) { + return ''; + } + + foreach ($arr as $name => $value) { + if ($prefix) { + $name = "{$prefix}[{$name}]"; } - case IO_ASYNC_CALLBACK_BLOCK: { - break; + if (is_array($value)) { + $ret = array_merge($ret, IO_Async::_uriEncodeArray($value, $name, false)); + } else { + $ret[$name] = $value; } - case IO_ASYNC_CALLBACK_LINE: { - break; + } + + if ($toplevel) { + $str = ''; + $i = 0; + foreach ($ret as $name => $value) { + if ($i++ > 0) { + $str .= '&'; + } + $str .= urlencode($name) . '=' . urlencode($value); } + return $str; } + return $ret; } + }

« previous php.pear.cvs (#665) next »