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
17 * base class for installer roles
19 require_once 'PEAR/Installer/Role/Common.php';
20 require_once 'PEAR/XMLParser.php';
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.3
28 * @link http://pear.php.net/package/PEAR
29 * @since Class available since Release 1.4.0a1
31 class PEAR_Installer_Role
34 * Set up any additional configuration variables that file roles require
36 * Never call this directly, it is called by the PEAR_Config constructor
39 public static function initializeConfig(&$config)
41 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
42 PEAR_Installer_Role::registerRoles();
45 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $class => $info) {
46 if (!$info['config_vars']) {
50 $config->_addConfigVars($class, $info['config_vars']);
55 * @param PEAR_PackageFile_v2
56 * @param string role name
58 * @return PEAR_Installer_Role_Common
60 public static function &factory($pkg, $role, &$config)
62 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
63 PEAR_Installer_Role::registerRoles();
66 if (!in_array($role, PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) {
71 $a = 'PEAR_Installer_Role_' . ucfirst($role);
72 if (!class_exists($a)) {
73 require_once str_replace('_', '/', $a) . '.php';
81 * Get a list of file roles that are valid for the particular release type.
83 * For instance, src files serve no purpose in regular php releases.
85 * @param bool clear cache
88 public static function getValidRoles($release, $clear = false)
90 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
91 PEAR_Installer_Role::registerRoles();
94 static $ret = array();
99 if (isset($ret[$release])) {
100 return $ret[$release];
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));
110 return $ret[$release];
114 * Get a list of roles that require their files to be installed
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
122 public static function getInstallableRoles($clear = false)
124 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
125 PEAR_Installer_Role::registerRoles();
138 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
139 if ($okreleases['installable']) {
140 $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
148 * Return an array of roles that are affected by the baseinstalldir attribute
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
156 public static function getBaseinstallRoles($clear = false)
158 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
159 PEAR_Installer_Role::registerRoles();
172 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
173 if ($okreleases['honorsbaseinstall']) {
174 $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
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
187 public static function getPhpRoles($clear = false)
189 if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
190 PEAR_Installer_Role::registerRoles();
203 foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
204 if ($okreleases['phpfile']) {
205 $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
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
220 * @return bool TRUE on success, a PEAR error on failure
222 public static function registerRoles($dir = null)
224 $GLOBALS['_PEAR_INSTALLER_ROLES'] = array();
225 $parser = new PEAR_XMLParser;
227 $dir = dirname(__FILE__) . '/Role';
230 if (!file_exists($dir) || !is_dir($dir)) {
231 return PEAR::raiseError("registerRoles: opendir($dir) failed: does not exist/is not directory");
234 $dp = @opendir($dir);
236 return PEAR::raiseError("registerRoles: opendir($dir) failed: $php_errmsg");
239 while ($entry = readdir($dp)) {
240 if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
244 $class = "PEAR_Installer_Role_".substr($entry, 0, -4);
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']);
254 $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data;
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);