[icinga-checkins] icinga.org: icinga-api/mhein/default: * implemented result objects for livestatus

git at icinga.org git at icinga.org
Tue Jan 12 14:21:31 CET 2010


Module: icinga-api
Branch: mhein/default
Commit: 7197366c64965fba29d487d839e71924fb96a215
URL:    https://git.icinga.org/?p=icinga-api.git;a=commit;h=7197366c64965fba29d487d839e71924fb96a215

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Wed Dec 23 12:23:56 2009 +0100

* implemented result objects for livestatus

---

 objects/result/IcingaApiResultLivestatus.php |  128 ++++++++++++++++----------
 1 files changed, 80 insertions(+), 48 deletions(-)

diff --git a/objects/result/IcingaApiResultLivestatus.php b/objects/result/IcingaApiResultLivestatus.php
index 36b77fe..60e30bc 100644
--- a/objects/result/IcingaApiResultLivestatus.php
+++ b/objects/result/IcingaApiResultLivestatus.php
@@ -14,6 +14,7 @@ class IcingaApiResultLivestatus
 
 	private $searchColumns = array();
 	private $lastRowCombined = false;
+	private $lastOffset = false;
 
 	/*
 	 * METHODS
@@ -50,6 +51,7 @@ class IcingaApiResultLivestatus
 	public function setSearchResult (array $data) {
 		$this->resultArray = $data;
 		$this->numResults = count($data);
+		$this->lastOffset = $this->numResults - 1;
 		return $this;
 	}
 
@@ -66,71 +68,101 @@ class IcingaApiResultLivestatus
 	}
 
 	/**
-	 * sets next result row
-	 *
+	 * sets next row of result array
 	 * @param	void
 	 * @return	void
 	 * @author	Christian Doebler <christian.doebler at netways.de>
 	 */
- 	public function next () {
- 		switch ($this->resultType) {
- 			case self::RESULT_OBJECT:
-				$this->resultRow = $this->searchObject->fetchObject();
-				if ($this->resultRow !== false) {
-					if ($this->offset === false) {
-						$this->offset = 0;
-					} else {
-						$this->offset++;
-					}
-				} else {
-					$this->offset = false;
-				}
-				break;
+	private function setNextResultArrayRow () {
+		if ($this->offset < $this->lastOffset || $this->offset === false) {
 
- 			case self::RESULT_ARRAY:
- 				if ($this->offset < $this->numResults) {
+			// set row offset
+			if ($this->offset === false) {
+				$this->offset = 0;
+			} else {
+				$this->offset++;
+			}
 
- 					// set row offset
-					if ($this->offset === false) {
-						$this->offset = 0;
-					} else {
-						$this->offset++;
-					}
+			if ($this->lastRowCombined !== false) {
 
-					if ($this->lastRowCombined !== false) {
+				if ($this->offset > $this->lastRowCombined) {
 
-						if ($this->offset > $this->lastRowCombined) {
+					// current row not converted to associative array -> convert
+					$this->resultArray[$this->offset] = array_combine(
+						$this->searchColumns,
+						$this->resultArray[$this->offset]
+					);
+					$this->lastRowCombined = $this->offset;
 
-							// current row not converted to associative array -> convert
-							$this->resultArray[$this->offset] = array_combine(
-								$this->searchColumns,
-								$this->resultArray[$this->offset]
-							);
-							$this->lastRowCombined = $this->offset;
+				}
 
-						}
+			} else {
 
-					} else {
+				// first row -> convert to associative array
+				$this->resultArray[$this->offset] = array_combine(
+					$this->searchColumns,
+					$this->resultArray[$this->offset]
+				);
+				$this->lastRowCombined = 0;
 
-						// first row -> convert to associative array
-						$this->resultArray[$this->offset] = array_combine(
-							$this->searchColumns,
-							$this->resultArray[$this->offset]
-						);
-						$this->lastRowCombined = 0;
+			}
 
-					}
+			// set row
+			$this->resultRow = $this->resultArray[$this->offset];
 
-					// set row
-					$this->resultRow = $this->resultArray[$this->offset];
+		} else {
 
- 				} else {
+			// no rows left -> reset
+			$this->offset = false;
+			$this->resultRow = false;
 
- 					// no rows left -> reset
-					$this->offset = false;
-					$this->resultRow = false;
+		}
+	}
 
-				}
+	/**
+	 * sets next row of result object
+	 * @param	void
+	 * @return	void
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	private function setNextResultObjectRow () {
+		$this->setNextResultArrayRow();
+
+		if ($this->resultRow !== false) {
+			if (is_array($this->resultRow)) {
+				// convert array into object
+				$rowStr = var_export($this->resultRow, true);
+				$rowArr = explode("\n", $rowStr);
+				array_pop($rowArr);
+				array_shift($rowArr);
+				$rowStr = implode("\n", $rowArr);
+
+				$rowStr = preg_replace('/^[^A-Z]+\'([A-Z_]+)\' => ([^\n]+)/m', 'public \$$1 = $2', $rowStr);
+				$rowStr = preg_replace('/,\n|,$/m', ';', $rowStr);
+				$rowStr = 'class ResultRow {' . $rowStr . '} $resultRow = new ResultRow();';
+				eval($rowStr);
+
+				$this->resultArray[$this->offset] = $resultRow;
+			}
+
+			$this->resultRow = $this->resultArray[$this->offset];
+		}
+	}
+
+	/**
+	 * sets next result row
+	 *
+	 * @param	void
+	 * @return	void
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+ 	public function next () {
+ 		switch ($this->resultType) {
+ 			case self::RESULT_OBJECT:
+				$this->setNextResultObjectRow();
+				break;
+ 			case self::RESULT_ARRAY:
+ 				$this->setNextResultArrayRow();
  				break;
  		}
  	}





More information about the icinga-checkins mailing list