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
19 require_once 'PEAR/Task/Common.php';
21 * Implements the replace file task.
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
31 class PEAR_Task_Replace extends PEAR_Task_Common
34 var $phase = PEAR_TASK_PACKAGEANDINSTALL;
38 * Validate the raw xml at parsing-time.
39 * @param PEAR_PackageFile_v2
40 * @param array raw, parsed xml
44 function validateXml($pkg, $xml, $config, $fileXml)
46 if (!isset($xml['attribs'])) {
47 return array(PEAR_TASK_ERROR_NOATTRIBS);
49 if (!isset($xml['attribs']['type'])) {
50 return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type');
52 if (!isset($xml['attribs']['to'])) {
53 return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to');
55 if (!isset($xml['attribs']['from'])) {
56 return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from');
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'],
63 } elseif ($xml['attribs']['type'] == 'php-const') {
64 if (defined($xml['attribs']['to'])) {
67 return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
68 array('valid PHP constant'));
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',
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',
85 return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'],
86 array('pear-config', 'package-info', 'php-const'));
92 * Initialize a task instance with the parameters
93 * @param array raw, parsed xml
96 function init($xml, $attribs)
98 $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml;
102 * Do a package.xml 1.0 replacement, with additional package-info fields available
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
111 function startSession($pkg, $contents, $dest)
113 $subst_from = $subst_to = array();
114 foreach ($this->_replacements as $a) {
117 if ($a['type'] == 'pear-config') {
118 if ($this->installphase == PEAR_TASK_PACKAGE) {
121 if ($a['to'] == 'master_server') {
122 $chan = $this->registry->getChannel($pkg->getChannel());
123 if (!PEAR::isError($chan)) {
124 $to = $chan->getServer();
126 $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
130 if ($this->config->isDefinedLayer('ftp')) {
131 // try the remote config file first
132 $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel());
134 // then default to local
135 $to = $this->config->get($a['to'], null, $pkg->getChannel());
138 $to = $this->config->get($a['to'], null, $pkg->getChannel());
142 $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
145 } elseif ($a['type'] == 'php-const') {
146 if ($this->installphase == PEAR_TASK_PACKAGE) {
149 if (defined($a['to'])) {
150 $to = constant($a['to']);
152 $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]");
156 if ($t = $pkg->packageInfo($a['to'])) {
159 $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]");
164 $subst_from[] = $a['from'];
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);