PEAR2_SimpleChannelFrontendPEAR2_SimpleChannelFrontend-0.2.0/php/PEAR2/Pyrus/ChannelFile/v1/Servers.php

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace PEAR2\Pyrus\ChannelFile\v1;
class Servers implements \ArrayAccess, \Countable, \Iterator
{
    /**
     * @var \PEAR2\Pyrus\ChannelFile\v1
     */
    protected $parent;

    protected $info = array();

    protected $type = 'primary';

    function __construct($info, \PEAR2\Pyrus\ChannelFile\v1 $parent)
    {
        if (isset($info['mirror']) && !isset($info['mirror'][0])) {
            $info['mirror'] = array($info['mirror']);
        }

        $this->info   = $info;
        $this->parent = $parent;
    }

    function current()
    {
        $info = current($this->info['mirror']);
        return new Mirror($info, $this, $this->parent, key($this->info['mirror']));
    }

    function rewind()
    {
        if (!isset($this->info['mirror'])) {
            return;
        }

        reset($this->info['mirror']);
    }

    function key()
    {
        return key($this->info['mirror']);
    }

    function next()
    {
        return next($this->info['mirror']);
    }

    function valid()
    {
        if (!isset($this->info['mirror'])) {
            return false;
        }

        return current($this->info['mirror']);
    }

    function count()
    {
        if (!isset($this->info['mirror'])) {
            return 0;
        }

        return count($this->info['mirror']);
    }

    function offsetExists($mirror)
    {
        foreach ($this->info as $type=>$details) {
            if ($type == 'mirror'
                && isset($details[0])
                && $details[0]['attribs']['host'] == $mirror
            ) {
                return true;
            }
        }

        return false;
    }

    function offsetUnset($mirror)
    {
        if (!isset($this->info['mirror'])) {
            return;
        }

        foreach ($this->info['mirror'] as $i => $details) {
            if (isset($details['attribs']) && isset($details['attribs']['host']) &&
                    $details['attribs']['host'] == $mirror
            ) {
                unset($this->info['mirror'][$i]);
                $this->info['mirror'] = array_values($this->info['mirror']);
                return $this->save();
            }
        }
    }

    function offsetGet($mirror)
    {
        if (!isset($this->info['mirror'])) {
            return new Mirror(array('attribs' => array('host' => $mirror)), $this, $this->parent, 0);
        }

        foreach ($this->info['mirror'] as $i => $details) {
            if (isset($details['attribs']) && isset($details['attribs']['host']) &&
                $details['attribs']['host'] == $mirror
            ) {
                return new Mirror($details, $this, $this->parent, $i);
            }
        }

        return new Mirror(array('attribs' => array('host' => $mirror)), $this, $this->parent, count($this->info['mirror']));
    }

    function offsetSet($mirror, $value)
    {
        if ($value === null) {
            return $this->offsetUnset($mirror);
        }

        if (!($value instanceof Mirror)) {
            throw new \PEAR2\Pyrus\ChannelFile\Exception('Can only set mirror to a ' .
                        '\PEAR2\Pyrus\ChannelFile\v1\Mirror object');
        }

        $info = $value->getInfo();
        if ($mirror != $value->server) {
            $info['attribs']['host'] = $mirror;
        }

        foreach ($this->info['mirror'] as $i => $details) {
            if (isset($details['attribs']) && isset($details['attribs']['host']) &&
                $details['attribs']['host'] == $mirror
            ) {
                $this->setMirror($i, $info);
                return $this->save();
            }
        }

        $this->setMirror(count($this->info['mirror']), $info);
        $this->save();
    }

    function setMirror($index, $info)
    {
        $this->info['mirror'][$index] = $info;
    }

    function save()
    {
        $info = $this->info;
        if (!$info['mirror']) {
            return $this->parent->rawmirrors = null;
        }

        if (count($info['mirror']) === 1) {
            return $this->parent->rawmirrors = $info['mirror'][0];
        }

        $this->parent->rawmirrors = $info['mirror'];
    }
}
EOF