Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 1
<?php
2
/**
3
 * PEAR_Installer_Role
4
 *
5
 * PHP versions 4 and 5
6
 *
7
 * @category   pear
8
 * @package    PEAR
9
 * @author     Greg Beaver <cellog@php.net>
187 mathias 10
 * @copyright  1997-2009 The Authors
11
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
94 jpm 12
 * @link       http://pear.php.net/package/PEAR
13
 * @since      File available since Release 1.4.0a1
14
 */
15
 
16
/**
17
 * base class for installer roles
18
 */
19
require_once 'PEAR/Installer/Role/Common.php';
20
require_once 'PEAR/XMLParser.php';
21
/**
22
 * @category   pear
23
 * @package    PEAR
24
 * @author     Greg Beaver <cellog@php.net>
187 mathias 25
 * @copyright  1997-2009 The Authors
26
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
27
 * @version    Release: 1.10.1
94 jpm 28
 * @link       http://pear.php.net/package/PEAR
29
 * @since      Class available since Release 1.4.0a1
30
 */
31
class PEAR_Installer_Role
32
{
33
    /**
34
     * Set up any additional configuration variables that file roles require
35
     *
36
     * Never call this directly, it is called by the PEAR_Config constructor
37
     * @param PEAR_Config
38
     */
187 mathias 39
    public static function initializeConfig(&$config)
94 jpm 40
    {
41
        if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
42
            PEAR_Installer_Role::registerRoles();
43
        }
187 mathias 44
 
94 jpm 45
        foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $class => $info) {
46
            if (!$info['config_vars']) {
47
                continue;
48
            }
187 mathias 49
 
50
            $config->_addConfigVars($class, $info['config_vars']);
94 jpm 51
        }
52
    }
53
 
54
    /**
55
     * @param PEAR_PackageFile_v2
56
     * @param string role name
57
     * @param PEAR_Config
58
     * @return PEAR_Installer_Role_Common
59
     */
187 mathias 60
    public static function &factory($pkg, $role, &$config)
94 jpm 61
    {
62
        if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
63
            PEAR_Installer_Role::registerRoles();
64
        }
187 mathias 65
 
94 jpm 66
        if (!in_array($role, PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) {
67
            $a = false;
68
            return $a;
69
        }
187 mathias 70
 
94 jpm 71
        $a = 'PEAR_Installer_Role_' . ucfirst($role);
72
        if (!class_exists($a)) {
73
            require_once str_replace('_', '/', $a) . '.php';
74
        }
187 mathias 75
 
94 jpm 76
        $b = new $a($config);
77
        return $b;
78
    }
79
 
80
    /**
81
     * Get a list of file roles that are valid for the particular release type.
82
     *
83
     * For instance, src files serve no purpose in regular php releases.
84
     * @param string
85
     * @param bool clear cache
86
     * @return array
87
     */
187 mathias 88
    public static function getValidRoles($release, $clear = false)
94 jpm 89
    {
90
        if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
91
            PEAR_Installer_Role::registerRoles();
92
        }
187 mathias 93
 
94 jpm 94
        static $ret = array();
95
        if ($clear) {
96
            $ret = array();
97
        }
187 mathias 98
 
94 jpm 99
        if (isset($ret[$release])) {
100
            return $ret[$release];
101
        }
187 mathias 102
 
94 jpm 103
        $ret[$release] = array();
104
        foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
105
            if (in_array($release, $okreleases['releasetypes'])) {
106
                $ret[$release][] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
107
            }
108
        }
187 mathias 109
 
94 jpm 110
        return $ret[$release];
111
    }
112
 
113
    /**
114
     * Get a list of roles that require their files to be installed
115
     *
116
     * Most roles must be installed, but src and package roles, for instance
117
     * are pseudo-roles.  src files are compiled into a new extension.  Package
118
     * roles are actually fully bundled releases of a package
119
     * @param bool clear cache
120
     * @return array
121
     */
187 mathias 122
    public static function getInstallableRoles($clear = false)
94 jpm 123
    {
124
        if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
125
            PEAR_Installer_Role::registerRoles();
126
        }
187 mathias 127
 
94 jpm 128
        static $ret;
129
        if ($clear) {
130
            unset($ret);
131
        }
187 mathias 132
 
133
        if (isset($ret)) {
134
            return $ret;
135
        }
136
 
137
        $ret = array();
138
        foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
