[libvirt] [libvirt-php][PATCH 3/8] examples: Rework a bit to resolve some warnings

Michal Privoznik mprivozn at redhat.com
Thu Oct 1 14:14:44 UTC 2015


Not only you should not use virDomain object as index to an array
in PHP where only strings and integers are allowed, you should
avoid mixing types passed to domain_get_info(). At some point a
domain name is passed, then virDomain is passed ...

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 examples/index.php   | 47 ++++++++++++++++++++++-------------------------
 examples/libvirt.php | 44 ++++++++------------------------------------
 2 files changed, 30 insertions(+), 61 deletions(-)

diff --git a/examples/index.php b/examples/index.php
index 94badb6..381baef 100644
--- a/examples/index.php
+++ b/examples/index.php
@@ -511,14 +511,14 @@
 				}
 		}
 
-		$res = $lv->get_domain_object($domName);
-		$dom = $lv->domain_get_info($res);
-		$mem = number_format($dom['memory'] / 1024, 2, '.', ' ').' MB';
-		$cpu = $dom['nrVirtCpu'];
-		$state = $lv->domain_state_translate($dom['state']);
-		$id = $lv->domain_get_id($res);
-		$arch = $lv->domain_get_arch($res);
-		$vnc = $lv->domain_get_vnc_port($res);
+		$dom = $lv->get_domain_object($domName);
+		$info = $lv->domain_get_info($dom);
+		$mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB';
+		$cpu = $info['nrVirtCpu'];
+		$state = $lv->domain_state_translate($info['state']);
+		$id = $lv->domain_get_id($dom);
+		$arch = $lv->domain_get_arch($dom);
+		$vnc = $lv->domain_get_vnc_port($dom);
 
 		if (!$id)
 			$id = 'N/A';
@@ -662,7 +662,6 @@
 		}
 
 		$doms = $lv->get_domains();
-		$domkeys = array_keys($doms);
 		echo "<table>
 			   <tr>
 				<th>Name</th>
@@ -682,23 +681,21 @@
 		echo "
 				<th>Action</th>
 			  </tr>";
-
 		$active = $tmp['active'];
-		for ($i = 0; $i < sizeof($doms); $i++) {
-			$name = $doms[$i];
-			$res = $lv->get_domain_by_name($name);
-			$uuid = libvirt_domain_get_uuid_string($res);
-			$dom = $lv->domain_get_info($name);
-			$mem = number_format($dom['memory'] / 1024, 2, '.', ' ').' MB';
-			$cpu = $dom['nrVirtCpu'];
-			$state = $lv->domain_state_translate($dom['state']);
-			$id = $lv->domain_get_id($res);
-			$arch = $lv->domain_get_arch($res);
-			$vnc = $lv->domain_get_vnc_port($res);
-			$nics = $lv->get_network_cards($res);
-			if (($diskcnt = $lv->get_disk_count($res)) > 0) {
-				$disks = $diskcnt.' / '.$lv->get_disk_capacity($res);
-				$diskdesc = 'Current physical size: '.$lv->get_disk_capacity($res, true);
+		foreach ($doms as $name) {
+			$dom = $lv->get_domain_object($name);
+			$uuid = libvirt_domain_get_uuid_string($dom);
+			$info = $lv->domain_get_info($dom);
+			$mem = number_format($info['memory'] / 1024, 2, '.', ' ').' MB';
+			$cpu = $info['nrVirtCpu'];
+			$state = $lv->domain_state_translate($info['state']);
+			$id = $lv->domain_get_id($dom);
+			$arch = $lv->domain_get_arch($dom);
+			$vnc = $lv->domain_get_vnc_port($dom);
+			$nics = $lv->get_network_cards($dom);
+			if (($diskcnt = $lv->get_disk_count($dom)) > 0) {
+				$disks = $diskcnt.' / '.$lv->get_disk_capacity($dom);
+				$diskdesc = 'Current physical size: '.$lv->get_disk_capacity($dom, true);
 			}
 			else {
 				$disks = '-';
diff --git a/examples/libvirt.php b/examples/libvirt.php
index 21e11d2..cb1a0ad 100644
--- a/examples/libvirt.php
+++ b/examples/libvirt.php
@@ -767,44 +767,17 @@
 			return libvirt_domain_get_name($res);
 		}
 
-		function domain_get_info_call($name = false, $name_override = false) {
-			$ret = array();
-
-			if ($name != false) {
-				$dom = $this->get_domain_object($name);
-				if (!$dom)
-					return false;
-
-				if ($name_override)
-					$name = $name_override;
-
-				$ret[$name] = libvirt_domain_get_info($dom);
-				return $ret;
-			}
-			else {
-				$doms = libvirt_list_domains($this->conn);
-				foreach ($doms as $dom) {
-					$tmp = $this->domain_get_name($dom);
-					$ret[$tmp] = libvirt_domain_get_info($dom);
-				}
-			}
-
-			ksort($ret);
-			return $ret;
-		}
-
-		function domain_get_info($name = false, $name_override = false) {
+		function domain_get_info($dom) {
 			if (!$this->allow_cached)
-				return $this->domain_get_info_call($name, $name_override);
+				return libvirt_domain_get_info($dom);
 
-			$domname = $name_override ? $name_override : $name;
-			$domkey  = $name_override ? $name_override : $this->domain_get_name($name);
-			if (!array_key_exists($domkey, $this->dominfos)) {
-				$tmp = $this->domain_get_info_call($name, $name_override);
-				$this->dominfos[$domkey] = $tmp[$domname];
+			$domname = libvirt_domain_get_name($dom);
+			if (!array_key_exists($domname, $this->dominfos)) {
+				$info = libvirt_domain_get_info($dom);
+				$this->dominfos[$domname] = $info;
 			}
 
-			return $this->dominfos[$domkey];
+			return $this->dominfos[$domname];
 		}
 
 		function get_last_error() {
@@ -940,8 +913,7 @@
 			$dom = $this->get_domain_object($domain);
 			if (!$dom)
 				return false;
-
-			$tmp = $this->domain_get_info( $domain, $name );
+			$tmp = $this->domain_get_info($dom);
 			if (!$tmp)
 				return $this->_set_last_error();
 			$ret = ( ($tmp['state'] == VIR_DOMAIN_RUNNING) || ($tmp['state'] == VIR_DOMAIN_BLOCKED) );
-- 
2.4.9




More information about the libvir-list mailing list