PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PEAR/Installer/Role.php

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