diff --git a/queue.php b/queue.php new file mode 100644 index 0000000..b140267 --- /dev/null +++ b/queue.php @@ -0,0 +1,79 @@ +queue = substr ($dsn, 7); + $file = new \file (); + if (! $file->file_exists (dirname ($this->queue))) + $file->mkdir (dirname ($this->queue), 0777, true); + if (! $file->file_exists ($this->queue)) + $file->touch ($this->queue); + return $this; + } + // }}} + + /** Add a new entry to the end of the queue + * @param mixed $entry The entry to add + */ + public function add ($entry) + // {{{ + { + $file = new \file (); + $file->lockEX ($this->queue); + $file->file_put_contents ($this->queue, + json_encode ($entry)."\n", FILE_APPEND); + $file->lockUN ($this->queue); + return $this; + } + // }}} + + /** Get all the entries of the queue + * @param boolean|null $delete If true, delete the read entry + * @return array + */ + public function getAll ($delete =false) + // {{{ + { + $file = new \file (); + if ($delete) + $file->lockEX ($this->queue); + else + $file->lockSH ($this->queue); + $content = $file->file_get_contents ($this->queue); + $entries = array (); + foreach (explode ("\n", $content) as $json) + { + $entries[] = json_decode ($json); + } + if ($delete) + $file->file_put_contents ($this->queue, ""); + $file->lockUN ($this->queue); + return $entries; + } + // }}} + + /** Get the number of entries in the queue + */ + public function count () + // {{{ + { + $file = new \file (); + $file->lockSH ($this->queue); + $content = $file->file_get_contents ($this->queue); + $count = substr_count ($content, "\n"); + $file->lockUN ($this->queue); + return $count; + } + // }}} + + /** Clear all the entries of the queue + */ + public function clear () + // {{{ + { + $file = new \file (); + $file->lockEX ($this->queue); + $file->file_put_contents ($this->queue, ""); + $file->lockUN ($this->queue); + return $this; + } + // }}} + + /** Get the first entry in the queue with optional removing of the entry + * @param boolean|null $delete If true, delete the read entry + * @return mixed Return null if there is no entry, or the first entry in the + * queue (FIFO) + */ + public function getFirst ($delete = false) + // {{{ + { + $file = new \file (); + if ($delete) + $file->lockEX ($this->queue); + else + $file->lockSH ($this->queue); + $content = $file->file_get_contents ($this->queue); + $entries = explode ("\n", $content); + if (count ($entries) === 0) + $res = null; + else + { + $json = array_shift ($entries); + $res = json_decode ($json); + if ($delete) + $file->file_put_contents ($this->queue, + implode ("\n", $entries)); + } + $file->lockUN ($this->queue); + return $res; + } + // }}} + + /** Get the last entry in the queue with optional removing of the entry + * @param boolean|null $delete If true, delete the read entry + * @return mixed Return null if there is no entry, or the last entry in the + * queue (LIFO) + */ + public function getLast ($delete = false) + // {{{ + { + $file = new \file (); + if ($delete) + $file->lockEX ($this->queue); + else + $file->lockSH ($this->queue); + $content = $file->file_get_contents ($this->queue); + $entries = explode ("\n", $content); + if (count ($entries) === 0) + $res = null; + else + { + $json = array_pop ($entries); + $res = json_decode ($json); + if ($delete) + $file->file_put_contents ($this->queue, + implode ("\n", $entries)); + } + $file->lockUN ($this->queue); + return $res; + } + // }}} + + /** Get X entries starting at position Y + * @param integer $start the starting position + * @param integer $number The number of entries to get + * @param boolean|null $delete If true, delete the read entries + * @return array An array of mixed entries + */ + public function getRange ($start, $number, $delete = false) + // {{{ + { + $file = new \file (); + if ($delete) + $file->lockEX ($this->queue); + else + $file->lockSH ($this->queue); + $content = $file->file_get_contents ($this->queue); + $entries = explode ("\n", $content); + $res = array (); + if (count ($entries) > 0) + { + for ($i = $start ; $i < $start + $number ; $i++) + { + if (! key_exists ($i, $entries)) + throw new \Exception ("Invalid entry requested", 406); + $json = $entries[$i]; + $content = json_decode ($json, true); + if (json_last_error() === JSON_ERROR_NONE) + $res[] = $content; + else + $res[] = $json; + } + if ($delete) + $file->file_put_contents ($this->queue, + implode ("\n", $entries)); + } + $file->lockUN ($this->queue); + return $res; + } + // }}} +}