Replace.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * <tasks:replace>
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  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
  15. */
  16. /**
  17. * Base class
  18. */
  19. require_once 'PEAR/Task/Common.php';
  20. /**
  21. * Implements the replace file task.
  22. * @category pear
  23. * @package PEAR
  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
  30. */
  31. class PEAR_Task_Replace extends PEAR_Task_Common
  32. {
  33. var $type = 'simple';
  34. var $phase = PEAR_TASK_PACKAGEANDINSTALL;
  35. var $_replacements;
  36. /**
  37. * Validate the raw xml at parsing-time.
  38. * @param PEAR_PackageFile_v2
  39. * @param array raw, parsed xml
  40. * @param PEAR_Config
  41. * @static
  42. */
  43. function validateXml($pkg, $xml, $config, $fileXml)
  44. {
  45. if (!isset($xml['attribs'])) {
  46. return array(PEAR_TASK_ERROR_NOATTRIBS);
  47. }
  48. if (!isset($xml['attribs']['type'])) {
  49. return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'type');
  50. }
  51. if (!isset($xml['attribs']['to'])) {
  52. return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'to');
  53. }
  54. if (!isset($xml['attribs']['from'])) {
  55. return array(PEAR_TASK_ERROR_MISSING_ATTRIB, 'from');
  56. }
  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());
  61. }
  62. } elseif ($xml['attribs']['type'] == 'php-const') {
  63. if (defined($xml['attribs']['to'])) {
  64. return true;
  65. } else {
  66. return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
  67. array('valid PHP constant'));
  68. }
  69. } elseif ($xml['attribs']['type'] == 'package-info') {
  70. if (in_array($xml['attribs']['to'],
  71. array('name', 'summary', 'channel', 'notes', 'extends', 'description',
  72. 'release_notes', 'license', 'release-license', 'license-uri',
  73. 'version', 'api-version', 'state', 'api-state', 'release_date',
  74. 'date', 'time'))) {
  75. return true;
  76. } else {
  77. return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $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. }
  83. } else {
  84. return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'],
  85. array('pear-config', 'package-info', 'php-const'));
  86. }
  87. return true;
  88. }
  89. /**
  90. * Initialize a task instance with the parameters
  91. * @param array raw, parsed xml
  92. * @param unused
  93. */
  94. function init($xml, $attribs)
  95. {
  96. $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml;
  97. }
  98. /**
  99. * Do a package.xml 1.0 replacement, with additional package-info fields available
  100. *
  101. * See validateXml() source for the complete list of allowed fields
  102. * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
  103. * @param string file contents
  104. * @param string the eventual final file location (informational only)
  105. * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
  106. * (use $this->throwError), otherwise return the new contents
  107. */
  108. function startSession($pkg, $contents, $dest)
  109. {
  110. $subst_from = $subst_to = array();
  111. foreach ($this->_replacements as $a) {
  112. $a = $a['attribs'];
  113. $to = '';
  114. if ($a['type'] == 'pear-config') {
  115. if ($this->installphase == PEAR_TASK_PACKAGE) {
  116. return false;
  117. }
  118. if ($a['to'] == 'master_server') {
  119. $chan = $this->registry->getChannel($pkg->getChannel());
  120. if (!PEAR::isError($chan)) {
  121. $to = $chan->getServer();
  122. } else {
  123. $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
  124. return false;
  125. }
  126. } else {
  127. if ($this->config->isDefinedLayer('ftp')) {
  128. // try the remote config file first
  129. $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel());
  130. if (is_null($to)) {
  131. // then default to local
  132. $to = $this->config->get($a['to'], null, $pkg->getChannel());
  133. }
  134. } else {
  135. $to = $this->config->get($a['to'], null, $pkg->getChannel());
  136. }
  137. }
  138. if (is_null($to)) {
  139. $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
  140. return false;
  141. }
  142. } elseif ($a['type'] == 'php-const') {
  143. if ($this->installphase == PEAR_TASK_PACKAGE) {
  144. return false;
  145. }
  146. if (defined($a['to'])) {
  147. $to = constant($a['to']);
  148. } else {
  149. $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]");
  150. return false;
  151. }
  152. } else {
  153. if ($t = $pkg->packageInfo($a['to'])) {
  154. $to = $t;
  155. } else {
  156. $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]");
  157. return false;
  158. }
  159. }
  160. if (!is_null($to)) {
  161. $subst_from[] = $a['from'];
  162. $subst_to[] = $to;
  163. }
  164. }
  165. $this->logger->log(3, "doing " . sizeof($subst_from) .
  166. " substitution(s) for $dest");
  167. if (sizeof($subst_from)) {
  168. $contents = str_replace($subst_from, $subst_to, $contents);
  169. }
  170. return $contents;
  171. }
  172. }
  173. ?>