summaryrefslogtreecommitdiffstats
path: root/lib/hostinfo.php
blob: 33b87d53cec0d7dff983056a1db38533ff9d3afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 :