PageRenderTime 73ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/php52/lib/php/PEAR/Installer/Role.php

https://github.com/ktmud/MNPP
PHP | 276 lines | 151 code | 34 blank | 91 comment | 32 complexity | 3dba76d8cad0ee633749b215fa2f7bbb MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause
  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 278552 2009-04-10 19:42:49Z 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.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. * @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. }