PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file Replace.php

Documentation is available at Replace.php

  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. {
  32.     var $type = 'simple';
  33.     var $phase = PEAR_TASK_PACKAGEANDINSTALL;
  34.     var $_replacements;
  35.  
  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.     /**
  91.      * Initialize a task instance with the parameters
  92.      * @param array raw, parsed xml
  93.      * @param unused 
  94.      */
  95.     function init($xml$attribs)
  96.     {
  97.         $this->_replacements = isset($xml['attribs']? array($xml$xml;
  98.     }
  99.  
  100.     /**
  101.      * Do a package.xml 1.0 replacement, with additional package-info fields available
  102.      *
  103.      * See validateXml() source for the complete list of allowed fields
  104.      * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
  105.      * @param string file contents
  106.      * @param string the eventual final file location (informational only)
  107.      * @return string|false|PEAR_Errorfalse to skip this file, PEAR_Error to fail
  108.      *          (use $this->throwError), otherwise return the new contents
  109.      */
  110.     function startSession($pkg$contents$dest)
  111.     {
  112.         $subst_from $subst_to = array();
  113.         foreach ($this->_replacements as $a{
  114.             $a $a['attribs'];
  115.             $to '';
  116.             if ($a['type'== 'pear-config'{
  117.                 if ($this->installphase == PEAR_TASK_PACKAGE{
  118.                     return false;
  119.                 }
  120.                 if ($a['to'== 'master_server'{
  121.                     $chan $this->registry->getChannel($pkg->getChannel());
  122.                     if (!PEAR::isError($chan)) {
  123.                         $to $chan->getServer();
  124.                     else {
  125.                         $this->logger->log(0"$dest: invalid pear-config replacement: $a[to]");
  126.                         return false;
  127.                     }
  128.                 else {
  129.                     if ($this->config->isDefinedLayer('ftp')) {
  130.                         // try the remote config file first
  131.                         $to $this->config->get($a['to']'ftp'$pkg->getChannel());
  132.                         if (is_null($to)) {
  133.                             // then default to local
  134.                             $to $this->config->get($a['to']null$pkg->getChannel());
  135.                         }
  136.                     else {
  137.                         $to $this->config->get($a['to']null$pkg->getChannel());
  138.                     }
  139.                 }
  140.                 if (is_null($to)) {
  141.                     $this->logger->log(0"$dest: invalid pear-config replacement: $a[to]");
  142.                     return false;
  143.                 }
  144.             elseif ($a['type'== 'php-const'{
  145.                 if ($this->installphase == PEAR_TASK_PACKAGE{
  146.                     return false;
  147.                 }
  148.                 if (defined($a['to'])) {
  149.                     $to constant($a['to']);
  150.                 else {
  151.                     $this->logger->log(0"$dest: invalid php-const replacement: $a[to]");
  152.                     return false;
  153.                 }
  154.             else {
  155.                 if ($t $pkg->packageInfo($a['to'])) {
  156.                     $to $t;
  157.                 else {
  158.                     $this->logger->log(0"$dest: invalid package-info replacement: $a[to]");
  159.                     return false;
  160.                 }
  161.             }
  162.             if (!is_null($to)) {
  163.                 $subst_from[$a['from'];
  164.                 $subst_to[$to;
  165.             }
  166.         }
  167.         $this->logger->log(3"doing " sizeof($subst_from.
  168.             " substitution(s) for $dest");
  169.         if (sizeof($subst_from)) {
  170.             $contents str_replace($subst_from$subst_to$contents);
  171.         }
  172.         return $contents;
  173.     }
  174. }
  175. ?>

Documentation generated on Wed, 06 Jul 2011 23:31:16 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.