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 * @version CVS: $Id: Role.php 313023 2011-07-06 19:17:11Z dufuz $
13 * @link http://pear.php.net/package/PEAR
14 * @since File available since Release 1.4.0a1
18 * base class for installer roles
20 require_once 'PEAR/Installer/Role/Common.php';
21 require_once 'PEAR/XMLParser.php';
25 * @author Greg Beaver <cellog@php.net>
26 * @copyright 1997-2009 The Authors
27 * @license http://opensource.org/licenses/bsd-license.php New BSD License
28 * @version Release: 1.9.4
29 * @link http://pear.php.net/package/PEAR
30 * @since Class available since Release 1.4.0a1
32 class PEAR_Installer_Role
35 * Set up any additional configuration variables that file roles require
37 * Never call this directly, it is called by the PEAR_Config constructor
42 function initializeConfig(&$config)
44 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
45 PEAR_Installer_Role::registerRoles();
48 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $class => $info) {
49 if (!$info['config_vars']) {
53 $config->_addConfigVars($class, $info['config_vars']);
58 * @param PEAR_PackageFile_v2
59 * @param string role name
61 * @return PEAR_Installer_Role_Common
64 function &factory($pkg, $role, &$config)
66 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
67 PEAR_Installer_Role::registerRoles();
70 if (!in_array($role, PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) {
75 $a = 'PEAR_Installer_Role_' . ucfirst($role);
76 if (!class_exists($a)) {
77 require_once str_replace('_', '/', $a) . '.php';
85 * Get a list of file roles that are valid for the particular release type.
87 * For instance, src files serve no purpose in regular php releases.
89 * @param bool clear cache
93 function getValidRoles($release, $clear = false)
95 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
96 PEAR_Installer_Role::registerRoles();
99 static $ret = array();
104 if (isset($ret[$release])) {
105 return $ret[$release];
108 $ret[$release] = array();
109 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
110 if (in_array($release, $okreleases['releasetypes'])) {
111 $ret[$release][] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
115 return $ret[$release];
119 * Get a list of roles that require their files to be installed
121 * Most roles must be installed, but src and package roles, for instance
122 * are pseudo-roles. src files are compiled into a new extension. Package
123 * roles are actually fully bundled releases of a package
124 * @param bool clear cache
128 function getInstallableRoles($clear = false)
130 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
131 PEAR_Installer_Role::registerRoles();
144 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
145 if ($okreleases['installable']) {
146 $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
154 * Return an array of roles that are affected by the baseinstalldir attribute
156 * Most roles ignore this attribute, and instead install directly into:
157 * PackageName/filepath
158 * so a tests file tests/file.phpt is installed into PackageName/tests/filepath.php
159 * @param bool clear cache
163 function getBaseinstallRoles($clear = false)
165 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
166 PEAR_Installer_Role::registerRoles();
179 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
180 if ($okreleases['honorsbaseinstall']) {
181 $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
189 * Return an array of file roles that should be analyzed for PHP content at package time,
190 * like the "php" role.
191 * @param bool clear cache
195 function getPhpRoles($clear = false)
197 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
198 PEAR_Installer_Role::registerRoles();
211 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
212 if ($okreleases['phpfile']) {
213 $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
221 * Scan through the Command directory looking for classes
222 * and see what commands they implement.
223 * @param string which directory to look for classes, defaults to
224 * the Installer/Roles subdirectory of
225 * the directory from where this file (__FILE__) is
228 * @return bool TRUE on success, a PEAR error on failure
232 function registerRoles($dir = null)
234 $GLOBALS['_PEAR_INSTALLER_ROLES'] = array();
235 $parser = new PEAR_XMLParser;
237 $dir = dirname(__FILE__) . '/Role';
240 if (!file_exists($dir) || !is_dir($dir)) {
241 return PEAR::raiseError("registerRoles: opendir($dir) failed: does not exist/is not directory");
244 $dp = @opendir($dir);
246 return PEAR::raiseError("registerRoles: opendir($dir) failed: $php_errmsg");
249 while ($entry = readdir($dp)) {
250 if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
254 $class = "PEAR_Installer_Role_".substr($entry, 0, -4);
256 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'][$class])) {
257 $file = "$dir/$entry";
258 $parser->parse(file_get_contents($file));
259 $data = $parser->getData();
260 if (!is_array($data['releasetypes'])) {
261 $data['releasetypes'] = array($data['releasetypes']);
264 $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data;
269 ksort($GLOBALS['_PEAR_INSTALLER_ROLES']);
270 PEAR_Installer_Role::getBaseinstallRoles(true);
271 PEAR_Installer_Role::getInstallableRoles(true);
272 PEAR_Installer_Role::getPhpRoles(true);
273 PEAR_Installer_Role::getValidRoles('****', true);