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