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 * @link http://pear.php.net/package/PEAR
13 * @since File available since Release 1.4.0a1
18 require_once 'PEAR/Task/Common.php';
20 * Implements the replace file task.
23 * @author Greg Beaver <cellog@php.net>
24 * @copyright 1997-2009 The Authors
25 * @license http://opensource.org/licenses/bsd-license.php New BSD License
26 * @version Release: 1.10.1
27 * @link http://pear.php.net/package/PEAR
28 * @since Class available since Release 1.4.0a1
30 class PEAR_Task_Replace extends PEAR_Task_Common
32 public $type = 'simple';
33 public $phase = PEAR_TASK_PACKAGEANDINSTALL;
34 public $_replacements;
37 * Validate the raw xml at parsing-time.
39 * @param PEAR_PackageFile_v2
40 * @param array raw, parsed xml
43 public static function validateXml($pkg, $xml, $config, $fileXml)
45 if (!isset($xml['attribs'])) {
46 return array(PEAR_TASK_ERROR_NOATTRIBS);
48 if (!isset($xml['attribs']['type'])) {
49 return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type');
51 if (!isset($xml['attribs']['to'])) {
52 return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to');
54 if (!isset($xml['attribs']['from'])) {
55 return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from');
57 if ($xml['attribs']['type'] == 'pear-config') {
58 if (!in_array($xml['attribs']['to'], $config->getKeys())) {
59 return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
60 $config->getKeys(), );
62 } elseif ($xml['attribs']['type'] == 'php-const') {
63 if (defined($xml['attribs']['to'])) {
66 return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
67 array('valid PHP constant'), );
69 } elseif ($xml['attribs']['type'] == 'package-info') {
71 $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',
79 return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
80 array('name', 'summary', 'channel', 'notes', 'extends', 'description',
81 'release_notes', 'license', 'release-license', 'license-uri',
82 'version', 'api-version', 'state', 'api-state', 'release_date',
86 return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'],
87 array('pear-config', 'package-info', 'php-const'), );
94 * Initialize a task instance with the parameters
95 * @param array raw, parsed xml
99 public function init($xml, $attribs, $lastVersion = null)
101 $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml;
105 * Do a package.xml 1.0 replacement, with additional package-info fields available
107 * See validateXml() source for the complete list of allowed fields
109 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
110 * @param string file contents
111 * @param string the eventual final file location (informational only)
112 * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
113 * (use $this->throwError), otherwise return the new contents
115 public function startSession($pkg, $contents, $dest)
117 $subst_from = $subst_to = array();
118 foreach ($this->_replacements as $a) {
121 if ($a['type'] == 'pear-config') {
122 if ($this->installphase == PEAR_TASK_PACKAGE) {
125 if ($a['to'] == 'master_server') {
126 $chan = $this->registry->getChannel($pkg->getChannel());
127 if (!PEAR::isError($chan)) {
128 $to = $chan->getServer();
130 $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
135 if ($this->config->isDefinedLayer('ftp')) {
136 // try the remote config file first
137 $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel());
139 // then default to local
140 $to = $this->config->get($a['to'], null, $pkg->getChannel());
143 $to = $this->config->get($a['to'], null, $pkg->getChannel());
147 $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
151 } elseif ($a['type'] == 'php-const') {
152 if ($this->installphase == PEAR_TASK_PACKAGE) {
155 if (defined($a['to'])) {
156 $to = constant($a['to']);
158 $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]");
163 if ($t = $pkg->packageInfo($a['to'])) {
166 $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]");
172 $subst_from[] = $a['from'];
177 3, "doing ".sizeof($subst_from).
178 " substitution(s) for $dest"
180 if (sizeof($subst_from)) {
181 $contents = str_replace($subst_from, $subst_to, $contents);