<?php
 require_once 'PEAR/Task/Common.php'; class PEAR_Task_Replace extends PEAR_Task_Common { var $type = 'simple'; var $phase = PEAR_TASK_PACKAGEANDINSTALL; var $_replacements; function validateXml($pkg, $xml, $config, $fileXml) { if (!isset($xml['attribs'])) { return array(PEAR_TASK_ERROR_NOATTRIBS); } if (!isset($xml['attribs']['type'])) { return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type'); } if (!isset($xml['attribs']['to'])) { return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to'); } if (!isset($xml['attribs']['from'])) { return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from'); } if ($xml['attribs']['type'] == 'pear-config') { if (!in_array($xml['attribs']['to'], $config->getKeys())) { return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], $config->getKeys()); } } elseif ($xml['attribs']['type'] == 'php-const') { if (defined($xml['attribs']['to'])) { return true; } else { return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], array('valid PHP constant')); } } elseif ($xml['attribs']['type'] == 'package-info') { if (in_array($xml['attribs']['to'], array('name', 'summary', 'channel', 'notes', 'extends', 'description', 'release_notes', 'license', 'release-license', 'license-uri', 'version', 'api-version', 'state', 'api-state', 'release_date', 'date', 'time'))) { return true; } else { return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'], array('name', 'summary', 'channel', 'notes', 'extends', 'description', 'release_notes', 'license', 'release-license', 'license-uri', 'version', 'api-version', 'state', 'api-state', 'release_date', 'date', 'time')); } } else { return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'], array('pear-config', 'package-info', 'php-const')); } return true; } function init($xml, $attribs) { $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml; } function startSession($pkg, $contents, $dest) { $subst_from = $subst_to = array(); foreach ($this->_replacements as $a) { $a = $a['attribs']; $to = ''; if ($a['type'] == 'pear-config') { if ($this->installphase == PEAR_TASK_PACKAGE) { return false; } if ($a['to'] == 'master_server') { $chan = $this->registry->getChannel($pkg->getChannel()); if (!PEAR::isError($chan)) { $to = $chan->getServer(); } else { $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]"); return false; } } else { if ($this->config->isDefinedLayer('ftp')) { $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel()); if (is_null($to)) { $to = $this->config->get($a['to'], null, $pkg->getChannel()); } } else { $to = $this->config->get($a['to'], null, $pkg->getChannel()); } } if (is_null($to)) { $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]"); return false; } } elseif ($a['type'] == 'php-const') { if ($this->installphase == PEAR_TASK_PACKAGE) { return false; } if (defined($a['to'])) { $to = constant($a['to']); } else { $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]"); return false; } } else { if ($t = $pkg->packageInfo($a['to'])) { $to = $t; } else { $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]"); return false; } } if (!is_null($to)) { $subst_from[] = $a['from']; $subst_to[] = $to; } } $this->logger->log(3, "doing " . sizeof($subst_from) . " substitution(s) for $dest"); if (sizeof($subst_from)) { $contents = str_replace($subst_from, $subst_to, $contents); } return $contents; } } ?>