Role.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * @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
  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.9.4
  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. * @access private
  39. * @static
  40. */
  41. function initializeConfig(&$config)
  42. {
  43. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
  44. PEAR_Installer_Role::registerRoles();
  45. }
  46. foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $class => $info) {
  47. if (!$info['config_vars']) {
  48. continue;
  49. }
  50. $config->_addConfigVars($class, $info['config_vars']);
  51. }
  52. }
  53. /**
  54. * @param PEAR_PackageFile_v2
  55. * @param string role name
  56. * @param PEAR_Config
  57. * @return PEAR_Installer_Role_Common
  58. * @static
  59. */
  60. function &factory($pkg, $role, &$config)
  61. {
  62. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
  63. PEAR_Installer_Role::registerRoles();
  64. }
  65. if (!in_array($role, PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) {
  66. $a = false;
  67. return $a;
  68. }
  69. $a = 'PEAR_Installer_Role_' . ucfirst($role);
  70. if (!class_exists($a)) {
  71. require_once str_replace('_', '/', $a) . '.php';
  72. }
  73. $b = new $a($config);
  74. return $b;
  75. }
  76. /**
  77. * Get a list of file roles that are valid for the particular release type.
  78. *
  79. * For instance, src files serve no purpose in regular php releases.
  80. * @param string
  81. * @param bool clear cache
  82. * @return array
  83. * @static
  84. */
  85. function getValidRoles($release, $clear = false)
  86. {
  87. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
  88. PEAR_Installer_Role::registerRoles();
  89. }
  90. static $ret = array();
  91. if ($clear) {
  92. $ret = array();
  93. }
  94. if (isset($ret[$release])) {
  95. return $ret[$release];
  96. }
  97. $ret[$release] = array();
  98. foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
  99. if (in_array($release, $okreleases['releasetypes'])) {
  100. $ret[$release][] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
  101. }
  102. }
  103. return $ret[$release];
  104. }
  105. /**
  106. * Get a list of roles that require their files to be installed
  107. *
  108. * Most roles must be installed, but src and package roles, for instance
  109. * are pseudo-roles. src files are compiled into a new extension. Package
  110. * roles are actually fully bundled releases of a package
  111. * @param bool clear cache
  112. * @return array
  113. * @static
  114. */
  115. function getInstallableRoles($clear = false)
  116. {
  117. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
  118. PEAR_Installer_Role::registerRoles();
  119. }
  120. static $ret;
  121. if ($clear) {
  122. unset($ret);
  123. }
  124. if (isset($ret)) {
  125. return $ret;
  126. }
  127. $ret = array();
  128. foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
  129. if ($okreleases['installable']) {
  130. $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
  131. }
  132. }
  133. return $ret;
  134. }
  135. /**
  136. * Return an array of roles that are affected by the baseinstalldir attribute
  137. *
  138. * Most roles ignore this attribute, and instead install directly into:
  139. * PackageName/filepath
  140. * so a tests file tests/file.phpt is installed into PackageName/tests/filepath.php
  141. * @param bool clear cache
  142. * @return array
  143. * @static
  144. */
  145. function getBaseinstallRoles($clear = false)
  146. {
  147. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
  148. PEAR_Installer_Role::registerRoles();
  149. }
  150. static $ret;
  151. if ($clear) {
  152. unset($ret);
  153. }
  154. if (isset($ret)) {
  155. return $ret;
  156. }
  157. $ret = array();
  158. foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
  159. if ($okreleases['honorsbaseinstall']) {
  160. $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
  161. }
  162. }
  163. return $ret;
  164. }
  165. /**
  166. * Return an array of file roles that should be analyzed for PHP content at package time,
  167. * like the "php" role.
  168. * @param bool clear cache
  169. * @return array
  170. * @static
  171. */
  172. function getPhpRoles($clear = false)
  173. {
  174. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) {
  175. PEAR_Installer_Role::registerRoles();
  176. }
  177. static $ret;
  178. if ($clear) {
  179. unset($ret);
  180. }
  181. if (isset($ret)) {
  182. return $ret;
  183. }
  184. $ret = array();
  185. foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) {
  186. if ($okreleases['phpfile']) {
  187. $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role));
  188. }
  189. }
  190. return $ret;
  191. }
  192. /**
  193. * Scan through the Command directory looking for classes
  194. * and see what commands they implement.
  195. * @param string which directory to look for classes, defaults to
  196. * the Installer/Roles subdirectory of
  197. * the directory from where this file (__FILE__) is
  198. * included.
  199. *
  200. * @return bool TRUE on success, a PEAR error on failure
  201. * @access public
  202. * @static
  203. */
  204. function registerRoles($dir = null)
  205. {
  206. $GLOBALS['_PEAR_INSTALLER_ROLES'] = array();
  207. $parser = new PEAR_XMLParser;
  208. if ($dir === null) {
  209. $dir = dirname(__FILE__) . '/Role';
  210. }
  211. if (!file_exists($dir) || !is_dir($dir)) {
  212. return PEAR::raiseError("registerRoles: opendir($dir) failed: does not exist/is not directory");
  213. }
  214. $dp = @opendir($dir);
  215. if (empty($dp)) {
  216. return PEAR::raiseError("registerRoles: opendir($dir) failed: $php_errmsg");
  217. }
  218. while ($entry = readdir($dp)) {
  219. if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
  220. continue;
  221. }
  222. $class = "PEAR_Installer_Role_".substr($entry, 0, -4);
  223. // List of roles
  224. if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'][$class])) {
  225. $file = "$dir/$entry";
  226. $parser->parse(file_get_contents($file));
  227. $data = $parser->getData();
  228. if (!is_array($data['releasetypes'])) {
  229. $data['releasetypes'] = array($data['releasetypes']);
  230. }
  231. $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data;
  232. }
  233. }
  234. closedir($dp);
  235. ksort($GLOBALS['_PEAR_INSTALLER_ROLES']);
  236. PEAR_Installer_Role::getBaseinstallRoles(true);
  237. PEAR_Installer_Role::getInstallableRoles(true);
  238. PEAR_Installer_Role::getPhpRoles(true);
  239. PEAR_Installer_Role::getValidRoles('****', true);
  240. return true;
  241. }
  242. }