diff options
Diffstat (limited to 'lib/hostinfo.php')
-rw-r--r-- | lib/hostinfo.php | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/hostinfo.php b/lib/hostinfo.php new file mode 100644 index 0000000..33b87d5 --- /dev/null +++ b/lib/hostinfo.php @@ -0,0 +1,60 @@ +<?php + +require_once('Symfony/Component/Yaml/autoload.php'); +use Symfony\Component\Yaml\Yaml; + +class Hostinfo +{ + public function __construct($path) + { + $this->path = $path; + if (!file_exists("$this->path/metadata/hosts")) { + throw new Exception("Invalid hostinfo path: $this->path"); + } + + $this->hosts = null; + $this->_data = array(); + } + + protected function load_hosts() + { + $hosts = Yaml::parse( + file_get_contents("$this->path/metadata/hosts")); + $this->hosts = $hosts['hosts']; + } + + protected function load_host($host) + { + $this->_data[$host] = Yaml::parse( + file_get_contents("$this->path/$host")); + } + + public function get_hosts() + { + if ($this->hosts === null) { + $this->load_hosts(); + } + + return $this->hosts; + } + + public function get_host($host) + { + if (!array_key_exists($host, $this->_data)) { + $this->load_host($host); + } + + return $this->_data[$host]; + } + + public function get_data() + { + $data = array(); + foreach ($this->get_hosts() as $host) { + $data[$host] = $this->get_host($host); + } + return $data; + } +} + +// vim: set ts=4 sw=4 tw=0 et : |