PEAR2_SimpleChannelServer-0.2.0PEAR2_SimpleChannelServer-0.2.0/php/PEAR2/Pyrus/ChannelRegistry/Xml.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
 * \PEAR2\Pyrus\ChannelRegistry\Xml
 *
 * PHP version 5
 *
 * @category  PEAR2
 * @package   PEAR2_Pyrus
 * @author    Greg Beaver <cellog@php.net>
 * @copyright 2010 The PEAR Group
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://svn.php.net/viewvc/pear2/Pyrus/
 */

/**
 * An implementation of a Pyrus channel registry within XML files.
 *
 * @category  PEAR2
 * @package   PEAR2_Pyrus
 * @author    Greg Beaver <cellog@php.net>
 * @copyright 2010 The PEAR Group
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      http://svn.php.net/viewvc/pear2/Pyrus/
 */
namespace PEAR2\Pyrus\ChannelRegistry;
use \PEAR2\Pyrus\Main as Main;
class Xml extends Base
{
    protected $channelpath;
    /**
     * Initialize the registry
     *
     * @param string $path
     */
    function __construct($path, $readonly = false)
    {
        $this->readonly = $readonly;
        if (isset(Main::$options['packagingroot'])) {
            $path = Main::prepend(Main::$options['packagingroot'], $path);
        }

        $this->path = $path;
        $this->channelpath = $path . DIRECTORY_SEPARATOR . '.xmlregistry' . DIRECTORY_SEPARATOR . 'channels';
        if (1 === $this->exists('pear.php.net')) {
            $this->initialized = false;
        } else {
            $this->initialized = true;
        }
    }

    /**
     * Convert a name into a path-friendly name
     *
     * @param string $name
     */
    private function _mung($name)
    {
        return str_replace(array('/', '\\'), array('##', '###'), $name);
    }

    private function _unmung($name)
    {
        return str_replace(array('##', '###'), array('/', '\\'), $name);
    }

    /**
     * Get the filename to store a channel
     *
     * @param \PEAR2\Pyrus\ChannelInterface|string $channel Channel to save
     *
     * @return string
     */
    protected function getChannelFile($channel)
    {
        if ($channel instanceof \PEAR2\Pyrus\ChannelInterface) {
            $channel = $channel->name;
        }

        return $this->channelpath . DIRECTORY_SEPARATOR . 'channel-' .
            $this->_mung($channel) . '.xml';
    }

    /**
     * Get the filename for a channel alias.
     *
     * @param string $alias Alias to save
     *
     * @return string
     */
    protected function getAliasFile($alias)
    {
        return $this->channelpath . DIRECTORY_SEPARATOR . 'channelalias-' .
            $this->_mung($alias) . '.txt';
    }

    function channelFromAlias($alias)
    {
        if (!$this->initialized) {
            return parent::channelFromAlias($alias);
        }

        if (file_exists($this->getAliasFile($alias))) {
            return file_get_contents($this->getAliasFile($alias));
        }

        if (file_exists($this->getChannelFile($alias))) {
            return $alias;
        }

        throw new Exception('Unknown channel/alias: ' . $alias);
    }

    /**
     * Check if the channel has been discovered.
     *
     * @param string $channel Name of the channel
     * @param bool   $strict  Allow aliases or not
     *
     * @return bool
     */
    function exists($channel, $strict = true)
    {
        if (file_exists($this->getChannelFile($channel))) {
            return true;
        }

        if ($strict) {
            return parent::exists($channel, $strict);
        }

        if (file_exists($this->getAliasFile($channel))) {
            return true;
        }

        return parent::exists($channel, $strict);
    }

    function add(\PEAR2\Pyrus\ChannelInterface $channel, $update = false, $lastmodified = false)
    {
        if ($this->readonly) {
            throw new Exception('Cannot add channel, registry is read-only');
        }

        $this->lazyInit();

        $file = $this->getChannelFile($channel);
        if (@file_exists($file)) {
            throw new Exception('Error: channel ' .$channel->name . ' has already been discovered');
        }

        if (!@is_dir(dirname($file))) {
            mkdir(dirname($file), 0755, true);
        }

        file_put_contents($file, (string) $channel);
        file_put_contents($this->getAliasFile($channel->alias), $channel->name);
    }

    function update(\PEAR2\Pyrus\ChannelInterface $channel)
    {
        if ($this->readonly) {
            throw new Exception('Cannot update channel, registry is read-only');
        }

        $this->lazyInit();

        $file = $this->getChannelFile($channel);
        if (!@file_exists($file)) {
            throw new Exception('Error: channel ' . $channel->name . ' is unknown');
        }

        file_put_contents($file, (string) $channel);
        file_put_contents($this->getAliasFile($channel->alias), $channel->name);
    }

    function delete(\PEAR2\Pyrus\ChannelInterface $channel)
    {
        if ($this->readonly) {
            throw new Exception('Cannot delete channel, registry is read-only');
        }

        $name = $channel->name;
        if (in_array($name, $this->getDefaultChannels())) {
            throw new Exception('Cannot delete default channel ' . $channel->name);
        }

        $this->lazyInit();

        if (!$this->exists($name)) {
            return true;
        }

        if ($this->packageCount($name)) {
            throw new Exception('Cannot delete channel ' . $name . ', packages are installed');
        }

        @unlink($this->getChannelFile($channel));
        @unlink($this->getAliasFile($channel->alias));
    }

    function get($channel, $strict = true)
    {
        $exists = $this->exists($channel, $strict);
        if ($exists === false) {
            throw new Exception('Unknown channel: ' . $channel);
        }

        $channel = $this->channelFromAlias($channel);
        if (1 === $exists) {
            return $this->getDefaultChannel($channel);
        }

        $chan = new \PEAR2\Pyrus\ChannelFile($this->getChannelFile($channel));
        return new Channel($this, $chan->getArray());
    }

    /**
     * List all discovered channels
     *
     * @return array
     */
    function listChannels()
    {
        if (!$this->initialized) {
            return $this->getDefaultChannels();
        }

        $ret = array();
        $dir = new \DirectoryIterator($this->channelpath);
        foreach (new \RegexIterator($dir, '/channel-(.+?)\.xml/', \RegexIterator::GET_MATCH) as $file) {
            $ret[] = $this->get($this->_unmung($file[1]))->name;
        }
        unset($dir);

        return $ret;
    }
}
EOF