PEAR2_SimpleChannelFrontendPEAR2_SimpleChannelFrontend-0.2.0/php/PEAR2/Pyrus/Installer/Role.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
 * \PEAR2\Pyrus\Installer\Role
 *
 * 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/
 */

/**
 * Base class for installation roles for 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\Installer;
use \PEAR2\Pyrus\Config as Config;
class Role
{
    static private $_roles;

    /**
     * Set up any additional configuration variables that file roles require
     *
     * Never call this directly, it is called by \PEAR2\Pyrus\Config constructor
     * @param \PEAR2\Pyrus\Config
     * @access private
     * @static
     */
    public static function initializeConfig(Config $config)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        foreach (self::$_roles as $class => $info) {
            if (!$info['config_vars']) {
                continue;
            }

            $config->addConfigValue($info['config_vars']);
        }
    }

    /**
     * @param string package type
     * @param string role name
     * @static
     */
    static function factory($packagetype, $role)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        if (!in_array($role, self::getValidRoles($packagetype))) {
            throw new Exception('Invalid role ' . $role . ' requested for package type ' . $packagetype);
        }

        $class = self::$_roles[$role]['class'];
        if (!class_exists($class, true)) {
            throw new Exception('Unable to load custom class ' . $class . ' for ' . $role);
        }
        return new $class(Config::current(), self::$_roles[$role]);
    }

    /**
     * Get a list of file roles that are valid for the particular release type.
     *
     * For instance, src files serve no purpose in regular php releases.
     * @param string
     * @param bool clear cache
     * @return array
     * @static
     */
    static function getValidRoles($release, $clear = false)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        static $ret = array();
        if ($clear) {
            $ret = array();
        }

        if (isset($ret[$release])) {
            return $ret[$release];
        }

        $ret[$release] = array();
        foreach (self::$_roles as $role => $okreleases) {
            if (in_array($release, $okreleases['releasetypes'])) {
                $ret[$release][] = $role;
            }
        }

        return $ret[$release];
    }

    /**
     * Get a list of roles that require their files to be installed
     *
     * Most roles must be installed, but src and package roles, for instance
     * are pseudo-roles.  src files are compiled into a new extension.  Package
     * roles are actually fully bundled releases of a package
     * @param bool clear cache
     * @return array
     * @static
     */
    static function getInstallableRoles($clear = false)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        static $ret;
        if ($clear) {
            unset($ret);
        }

        if (!isset($ret)) {
            $ret = array();
            foreach (self::$_roles as $role => $okreleases) {
                if ($okreleases['installable']) {
                    $ret[] = $role;
                }
            }
        }

        return $ret;
    }

    /**
     * Return an array of roles that are affected by the baseinstalldir attribute
     *
     * Most roles ignore this attribute, and instead install directly into:
     * PackageName/filepath
     * so a tests file tests/file.phpt is installed into PackageName/tests/filepath.php
     * @param bool clear cache
     * @return array
     * @static
     */
    static function getBaseinstallRoles($clear = false)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        static $ret;
        if ($clear) {
            unset($ret);
        }

        if (!isset($ret)) {
            $ret = array();
            foreach (self::$_roles as $role => $okreleases) {
                if ($okreleases['honorsbaseinstall']) {
                    $ret[] = $role;
                }
            }
        }

        return $ret;
    }

    /**
     * Scan through the Command directory looking for classes
     * and see what commands they implement.
     * @param string which directory to look for classes, defaults to
     *               the Installer/Roles subdirectory of
     *               the directory from where this file (__FILE__) is
     *               included.
     *
     * @return bool TRUE on success, a PEAR error on failure
     * @access public
     * @static
     */
    static function registerRoles($dir = null)
    {
        self::$_roles = array();
        $parser = new \PEAR2\Pyrus\XMLParser;
        if ($dir === null) {
            $dir = __DIR__ . '/Role';
        }

        if (!file_exists($dir) || !is_dir($dir)) {
            throw new Role\Exception("registerRoles: opendir($dir) failed");
        }

        $dp = @opendir($dir);
        if (empty($dp)) {
            throw new Role\Exception("registerRoles: opendir($dir) failed");
        }

        $schemapath = \PEAR2\Pyrus\Main::getDataPath() . '/customrole-2.0.xsd';
        if (!file_exists($schemapath)) {
            $schemapath = realpath(__DIR__ . '/../../../../data/customrole-2.0.xsd');
        }

        while ($entry = readdir($dp)) {
            if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
                continue;
            }

            $role = strtolower(basename($entry, '.xml'));
            // List of roles
            if (!isset(self::$_roles[$role])) {
                $file = "$dir/$entry";
                $data = $parser->parse($file, $schemapath);
                $data = $data['role'];
                if (!is_array($data['releasetypes'])) {
                    $data['releasetypes'] = array($data['releasetypes']);
                }

                self::$_roles[$role] = $data;
            }
        }

        closedir($dp);
        $roles = self::$_roles;
        ksort($roles);
        self::$_roles = $roles;
        self::getBaseinstallRoles(true);
        self::getInstallableRoles(true);
        self::getValidRoles('****', true);
        return true;
    }

    static function registerCustomRole($info)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        self::$_roles[$info['name']] = $info;
        $roles = self::$_roles;
        ksort($roles);
        self::$_roles = $roles;
        self::getBaseinstallRoles(true);
        self::getInstallableRoles(true);
        self::getValidRoles('****', true);
        if (isset($info['configvar'])) {
            if (!isset($info['configvar'][0])) {
                $info['configvar'] = array($info['configvar']);
            }

            foreach ($info['configvar'] as $configvar) {
                if (($configvar['configtype'] == 'system' && in_array($configvar['name'], Config::current()->customsystemvars)) ||
                    ($configvar['configtype'] == 'user' && in_array($configvar['name'], Config::current()->customuservars)) ||
                    ($configvar['configtype'] == 'channel' && in_array($configvar['name'], Config::current()->customchannelvars))) {
                    continue;
                }

                $default = $configvar['default'];
                if (false !== strpos($default, '<?php')) {
                    $tmp = Config::current()->temp_dir . DIRECTORY_SEPARATOR . '.configdefault.php';
                    if (!file_exists(dirname($tmp))) {
                        mkdir(dirname($tmp), 0755, true);
                    }

                    if (file_put_contents($tmp, $default) === false) {
                        throw new Role\Exception("Cannot create custom role configuration file $tmp");
                    }

                    $getDefault = function() use ($tmp) {
                        include $tmp;
                        return $default;
                    };

                    $default = $getDefault();
                }

                Config::addConfigValue($configvar['name'], $default, $configvar['configtype']);
            }
        }
    }

    /**
     * Retrieve configuration information about a file role from its XML info
     *
     * @param string $role Role Classname, as in "\PEAR2\Pyrus\Installer\Role\Data"
     * @return array
     */
    static function getInfo($role)
    {
        if (!isset(self::$_roles)) {
            self::registerRoles();
        }

        if (empty(self::$_roles[$role])) {
            throw new Role\Exception('Unknown Role: "' . $role . '"');
        }

        return self::$_roles[$role];
    }
}
EOF