diff --git a/README.md b/README.md
index 4974f30..eabf608 100644
--- a/README.md
+++ b/README.md
@@ -2,3 +2,5 @@ ugly-queue
==========
A simple file-based queue system for PHP 5.3.3+
+
+Documentation and Test suites forthcoming.
\ No newline at end of file
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..45e26b7
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,36 @@
+{
+ "name" : "dcarbone/ugly-queue",
+ "type" : "library",
+ "description" : "A simple file-based queue system for PHP 5.3.3+",
+
+ "keywords": [
+ "php",
+ "queue",
+ "file queue",
+ "ugly queue"
+ ],
+ "homepage": "https://github.com/dcarbone/ugly-queue",
+ "license": "GPLv3",
+
+ "authors" : [
+ {
+ "name" : "Daniel Carbone",
+ "email" : "daniel.p.carbone@gmail.com"
+ }
+ ],
+
+ "require" : {
+ "php" : ">=5.3.3",
+ "dcarbone/helpers" : "6.1.*"
+ },
+
+ "require-dev" : {
+ "phpunit/phpunit": "4.1.*"
+ },
+
+ "autoload" : {
+ "psr-4" : {
+ "DCarbone\\" : "src/"
+ }
+ }
+}
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
new file mode 100644
index 0000000..d314116
--- /dev/null
+++ b/phpunit.xml.dist
@@ -0,0 +1,25 @@
+
+
Directory access is forbidden.
+ + +HTML; + file_put_contents($this->queueGroupDirPath.'index.html', $html); + } + + if (!file_exists($this->queueGroupDirPath.'queue.txt')) + file_put_contents($this->queueGroupDirPath.'queue.txt', ''); + + $this->init = true; + } + + /** + * @param int $count + * @throws \RuntimeException + * @return bool|array + */ + public function processQueue($count = 1) + { + if ($this->init === false) + throw new \RuntimeException('file_queue::load_queue_data - Must first initialize queue!'); + + // If we don't have a lock, assume issue and move on. + if ($this->haveLock === false || !file_exists($this->queueGroupDirPath.'queue.txt')) + return false; + + // Find number of lines in the queue file + $line_count = FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt'); + + // If queue line count is 0, assume empty + if ($line_count === 0) + return false; + + // Try to open the file for reading / writing. + $queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r+'); + if ($queue_file_handle === false) + $this->unlock(); + + // Get an array of the oldest $count data in the queue + $data = array(); + $start_line = $line_count - $count; + $i = 0; + while (($line = fscanf($queue_file_handle, "%s\t%s\n")) !== false && $i < $line_count) + { + if ($i++ >= $start_line) + { + list ($key, $value) = $line; + $data[$key] = $value; + } + } + + // If we have consumed the rest of the file + if ($count >= $line_count) + { + rewind($queue_file_handle); + ftruncate($queue_file_handle, 0); + fclose($queue_file_handle); + $this->unlock(); + } + // Otherwise, create new queue file minus the processed lines. + else + { + $tmp = fopen($this->queueGroupDirPath.'queue.tmp', 'w+'); + rewind($queue_file_handle); + $i = 0; + while (($line = fgets($queue_file_handle)) !== false && $i < $start_line) + { + if ($line !== "\n" || $line !== "") + fwrite($tmp, $line); + + $i++; + } + + fclose($queue_file_handle); + fclose($tmp); + FileHelper::superUnlink($this->queueGroupDirPath.'queue.txt'); + rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt'); + } + + return $data; + } + + /** + * @param string $key + * @param string|array $value + * @return bool + * @throws \RuntimeException + */ + public function addToQueue($key, $value) + { + if ($this->init === false) + throw new \RuntimeException('file_queue::add_to_queue - Must first initialize queue!'); + + // If we don't have a lock, assume issue and move on. + if ($this->haveLock === false) + return false; + + if (!is_resource($this->_tmpHandle)) + { + $this->_tmpHandle = fopen($this->queueGroupDirPath.'queue.tmp', 'w+'); + if ($this->_tmpHandle === false) + return false; + } + + if (is_array($value) || $value instanceof \stdClass) + $value = json_encode($value); + + return (bool)fwrite( + $this->_tmpHandle, + $key."\t".str_replace(array("\r\n", "\n"), ' ', $value) + ."\n"); + } + + /** + * If there is a tmp queue file, add it's contents to the beginning of a new queue file + * + * @return void + */ + protected function _populateQueue() + { + if (is_resource($this->_tmpHandle)) + { + if (file_exists($this->queueGroupDirPath.'queue.txt')) + { + $queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r+'); + while (($line = fgets($queue_file_handle)) !== false) + { + if ($line !== "\n" && $line !== "") + fwrite($this->_tmpHandle, $line); + } + + fclose($queue_file_handle); + FileHelper::superUnlink($this->queueGroupDirPath.'queue.txt'); + } + + fclose($this->_tmpHandle); + rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt'); + } + } + + /** + * @return string + */ + public function getQueueGroup() + { + return $this->queueGroup; + } + + /** + * @return string + */ + public function getQueueBaseDir() + { + return $this->queueBaseDir; + } +} \ No newline at end of file diff --git a/tests/UglyQueue/UglyQueueTest.php b/tests/UglyQueue/UglyQueueTest.php new file mode 100644 index 0000000..7fd3725 --- /dev/null +++ b/tests/UglyQueue/UglyQueueTest.php @@ -0,0 +1,48 @@ + dirname(__DIR__).'/misc/', + ); + + $uglyQueue = new \DCarbone\UglyQueue($conf); + + return $uglyQueue; + } + + /** + * @covers \DCarbone\UglyQueue::__construct + * @uses \DCarbone\UglyQueue + * @expectedException \InvalidArgumentException + */ + public function testExceptionThrownWhenConstructingUglyQueueWithEmptyOrInvalidConf() + { + $conf = array(); + $uglyQueue = new \DCarbone\UglyQueue($conf); + } + + /** + * @covers \DCarbone\UglyQueue::__construct + * @uses \DCarbone\UglyQueue + * @expectedException \RuntimeException + */ + public function testExceptionThrownWhenConstructingUglyQueueWithInvalidQueueBaseDirPath() + { + $conf = array( + 'queue-base-dir' => 'sandwiches', + ); + + $uglyQueue = new \DCarbone\UglyQueue($conf); + } +} diff --git a/tests/misc/index.html b/tests/misc/index.html new file mode 100644 index 0000000..e515a86 --- /dev/null +++ b/tests/misc/index.html @@ -0,0 +1 @@ +