1 | <?php |
---|
2 | /** |
---|
3 | * <tasks:replace> |
---|
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: Replace.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 |
---|
18 | */ |
---|
19 | require_once 'PEAR/Task/Common.php'; |
---|
20 | /** |
---|
21 | * Implements the replace file task. |
---|
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_Task_Replace extends PEAR_Task_Common |
---|
32 | { |
---|
33 | var $type = 'simple'; |
---|
34 | var $phase = PEAR_TASK_PACKAGEANDINSTALL; |
---|
35 | var $_replacements; |
---|
36 | |
---|
37 | /** |
---|
38 | * Validate the raw xml at parsing-time. |
---|
39 | * @param PEAR_PackageFile_v2 |
---|
40 | * @param array raw, parsed xml |
---|
41 | * @param PEAR_Config |
---|
42 | * @static |
---|
43 | */ |
---|
44 | function validateXml($pkg, $xml, $config, $fileXml) |
---|
45 | { |
---|
46 | if (!isset($xml['attribs'])) { |
---|
47 | return array(PEAR_TASK_ERROR_NOATTRIBS); |
---|
48 | } |
---|
49 | if (!isset($xml['attribs']['type'])) { |
---|
50 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type'); |
---|
51 | } |
---|
52 | if (!isset($xml['attribs']['to'])) { |
---|
53 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to'); |
---|
54 | } |
---|
55 | if (!isset($xml['attribs']['from'])) { |
---|
56 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from'); |
---|
57 | } |
---|
58 | if ($xml['attribs']['type'] == 'pear-config') { |
---|
59 | if (!in_array($xml['attribs']['to'], $config->getKeys())) { |
---|
60 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
---|
61 | $config->getKeys()); |
---|
62 | } |
---|
63 | } elseif ($xml['attribs']['type'] == 'php-const') { |
---|
64 | if (defined($xml['attribs']['to'])) { |
---|
65 | return true; |
---|
66 | } else { |
---|
67 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
---|
68 | array('valid PHP constant')); |
---|
69 | } |
---|
70 | } elseif ($xml['attribs']['type'] == 'package-info') { |
---|
71 | if (in_array($xml['attribs']['to'], |
---|
72 | array('name', 'summary', 'channel', 'notes', 'extends', 'description', |
---|
73 | 'release_notes', 'license', 'release-license', 'license-uri', |
---|
74 | 'version', 'api-version', 'state', 'api-state', 'release_date', |
---|
75 | 'date', 'time'))) { |
---|
76 | return true; |
---|
77 | } else { |
---|
78 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
---|
79 | array('name', 'summary', 'channel', 'notes', 'extends', 'description', |
---|
80 | 'release_notes', 'license', 'release-license', 'license-uri', |
---|
81 | 'version', 'api-version', 'state', 'api-state', 'release_date', |
---|
82 | 'date', 'time')); |
---|
83 | } |
---|
84 | } else { |
---|
85 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'], |
---|
86 | array('pear-config', 'package-info', 'php-const')); |
---|
87 | } |
---|
88 | return true; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Initialize a task instance with the parameters |
---|
93 | * @param array raw, parsed xml |
---|
94 | * @param unused |
---|
95 | */ |
---|
96 | function init($xml, $attribs) |
---|
97 | { |
---|
98 | $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Do a package.xml 1.0 replacement, with additional package-info fields available |
---|
103 | * |
---|
104 | * See validateXml() source for the complete list of allowed fields |
---|
105 | * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2 |
---|
106 | * @param string file contents |
---|
107 | * @param string the eventual final file location (informational only) |
---|
108 | * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail |
---|
109 | * (use $this->throwError), otherwise return the new contents |
---|
110 | */ |
---|
111 | function startSession($pkg, $contents, $dest) |
---|
112 | { |
---|
113 | $subst_from = $subst_to = array(); |
---|
114 | foreach ($this->_replacements as $a) { |
---|
115 | $a = $a['attribs']; |
---|
116 | $to = ''; |
---|
117 | if ($a['type'] == 'pear-config') { |
---|
118 | if ($this->installphase == PEAR_TASK_PACKAGE) { |
---|
119 | return false; |
---|
120 | } |
---|
121 | if ($a['to'] == 'master_server') { |
---|
122 | $chan = $this->registry->getChannel($pkg->getChannel()); |
---|
123 | if (!PEAR::isError($chan)) { |
---|
124 | $to = $chan->getServer(); |
---|
125 | } else { |
---|
126 | $this->logger->log(0, "$dest: invalid pear-config replacement: {$a['to']}"); |
---|
127 | return false; |
---|
128 | } |
---|
129 | } else { |
---|
130 | if ($this->config->isDefinedLayer('ftp')) { |
---|
131 | // try the remote config file first |
---|
132 | $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel()); |
---|
133 | if (is_null($to)) { |
---|
134 | // then default to local |
---|
135 | $to = $this->config->get($a['to'], null, $pkg->getChannel()); |
---|
136 | } |
---|
137 | } else { |
---|
138 | $to = $this->config->get($a['to'], null, $pkg->getChannel()); |
---|
139 | } |
---|
140 | } |
---|
141 | if (is_null($to)) { |
---|
142 | $this->logger->log(0, "$dest: invalid pear-config replacement: {$a['to']}"); |
---|
143 | return false; |
---|
144 | } |
---|
145 | } elseif ($a['type'] == 'php-const') { |
---|
146 | if ($this->installphase == PEAR_TASK_PACKAGE) { |
---|
147 | return false; |
---|
148 | } |
---|
149 | if (defined($a['to'])) { |
---|
150 | $to = constant($a['to']); |
---|
151 | } else { |
---|
152 | $this->logger->log(0, "$dest: invalid php-const replacement: {$a['to']}"); |
---|
153 | return false; |
---|
154 | } |
---|
155 | } else { |
---|
156 | if ($t = $pkg->packageInfo($a['to'])) { |
---|
157 | $to = $t; |
---|
158 | } else { |
---|
159 | $this->logger->log(0, "$dest: invalid package-info replacement: {$a['to']}"); |
---|
160 | return false; |
---|
161 | } |
---|
162 | } |
---|
163 | if (!is_null($to)) { |
---|
164 | $subst_from[] = $a['from']; |
---|
165 | $subst_to[] = $to; |
---|
166 | } |
---|
167 | } |
---|
168 | $this->logger->log(3, "doing " . sizeof($subst_from) . |
---|
169 | " substitution(s) for $dest"); |
---|
170 | if (sizeof($subst_from)) { |
---|
171 | $contents = str_replace($subst_from, $subst_to, $contents); |
---|
172 | } |
---|
173 | return $contents; |
---|
174 | } |
---|
175 | } |
---|
176 | ?> |
---|