Updated PEAR and PEAR packages.
[timetracker.git] / WEB-INF / lib / pear / PEAR / Installer / Role.php
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>
10  * @copyright  1997-2009 The Authors
11  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
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>
25  * @copyright  1997-2009 The Authors
26  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
27  * @version    Release: 1.10.1
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      */
39     public static function initializeConfig(&$config)
40     {
41         if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
42             PEAR_Installer_Role::registerRoles();
43         }
44
45         foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $class => $info) {
46             if (!$info['config_vars']) {
47                 continue;
48             }
49
50             $config->_addConfigVars($class, $info['config_vars']);
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      */
60     public static function &factory($pkg, $role, &$config)
61     {
62         if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
63             PEAR_Installer_Role::registerRoles();
64         }
65
66         if (!in_array($role, PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) {
67             $a = false;
68             return $a;
69         }
70
71         $a = 'PEAR_Installer_Role_' . ucfirst($role);
72         if (!class_exists($a)) {
73             require_once str_replace('_', '/', $a) . '.php';
74         }
75
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      */
88     public static function getValidRoles($release, $clear = false)
89     {
90         if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
91             PEAR_Installer_Role::registerRoles();
92         }
93
94         static $ret = array();
95         if ($clear) {
96             $ret = array();
97         }
98
99         if (isset($ret[$release])) {
100             return $ret[$release];
101         }
102
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         }
109
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      */
122     public static function getInstallableRoles($clear = false)
123     {
124         if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
125             PEAR_Installer_Role::registerRoles();
126         }
127
128         static $ret;
129         if ($clear) {
130             unset($ret);
131         }
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));
141             }
142         }
143
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      */
156     public static function getBaseinstallRoles($clear = false)
157     {
158         if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
159             PEAR_Installer_Role::registerRoles();
160         }
161
162         static $ret;
163         if ($clear) {
164             unset($ret);
165         }
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));
175             }
176         }
177
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     public static function getPhpRoles($clear = false)
188     {
189         if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
190             PEAR_Installer_Role::registerRoles();
191         }
192
193         static $ret;
194         if ($clear) {
195             unset($ret);
196         }
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));
206             }
207         }
208
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      */
222     public static function registerRoles($dir = null)
223     {
224         $GLOBALS['_PEAR_INSTALLER_ROLES'] = array();
225         $parser = new PEAR_XMLParser;
226         if ($dir === null) {
227             $dir = dirname(__FILE__) . '/Role';
228         }
229
230         if (!file_exists($dir) || !is_dir($dir)) {
231             return PEAR::raiseError("registerRoles: opendir($dir) failed: does not exist/is not directory");
232         }
233
234         $dp = @opendir($dir);
235         if (empty($dp)) {
236             return PEAR::raiseError("registerRoles: opendir($dir) failed: $php_errmsg");
237         }
238
239         while ($entry = readdir($dp)) {
240             if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
241                 continue;
242             }
243
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                 }
253
254                 $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data;
255             }
256         }
257
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     }
266 }