From 95c15758b50144105064d2613d1e9a9da23d4e7c Mon Sep 17 00:00:00 2001 From: horus_arch Date: Mon, 20 Feb 2017 12:42:43 +0100 Subject: Committed vendor/ for lazyness. --- .../pda/pheanstalk/src/Socket/NativeSocket.php | 130 +++++++++++++++++++++ .../pda/pheanstalk/src/Socket/StreamFunctions.php | 99 ++++++++++++++++ .../pda/pheanstalk/src/Socket/WriteHistory.php | 64 ++++++++++ 3 files changed, 293 insertions(+) create mode 100644 intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/NativeSocket.php create mode 100644 intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/StreamFunctions.php create mode 100644 intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/WriteHistory.php (limited to 'intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket') diff --git a/intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/NativeSocket.php b/intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/NativeSocket.php new file mode 100644 index 0000000..192dd73 --- /dev/null +++ b/intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/NativeSocket.php @@ -0,0 +1,130 @@ +_socket = $this->_wrapper() + ->pfsockopen($host, $port, $errno, $errstr, $connectTimeout, $connectPersistent); + } else { + $this->_socket = $this->_wrapper() + ->fsockopen($host, $port, $errno, $errstr, $connectTimeout, $connectPersistent); + } + + if (!$this->_socket) { + throw new Exception\ConnectionException($errno, $errstr . " (connecting to $host:$port)"); + } + + $this->_wrapper() + ->stream_set_timeout($this->_socket, self::SOCKET_TIMEOUT); + } + + /* (non-phpdoc) + * @see Socket::write() + */ + public function write($data) + { + $history = new WriteHistory(self::WRITE_RETRIES); + + for ($written = 0, $fwrite = 0; $written < strlen($data); $written += $fwrite) { + $fwrite = $this->_wrapper() + ->fwrite($this->_socket, substr($data, $written)); + + $history->log($fwrite); + + if ($history->isFullWithNoWrites()) { + throw new Exception\SocketException(sprintf( + 'fwrite() failed to write data after %u tries', + self::WRITE_RETRIES + )); + } + } + } + + /* (non-phpdoc) + * @see Socket::write() + */ + public function read($length) + { + $read = 0; + $parts = ''; + + while ($read < $length && !$this->_wrapper()->feof($this->_socket)) { + $data = $this->_wrapper() + ->fread($this->_socket, $length - $read); + + if ($data === false) { + throw new Exception\SocketException('fread() returned false'); + } + + $read += strlen($data); + $parts .= $data; + } + + return $parts; + } + + /* (non-phpdoc) + * @see Socket::write() + */ + public function getLine($length = null) + { + do { + $data = isset($length) ? + $this->_wrapper()->fgets($this->_socket, $length) : + $this->_wrapper()->fgets($this->_socket); + + if ($this->_wrapper()->feof($this->_socket)) { + throw new Exception\SocketException("Socket closed by server!"); + } + } while ($data === false); + + return rtrim($data); + } + + public function disconnect() + { + $this->_wrapper()->fclose($this->_socket); + } + + // ---------------------------------------- + + /** + * Wrapper class for all stream functions. + * Facilitates mocking/stubbing stream operations in unit tests. + */ + private function _wrapper() + { + return StreamFunctions::instance(); + } +} diff --git a/intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/StreamFunctions.php b/intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/StreamFunctions.php new file mode 100644 index 0000000..6f793f8 --- /dev/null +++ b/intern.gospeladlershof.de/vendor/pda/pheanstalk/src/Socket/StreamFunctions.php @@ -0,0 +1,99 @@ +_limit = $limit; + } + + /** + * Whether the history has reached its limit of entries. + */ + public function isFull() + { + return count($this->_data) >= $this->_limit; + } + + public function hasWrites() + { + return (bool) array_sum($this->_data); + } + + public function isFullWithNoWrites() + { + return $this->isFull() && !$this->hasWrites(); + } + + /** + * Logs the return value from a write call. + * Returns the input value. + */ + public function log($write) + { + if ($this->isFull()) { + array_shift($this->_data); + } + + $this->_data[] = (int) $write; + + return $write; + } +} -- cgit v1.2.3