<commit_before><?php
/**
 * beanstalk: A minimalistic PHP beanstalk client.
 *
 * Copyright (c) 2009-2013 <NAME>
 *
 * Distributed under the terms of the MIT License.
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright  2009-2013 <NAME> <<EMAIL>>
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link       http://github.com/davidpersson/beanstalk
 */

/**
 * An interface to the beanstalk queue service. Implements the beanstalk
 * protocol spec 1.2. Where appropriate the documentation from the protcol has
 * been added to the docblocks in this class.
 *
 * @link https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
 */
class Socket_Beanstalk {

	/**
	 * Holds a boolean indicating whether a connection to the server is
	 * currently established or not.
	 *
	 * @var boolean
	 */
	public $connected = false;

	/**
	 * Holds configuration values.
	 *
	 * @var array
	 */
	protected $_config = array();

	/**
	 * The current connection resource handle (if any).
	 *
	 * @var resource
	 */
	protected $_connection;

	/**
	 * Generated errors. Will hold a maximum of 200 error messages at any time
	 * to prevent pilling up messages and using more and more memory. This is
	 * especially important if this class is used in long-running workers.
	 *
	 * @see Socket_Beanstalk::errors()
	 * @see Socket_Beanstalk::_error()
	 * @var array
	 */
	protected $_errors = array();

	/**
	 * Constructor.
	 *
	 * @param array $config An array of configuration values:
	 *        - `'persistent'`  Whether to make the connection persistent or
	 *                          not, defaults to `true` as the FAQ recommends
	 *                          persistent connections.
	 *        - `'host'`        The beanstalk server hostname or IP address to
	 *                          connect to, defaults to `127.0.0.1`.
	 *        - `'port'`        The port of the server to connect to, defaults
	 *                          to `11300`.
	 *        - `'timeout'`     Timeout in seconds when establishing the
	 *                          connection, defaults to `1`.
	 * @return void
	 */
	public function __construct(array $config = array()) {
		$defaults = array(
			'persistent' => true,
			'host' => '127.0.0.1',
			'port' => 11300,
			'timeout' => 1
		);
		$this->_config = $config + $defaults;
	}

	/**
	 * Destructor, disconnects from the server.
	 *
	 * @return void
	 */
	public function __destruct() {
		$this->disconnect();
	}

	/**
	 * Initiates a socket connection to the beanstalk server. The resulting
	 * stream will not have any timeout set on it. Which means it can wait an
	 * unlimited amount of time until a packet becomes available. This is
	 * required for doing blocking reads.
	 *
	 * @see Socket_Beanstalk::$_connection
	 * @see Socket_Beanstalk::reserve()
	 * @return boolean `true` if the connection was established, `false` otherwise.
	 */
	public function connect() {
		if (isset($this->_connection)) {
			$this->disconnect();
		}

		$function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
		$params = array($this->_config['host'], $this->_config['port'], &$errNum, &$errStr);

		if ($this->_config['timeout']) {
			$params[] = $this->_config['timeout'];
		}
		$this->_connection = @call_user_func_array($function, $params);

		if (!empty($errNum) || !empty($errStr)) {
			$this->_error("{$errNum}: {$errStr}");
		}

		$this->connected = is_resource($this->_connection);

		if ($this->connected) {
			stream_set_timeout($this->_connection, -1);
		}
		return $this->connected;
	}

	/**
	 * Closes the connection to the beanstalk server.
	 *
	 * @return boolean `true` if diconnecting was successful.
	 */
	public function disconnect() {
		if (!is_resource($this->_connection)) {
			$this->connected = false;
		} else {
			$this->connected = !fclose($this->_connection);

			if (!$this->connected) {
				$this->_connection = null;
			}
		}
		return !$this->connected;
	}

	/**
	 * Returns collected error messages.
	 *
	 * @return array An array of error messages.
	 */
	public function errors() {
		return $this->_errors;
	}

	/**
	 * Pushes an error message to `Beanstalk::$_errors`. Ensures
	 * that at any point there are not more than 200 messages.
	 *
	 * @param string $message The error message.
	 * @return void
	 */
	protected function _error($message) {
		if (count($this->_errors) >= 200) {
			array_shift($this->_errors);
		}
		array_push($this->_errors, $message);
	}

	/**
	 * Writes a packet to the socket. Prior to writing to the socket will check
	 * for availability of the connection.
	 *
	 * @param string $data
	 * @return integer|boolean number of written bytes or `false` on error.
	 */
	protected function _write($data) {
		if (!$this->connected && !$this->connect()) {
			return false;
		}

		$data .= "\r\n";
		return fwrite($this->_connection, $data, strlen($data));
	}

	/**
	 * Reads a packet from the socket. Prior to reading from the socket will
	 * check for availability of the connection.
	 *
	 * @param int $length Number of bytes to read.
	 * @return string|boolean Data or `false` on error.
	 */
	protected function _read($length = null) {
		if (!$this->connected && !$this->connect()) {
			return false;
		}
		if ($length) {
			if (feof($this->_connection)) {
				return false;
			}
			$data = fread($this->_connection, $length + 2);
			$meta = stream_get_meta_data($this->_connection);

			if ($meta['timed_out']) {
				$this->_error('Connection timed out.');
				return false;
			}
			$packet = rtrim($data, "\r\n");
		} else {
			$packet = stream_get_line($this->_connection, 16384, "\r\n");
		}
		return $packet;
	}<commit_msg>Exchanging fread() for stream_get_contents(); fixes issue #8.<commit_after><?php
/**
 * beanstalk: A minimalistic PHP beanstalk client.
 *
 * Copyright (c) 2009-2013 <NAME>
 *
 * Distributed under the terms of the MIT License.
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright  2009-2013 <NAME> <<EMAIL>>
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link       http://github.com/davidpersson/beanstalk
 */

/**
 * An interface to the beanstalk queue service. Implements the beanstalk
 * protocol spec 1.2. Where appropriate the documentation from the protcol has
 * been added to the docblocks in this class.
 *
 * @link https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
 */
class Socket_Beanstalk {

