Редакция 69 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 69 | Редакция 94 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * <tasks:replace>
|
3 | * <tasks:replace>
|
4 | *
|
4 | *
|
5 | * PHP versions 4 and 5
|
5 | * PHP versions 4 and 5
|
6 | *
|
6 | *
|
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license
|
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license
|
8 | * that is available through the world-wide-web at the following URI:
|
8 | * that is available through the world-wide-web at the following URI:
|
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
10 | * the PHP License and are unable to obtain it through the web, please
|
10 | * the PHP License and are unable to obtain it through the web, please
|
11 | * send a note to license@php.net so we can mail you a copy immediately.
|
11 | * send a note to license@php.net so we can mail you a copy immediately.
|
12 | *
|
12 | *
|
13 | * @category pear
|
13 | * @category pear
|
14 | * @package PEAR
|
14 | * @package PEAR
|
15 | * @author Greg Beaver <cellog@php.net>
|
15 | * @author Greg Beaver <cellog@php.net>
|
16 | * @copyright 1997-2008 The PHP Group
|
16 | * @copyright 1997-2008 The PHP Group
|
17 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
17 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
18 | * @version CVS: $Id: Replace.php,v 1.17 2008/05/13 21:28:20 cellog Exp $
|
18 | * @version CVS: $Id: Replace.php,v 1.17 2008/05/13 21:28:20 cellog Exp $
|
19 | * @link http://pear.php.net/package/PEAR
|
19 | * @link http://pear.php.net/package/PEAR
|
20 | * @since File available since Release 1.4.0a1
|
20 | * @since File available since Release 1.4.0a1
|
21 | */
|
21 | */
|
22 | /**
|
22 | /**
|
23 | * Base class
|
23 | * Base class
|
24 | */
|
24 | */
|
25 | require_once 'PEAR/Task/Common.php'; |
25 | require_once 'PEAR/Task/Common.php'; |
26 | /**
|
26 | /**
|
27 | * Implements the replace file task.
|
27 | * Implements the replace file task.
|
28 | * @category pear
|
28 | * @category pear
|
29 | * @package PEAR
|
29 | * @package PEAR
|
30 | * @author Greg Beaver <cellog@php.net>
|
30 | * @author Greg Beaver <cellog@php.net>
|
31 | * @copyright 1997-2008 The PHP Group
|
31 | * @copyright 1997-2008 The PHP Group
|
32 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
32 | * @license http://www.php.net/license/3_0.txt PHP License 3.0
|
33 | * @version Release: 1.7.2
|
33 | * @version Release: 1.7.2
|
34 | * @link http://pear.php.net/package/PEAR
|
34 | * @link http://pear.php.net/package/PEAR
|
35 | * @since Class available since Release 1.4.0a1
|
35 | * @since Class available since Release 1.4.0a1
|
36 | */
|
36 | */
|
37 | class PEAR_Task_Replace extends PEAR_Task_Common |
37 | class PEAR_Task_Replace extends PEAR_Task_Common |
38 | {
|
38 | {
|
39 | var $type = 'simple'; |
39 | var $type = 'simple'; |
40 | var $phase = PEAR_TASK_PACKAGEANDINSTALL; |
40 | var $phase = PEAR_TASK_PACKAGEANDINSTALL; |
41 | var $_replacements; |
41 | var $_replacements; |
42 | 42 | ||
43 | /**
|
43 | /**
|
44 | * Validate the raw xml at parsing-time.
|
44 | * Validate the raw xml at parsing-time.
|
45 | * @param PEAR_PackageFile_v2
|
45 | * @param PEAR_PackageFile_v2
|
46 | * @param array raw, parsed xml
|
46 | * @param array raw, parsed xml
|
47 | * @param PEAR_Config
|
47 | * @param PEAR_Config
|
48 | * @static
|
48 | * @static
|
49 | */
|
49 | */
|
50 | function validateXml($pkg, $xml, $config, $fileXml) |
50 | function validateXml($pkg, $xml, $config, $fileXml) |
51 | {
|
51 | {
|
52 | if (!isset($xml['attribs'])) { |
52 | if (!isset($xml['attribs'])) { |
53 | return array(PEAR_TASK_ERROR_NOATTRIBS); |
53 | return array(PEAR_TASK_ERROR_NOATTRIBS); |
54 | }
|
54 | }
|
55 | if (!isset($xml['attribs']['type'])) { |
55 | if (!isset($xml['attribs']['type'])) { |
56 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type'); |
56 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type'); |
57 | }
|
57 | }
|
58 | if (!isset($xml['attribs']['to'])) { |
58 | if (!isset($xml['attribs']['to'])) { |
59 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to'); |
59 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to'); |
60 | }
|
60 | }
|
61 | if (!isset($xml['attribs']['from'])) { |
61 | if (!isset($xml['attribs']['from'])) { |
62 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from'); |
62 | return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from'); |
63 | }
|
63 | }
|
64 | if ($xml['attribs']['type'] == 'pear-config') { |
64 | if ($xml['attribs']['type'] == 'pear-config') { |
65 | if (!in_array($xml['attribs']['to'], $config->getKeys())) { |
65 | if (!in_array($xml['attribs']['to'], $config->getKeys())) { |
66 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
66 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
67 | $config->getKeys()); |
67 | $config->getKeys()); |
68 | }
|
68 | }
|
69 | } elseif ($xml['attribs']['type'] == 'php-const') { |
69 | } elseif ($xml['attribs']['type'] == 'php-const') { |
70 | if (defined($xml['attribs']['to'])) { |
70 | if (defined($xml['attribs']['to'])) { |
71 | return true; |
71 | return true; |
72 | } else { |
72 | } else { |
73 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
73 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
74 | array('valid PHP constant')); |
74 | array('valid PHP constant')); |
75 | }
|
75 | }
|
76 | } elseif ($xml['attribs']['type'] == 'package-info') { |
76 | } elseif ($xml['attribs']['type'] == 'package-info') { |
77 | if (in_array($xml['attribs']['to'], |
77 | if (in_array($xml['attribs']['to'], |
78 | array('name', 'summary', 'channel', 'notes', 'extends', 'description', |
78 | array('name', 'summary', 'channel', 'notes', 'extends', 'description', |
79 | 'release_notes', 'license', 'release-license', 'license-uri', |
79 | 'release_notes', 'license', 'release-license', 'license-uri', |
80 | 'version', 'api-version', 'state', 'api-state', 'release_date', |
80 | 'version', 'api-version', 'state', 'api-state', 'release_date', |
81 | 'date', 'time'))) { |
81 | 'date', 'time'))) { |
82 | return true; |
82 | return true; |
83 | } else { |
83 | } else { |
84 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
84 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], |
85 | array('name', 'summary', 'channel', 'notes', 'extends', 'description', |
85 | array('name', 'summary', 'channel', 'notes', 'extends', 'description', |
86 | 'release_notes', 'license', 'release-license', 'license-uri', |
86 | 'release_notes', 'license', 'release-license', 'license-uri', |
87 | 'version', 'api-version', 'state', 'api-state', 'release_date', |
87 | 'version', 'api-version', 'state', 'api-state', 'release_date', |
88 | 'date', 'time')); |
88 | 'date', 'time')); |
89 | }
|
89 | }
|
90 | } else { |
90 | } else { |
91 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'], |
91 | return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'], |
92 | array('pear-config', 'package-info', 'php-const')); |
92 | array('pear-config', 'package-info', 'php-const')); |
93 | }
|
93 | }
|
94 | return true; |
94 | return true; |
95 | }
|
95 | }
|
96 | 96 | ||
97 | /**
|
97 | /**
|
98 | * Initialize a task instance with the parameters
|
98 | * Initialize a task instance with the parameters
|
99 | * @param array raw, parsed xml
|
99 | * @param array raw, parsed xml
|
100 | * @param unused
|
100 | * @param unused
|
101 | */
|
101 | */
|
102 | function init($xml, $attribs) |
102 | function init($xml, $attribs) |
103 | {
|
103 | {
|
104 | $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml; |
104 | $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml; |
105 | }
|
105 | }
|
106 | 106 | ||
107 | /**
|
107 | /**
|
108 | * Do a package.xml 1.0 replacement, with additional package-info fields available
|
108 | * Do a package.xml 1.0 replacement, with additional package-info fields available
|
109 | *
|
109 | *
|
110 | * See validateXml() source for the complete list of allowed fields
|
110 | * See validateXml() source for the complete list of allowed fields
|
111 | * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
|
111 | * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
|
112 | * @param string file contents
|
112 | * @param string file contents
|
113 | * @param string the eventual final file location (informational only)
|
113 | * @param string the eventual final file location (informational only)
|
114 | * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
|
114 | * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
|
115 | * (use $this->throwError), otherwise return the new contents
|
115 | * (use $this->throwError), otherwise return the new contents
|
116 | */
|
116 | */
|
117 | function startSession($pkg, $contents, $dest) |
117 | function startSession($pkg, $contents, $dest) |
118 | {
|
118 | {
|
119 | $subst_from = $subst_to = array(); |
119 | $subst_from = $subst_to = array(); |
120 | foreach ($this->_replacements as $a) { |
120 | foreach ($this->_replacements as $a) { |
121 | $a = $a['attribs']; |
121 | $a = $a['attribs']; |
122 | $to = ''; |
122 | $to = ''; |
123 | if ($a['type'] == 'pear-config') { |
123 | if ($a['type'] == 'pear-config') { |
124 | if ($this->installphase == PEAR_TASK_PACKAGE) { |
124 | if ($this->installphase == PEAR_TASK_PACKAGE) { |
125 | return false; |
125 | return false; |
126 | }
|
126 | }
|
127 | if ($a['to'] == 'master_server') { |
127 | if ($a['to'] == 'master_server') { |
128 | $chan = $this->registry->getChannel($pkg->getChannel()); |
128 | $chan = $this->registry->getChannel($pkg->getChannel()); |
129 | if (!PEAR::isError($chan)) { |
129 | if (!PEAR::isError($chan)) { |
130 | $to = $chan->getServer(); |
130 | $to = $chan->getServer(); |
131 | } else { |
131 | } else { |
132 | $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]"); |
132 | $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]"); |
133 | return false; |
133 | return false; |
134 | }
|
134 | }
|
135 | } else { |
135 | } else { |
136 | if ($this->config->isDefinedLayer('ftp')) { |
136 | if ($this->config->isDefinedLayer('ftp')) { |
137 | // try the remote config file first
|
137 | // try the remote config file first
|
138 | $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel()); |
138 | $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel()); |
139 | if (is_null($to)) { |
139 | if (is_null($to)) { |
140 | // then default to local
|
140 | // then default to local
|
141 | $to = $this->config->get($a['to'], null, $pkg->getChannel()); |
141 | $to = $this->config->get($a['to'], null, $pkg->getChannel()); |
142 | }
|
142 | }
|
143 | } else { |
143 | } else { |
144 | $to = $this->config->get($a['to'], null, $pkg->getChannel()); |
144 | $to = $this->config->get($a['to'], null, $pkg->getChannel()); |
145 | }
|
145 | }
|
146 | }
|
146 | }
|
147 | if (is_null($to)) { |
147 | if (is_null($to)) { |
148 | $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]"); |
148 | $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]"); |
149 | return false; |
149 | return false; |
150 | }
|
150 | }
|
151 | } elseif ($a['type'] == 'php-const') { |
151 | } elseif ($a['type'] == 'php-const') { |
152 | if ($this->installphase == PEAR_TASK_PACKAGE) { |
152 | if ($this->installphase == PEAR_TASK_PACKAGE) { |
153 | return false; |
153 | return false; |
154 | }
|
154 | }
|
155 | if (defined($a['to'])) { |
155 | if (defined($a['to'])) { |
156 | $to = constant($a['to']); |
156 | $to = constant($a['to']); |
157 | } else { |
157 | } else { |
158 | $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]"); |
158 | $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]"); |
159 | return false; |
159 | return false; |
160 | }
|
160 | }
|
161 | } else { |
161 | } else { |
162 | if ($t = $pkg->packageInfo($a['to'])) { |
162 | if ($t = $pkg->packageInfo($a['to'])) { |
163 | $to = $t; |
163 | $to = $t; |
164 | } else { |
164 | } else { |
165 | $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]"); |
165 | $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]"); |
166 | return false; |
166 | return false; |
167 | }
|
167 | }
|
168 | }
|
168 | }
|
169 | if (!is_null($to)) { |
169 | if (!is_null($to)) { |
170 | $subst_from[] = $a['from']; |
170 | $subst_from[] = $a['from']; |
171 | $subst_to[] = $to; |
171 | $subst_to[] = $to; |
172 | }
|
172 | }
|
173 | }
|
173 | }
|
174 | $this->logger->log(3, "doing " . sizeof($subst_from) . |
174 | $this->logger->log(3, "doing " . sizeof($subst_from) . |
175 | " substitution(s) for $dest"); |
175 | " substitution(s) for $dest"); |
176 | if (sizeof($subst_from)) { |
176 | if (sizeof($subst_from)) { |
177 | $contents = str_replace($subst_from, $subst_to, $contents); |
177 | $contents = str_replace($subst_from, $subst_to, $contents); |
178 | }
|
178 | }
|
179 | return $contents; |
179 | return $contents; |
180 | }
|
180 | }
|
181 | }
|
181 | }
|
182 | ?>
|
182 | ?>
|