139
            if ($okreleases['installable']) {
140
                $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
94 jpm 141
            }
142
        }
187 mathias 143
 
94 jpm 144
        return $ret;
145
    }
146
 
147
    /**
148
     * Return an array of roles that are affected by the baseinstalldir attribute
149
     *
150
     * Most roles ignore this attribute, and instead install directly into:
151
     * PackageName/filepath
152
     * so a tests file tests/file.phpt is installed into PackageName/tests/filepath.php
153
     * @param bool clear cache
154
     * @return array
155
     */
187 mathias 156
    public static function getBaseinstallRoles($clear = false)
94 jpm 157
    {
158
        if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
159
            PEAR_Installer_Role::registerRoles();
160
        }
187 mathias 161
 
94 jpm 162
        static $ret;
163
        if ($clear) {
164
            unset($ret);
165
        }
187 mathias 166
 
167
        if (isset($ret)) {
168
            return $ret;
169
        }
170
 
171
        $ret = array();
172
        foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
173
            if ($okreleases['honorsbaseinstall']) {
174
                $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
94 jpm 175
            }
176
        }
187 mathias 177
 
94 jpm 178
        return $ret;
179
    }
180
 
181
    /**
182
     * Return an array of file roles that should be analyzed for PHP content at package time,
183
     * like the "php" role.
184
     * @param bool clear cache
185
     * @return array
186
     */
187 mathias 187
    public static function getPhpRoles($clear = false)
94 jpm 188
    {
189
        if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
190
            PEAR_Installer_Role::registerRoles();
191
        }
187 mathias 192
 
94 jpm 193
        static $ret;
194
        if ($clear) {
195
            unset($ret);
196
        }
187 mathias 197
 
198
        if (isset($ret)) {
199
            return $ret;
200
        }
201
 
202
        $ret = array();
203
        foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
204
            if ($okreleases['phpfile']) {
205
                $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
94 jpm 206
            }
207
        }
187 mathias 208
 
94 jpm 209
        return $ret;
210
    }
211
 
212
    /**
213
     * Scan through the Command directory looking for classes
214
     * and see what commands they implement.
215
     * @param string which directory to look for classes, defaults to
216
     *               the Installer/Roles subdirectory of
217
     *               the directory from where this file (__FILE__) is
218
     *               included.
219
     *
220
     * @return bool TRUE on success, a PEAR error on failure
221
     */
187 mathias 222
    public static function registerRoles($dir = null)
94 jpm 223
    {
224
        $GLOBALS['_PEAR_INSTALLER_ROLES'] = array();
225
        $parser = new PEAR_XMLParser;
226
        if ($dir === null) {
227
            $dir = dirname(__FILE__) . '/Role';
228
        }
187 mathias 229
 
94 jpm 230
        if (!file_exists($dir) || !is_dir($dir)) {
187 mathias 231
            return PEAR::raiseError("registerRoles: opendir($dir) failed: does not exist/is not directory");
94 jpm 232
        }
187 mathias 233
 
94 jpm 234
        $dp = @opendir($dir);
235
        if (empty($dp)) {
187 mathias 236
            return PEAR::raiseError("registerRoles: opendir($dir) failed: $php_errmsg");
94 jpm 237
        }
187 mathias 238
 
94 jpm 239
        while ($entry = readdir($dp)) {
240
            if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
241
                continue;
242
            }
187 mathias 243
 
94 jpm 244
            $class = "PEAR_Installer_Role_".substr($entry, 0, -4);
245
            // List of roles
246
            if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'][$class])) {
247
                $file = "$dir/$entry";
248
                $parser->parse(file_get_contents($file));
249
                $data = $parser->getData();
250
                if (!is_array($data['releasetypes'])) {
251
                    $data['releasetypes'] = array($data['releasetypes']);
252
                }
187 mathias 253
 
94 jpm 254
                $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data;
255
            }
256
        }
187 mathias 257
 
94 jpm 258
        closedir($dp);
259
        ksort($GLOBALS['_PEAR_INSTALLER_ROLES']);
260
        PEAR_Installer_Role::getBaseinstallRoles(true);
261
        PEAR_Installer_Role::getInstallableRoles(true);
262
        PEAR_Installer_Role::getPhpRoles(true);
263
        PEAR_Installer_Role::getValidRoles('****', true);
264
        return true;
265
    }
187 mathias 266
}