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 | /** |
---|
18 | * base class for installer roles |
---|
19 | */ |
---|
20 | require_once 'PEAR/Installer/Role/Common.php'; |
---|
21 | require_once 'PEAR/XMLParser.php'; |
---|
22 | /** |
---|
23 | * @category pear |
---|
24 | * @package PEAR |
---|
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 |
---|
31 | */ |
---|
32 | class PEAR_Installer_Role |
---|
33 | { |
---|
34 | /** |
---|
35 | * Set up any additional configuration variables that file roles require |
---|
36 | * |
---|
37 | * Never call this directly, it is called by the PEAR_Config constructor |
---|
38 | * @param PEAR_Config |
---|
39 | * @access private |
---|
40 | * @static |
---|
41 | */ |
---|
42 | function initializeConfig(&$config) |
---|
43 | { |
---|
44 | if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) { |
---|
45 | PEAR_Installer_Role::registerRoles(); |
---|
46 | } |
---|
47 | |
---|
48 | foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $class => $info) { |
---|
49 | if (!$info['config_vars']) { |
---|
50 | continue; |
---|
51 | } |
---|
52 | |
---|
53 | $config->_addConfigVars($class, $info['config_vars']); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * @param PEAR_PackageFile_v2 |
---|
59 | * @param string role name |
---|
60 | * @param PEAR_Config |
---|
61 | * @return PEAR_Installer_Role_Common |
---|
62 | * @static |
---|
63 | */ |
---|
64 | function &factory($pkg, $role, &$config) |
---|
65 | { |
---|
66 | if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) { |
---|
67 | PEAR_Installer_Role::registerRoles(); |
---|
68 | } |
---|
69 | |
---|
70 | if (!in_array($role, PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) { |
---|
71 | $a = false; |
---|
72 | return $a; |
---|
73 | } |
---|
74 | |
---|
75 | $a = 'PEAR_Installer_Role_' . ucfirst($role); |
---|
76 | if (!class_exists($a)) { |
---|
77 | require_once str_replace('_', '/', $a) . '.php'; |
---|
78 | } |
---|
79 | |
---|
80 | $b = new $a($config); |
---|
81 | return $b; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Get a list of file roles that are valid for the particular release type. |
---|
86 | * |
---|
87 | * For instance, src files serve no purpose in regular php releases. |
---|
88 | * @param string |
---|
89 | * @param bool clear cache |
---|
90 | * @return array |
---|
91 | * @static |
---|
92 | */ |
---|
93 | function getValidRoles($release, $clear = false) |
---|
94 | { |
---|
95 | if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) { |
---|
96 | PEAR_Installer_Role::registerRoles(); |
---|
97 | } |
---|
98 | |
---|
99 | static $ret = array(); |
---|
100 | if ($clear) { |
---|
101 | $ret = array(); |
---|
102 | } |
---|
103 | |
---|
104 | if (isset($ret[$release])) { |
---|
105 | return $ret[$release]; |
---|
106 | } |
---|
107 | |
---|
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)); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | return $ret[$release]; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Get a list of roles that require their files to be installed |
---|
120 | * |
---|
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 |
---|
125 | * @return array |
---|
126 | * @static |
---|
127 | */ |
---|
128 | function getInstallableRoles($clear = false) |
---|
129 | { |
---|
130 | if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) { |
---|
131 | PEAR_Installer_Role::registerRoles(); |
---|
132 | } |
---|
133 | |
---|
134 | static $ret; |
---|
135 | if ($clear) { |
---|
136 | unset($ret); |
---|
137 | } |
---|
138 | |
---|
139 | if (isset($ret)) { |
---|
140 | return $ret; |
---|
141 | } |
---|
142 | |
---|
143 | $ret = array(); |
---|
144 | foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) { |
---|
145 | if ($okreleases['installable']) { |
---|
146 | $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role)); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | return $ret; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Return an array of roles that are affected by the baseinstalldir attribute |
---|
155 | * |
---|
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 |
---|
160 | * @return array |
---|
161 | * @static |
---|
162 | */ |
---|
163 | function getBaseinstallRoles($clear = false) |
---|
164 | { |
---|
165 | if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) { |
---|
166 | PEAR_Installer_Role::registerRoles(); |
---|
167 | } |
---|
168 | |
---|
169 | static $ret; |
---|
170 | if ($clear) { |
---|
171 | unset($ret); |
---|
172 | } |
---|
173 | |
---|
174 | if (isset($ret)) { |
---|
175 | return $ret; |
---|
176 | } |
---|
177 | |
---|
178 | $ret = array(); |
---|
179 | foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) { |
---|
180 | if ($okreleases['honorsbaseinstall']) { |
---|
181 | $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role)); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | return $ret; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
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 |
---|
192 | * @return array |
---|
193 | * @static |
---|
194 | */ |
---|
195 | function getPhpRoles($clear = false) |
---|
196 | { |
---|
197 | if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'])) { |
---|
198 | PEAR_Installer_Role::registerRoles(); |
---|
199 | } |
---|
200 | |
---|
201 | static $ret; |
---|
202 | if ($clear) { |
---|
203 | unset($ret); |
---|
204 | } |
---|
205 | |
---|
206 | if (isset($ret)) { |
---|
207 | return $ret; |
---|
208 | } |
---|
209 | |
---|
210 | $ret = array(); |
---|
211 | foreach ($GLOBALS['_PEAR_INSTALLER_ROLES'] as $role => $okreleases) { |
---|
212 | if ($okreleases['phpfile']) { |
---|
213 | $ret[] = strtolower(str_replace('PEAR_Installer_Role_', '', $role)); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | return $ret; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
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 |
---|
226 | * included. |
---|
227 | * |
---|
228 | * @return bool TRUE on success, a PEAR error on failure |
---|
229 | * @access public |
---|
230 | * @static |
---|
231 | */ |
---|
232 | function registerRoles($dir = null) |
---|
233 | { |
---|
234 | $GLOBALS['_PEAR_INSTALLER_ROLES'] = array(); |
---|
235 | $parser = new PEAR_XMLParser; |
---|
236 | if ($dir === null) { |
---|
237 | $dir = dirname(__FILE__) . '/Role'; |
---|
238 | } |
---|
239 | |
---|
240 | if (!file_exists($dir) || !is_dir($dir)) { |
---|
241 | return PEAR::raiseError("registerRoles: opendir($dir) failed: does not exist/is not directory"); |
---|
242 | } |
---|
243 | |
---|
244 | $dp = @opendir($dir); |
---|
245 | if (empty($dp)) { |
---|
246 | return PEAR::raiseError("registerRoles: opendir($dir) failed: $php_errmsg"); |
---|
247 | } |
---|
248 | |
---|
249 | while ($entry = readdir($dp)) { |
---|
250 | if ($entry{0} == '.' || substr($entry, -4) != '.xml') { |
---|
251 | continue; |
---|
252 | } |
---|
253 | |
---|
254 | $class = "PEAR_Installer_Role_".substr($entry, 0, -4); |
---|
255 | // List of roles |
---|
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']); |
---|
262 | } |
---|
263 | |
---|
264 | $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data; |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | closedir($dp); |
---|
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); |
---|
274 | return true; |
---|
275 | } |
---|
276 | } |
---|