	/**
	 * Holds a boolean indicating whether a connection to the server is
	 * currently established or not.
	 *
	 * @var boolean
	 */
	public $connected = false;

	/**
	 * Holds configuration values.
	 *
	 * @var array
	 */
	protected $_config = array();

	/**
	 * The current connection resource handle (if any).
	 *
	 * @var resource
	 */
	protected $_connection;

	/**
	 * Generated errors. Will hold a maximum of 200 error messages at any time
	 * to prevent pilling up messages and using more and more memory. This is
	 * especially important if this class is used in long-running workers.
	 *
	 * @see Socket_Beanstalk::errors()
	 * @see Socket_Beanstalk::_error()
	 * @var array
	 */
	protected $_errors = array();

	/**
	 * Constructor.
	 *
	 * @param array $config An array of configuration values:
	 *        - `'persistent'`  Whether to make the connection persistent or
	 *                          not, defaults to `true` as the FAQ recommends
	 *                          persistent connections.
	 *        - `'host'`        The beanstalk server hostname or IP address to
	 *                          connect to, defaults to `127.0.0.1`.
	 *        - `'port'`        The port of the server to connect to, defaults
	 *                          to `11300`.
	 *        - `'timeout'`     Timeout in seconds when establishing the
	 *                          connection, defaults to `1`.
	 * @return void
	 */
	public function __construct(array $config = array()) {
		$defaults = array(
			'persistent' => true,
			'host' => '127.0.0.1',
			'port' => 11300,
			'timeout' => 1
		);
		$this->_config = $config + $defaults;
	}

	/**
	 * Destructor, disconnects from the server.
	 *
	 * @return void
	 */
	public function __destruct() {
		$this->disconnect();
	}

	/**
	 * Initiates a socket connection to the beanstalk server. The resulting
	 * stream will not have any timeout set on it. Which means it can wait an
	 * unlimited amount of time until a packet becomes available. This is
	 * required for doing blocking reads.
	 *
	 * @see Socket_Beanstalk::$_connection
	 * @see Socket_Beanstalk::reserve()
	 * @return boolean `true` if the connection was established, `false` otherwise.
	 */
	public function connect() {
		if (isset($this->_connection)) {
			$this->disconnect();
		}

		$function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
		$params = array($this->_config['host'], $this->_config['port'], &$errNum, &$errStr);

		if ($this->_config['timeout']) {
			$params[] = $this->_config['timeout'];
		}
		$this->_connection = @call_user_func_array($function, $params);

		if (!empty($errNum) || !empty($errStr)) {
			$this->_error("{$errNum}: {$errStr}");
		}

		$this->connected = is_resource($this->_connection);

		if ($this->connected) {
			stream_set_timeout($this->_connection, -1);
		}
		return $this->connected;
	}

	/**
	 * Closes the connection to the beanstalk server.
	 *
	 * @return boolean `true` if diconnecting was successful.
	 */
	public function disconnect() {
		if (!is_resource($this->_connection)) {
			$this->connected = false;
		} else {
			$this->connected = !fclose($this->_connection);

			if (!$this->connected) {
				$this->_connection = null;
			}
		}
		return !$this->connected;
	}

	/**
	 * Returns collected error messages.
	 *
	 * @return array An array of error messages.
	 */
	public function errors() {
		return $this->_errors;
	}

	/**
	 * Pushes an error message to `Beanstalk::$_errors`. Ensures
	 * that at any point there are not more than 200 messages.
	 *
	 * @param string $message The error message.
	 * @return void
	 */
	protected function _error($message) {
		if (count($this->_errors) >= 200) {
			array_shift($this->_errors);
		}
		array_push($this->_errors, $message);
	}

	/**
	 * Writes a packet to the socket. Prior to writing to the socket will check
	 * for availability of the connection.
	 *
	 * @param string $data
	 * @return integer|boolean number of written bytes or `false` on error.
	 */
	protected function _write($data) {
		if (!$this->connected && !$this->connect()) {
			return false;
		}

		$data .= "\r\n";
		return fwrite($this->_connection, $data, strlen($data));
	}

	/**
	 * Reads a packet from the socket. Prior to reading from the socket will
	 * check for availability of the connection.
	 *
	 * @param int $length Number of bytes to read.
	 * @return string|boolean Data or `false` on error.
	 */
	protected function _read($length = null) {
		if (!$this->connected && !$this->connect()) {
			return false;
		}
		if ($length) {
			if (feof($this->_connection)) {
				return false;
			}
			$data = stream_get_contents($this->_connection, $length + 2);
			$meta = stream_get_meta_data($this->_connection);

			if ($meta['timed_out']) {
				$this->_error('Connection timed out.');
				return false;
			}
			$packet = rtrim($data, "\r\n");
		} else {
			$packet = stream_get_line($this->_connection, 16384, "\r\n");
		}
		return $packet;
	}
{'fraction_non_alphanumeric': 0.13896194985095564, 'fraction_numerical': 0.010257759074171488, 'mean_word_length': 2.980111653872994, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 95, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3067401', 'n_tokens_mistral': 3826, 'n_tokens_neox': 3325, 'n_words': 1086}