Code Coverage for /home/user/workspace/all/Pyrus/src/Pyrus/Task/Replace.php

Coverage: 29%

Aggregate Code Coverage for all tests

       1           : <?php
       2           : /**
       3           :  * <tasks:replace>
       4           :  *
       5           :  * PHP version 5
       6           :  *
       7           :  * @category  PEAR2
       8           :  * @package   PEAR2_Pyrus
       9           :  * @author    Greg Beaver <cellog@php.net>
      10           :  * @copyright 2008 The PEAR Group
      11           :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      12           :  * @version   SVN: $Id$
      13           :  * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
      14           :  */
      15           : 
      16           : /**
      17           :  * Implements the replace file task.
      18           :  *
      19           :  * @category  PEAR2
      20           :  * @package   PEAR2_Pyrus
      21           :  * @author    Greg Beaver <cellog@php.net>
      22           :  * @copyright 2008 The PEAR Group
      23           :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      24           :  * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
      25           :  */
      26           : class PEAR2_Pyrus_Task_Replace extends PEAR2_Pyrus_Task_Common
      27         2 : {
      28           :     var $type = 'simple';
      29           :     var $phase = PEAR2_PYRUS_TASK_PACKAGEANDINSTALL;
      30           :     var $_replacements;
      31           : 
      32           :     /**
      33           :      * Validate the raw xml at parsing-time.
      34           :      * @param PEAR_PackageFile_v2
      35           :      * @param array raw, parsed xml
      36           :      * @param PEAR_Config
      37           :      * @static
      38           :      */
      39           :     static function validateXml($pkg, $xml, $config, $fileXml)
      40           :     {
      41           :         if (!isset($xml['attribs'])) {
      42           :             return array(PEAR2_PYRUS_TASK_ERROR_NOATTRIBS);
      43           :         }
      44           :         if (!isset($xml['attribs']['type'])) {
      45           :             return array(PEAR2_PYRUS_TASK_ERROR_MISSING_ATTRIB, 'type');
      46           :         }
      47           :         if (!isset($xml['attribs']['to'])) {
      48           :             return array(PEAR2_PYRUS_TASK_ERROR_MISSING_ATTRIB, 'to');
      49           :         }
      50           :         if (!isset($xml['attribs']['from'])) {
      51           :             return array(PEAR2_PYRUS_TASK_ERROR_MISSING_ATTRIB, 'from');
      52           :         }
      53           :         if ($xml['attribs']['type'] == 'pear-config') {
      54           :             if (!in_array($xml['attribs']['to'], $config->systemvars)) {
      55           :                 return array(PEAR2_PYRUS_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
      56           :                     $config->systemvars);
      57           :             }
      58           :         } elseif ($xml['attribs']['type'] == 'php-const') {
      59           :             if (defined($xml['attribs']['to'])) {
      60           :                 return true;
      61           :             } else {
      62           :                 return array(PEAR2_PYRUS_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
      63           :                     array('valid PHP constant'));
      64           :             }
      65           :         } elseif ($xml['attribs']['type'] == 'package-info') {
      66           :             if (in_array($xml['attribs']['to'],
      67           :                 array('name', 'summary', 'channel', 'notes', 'extends', 'description',
      68           :                     'release_notes', 'license', 'release-license', 'license-uri',
      69           :                     'version', 'api-version', 'state', 'api-state', 'release_date',
      70           :                     'date', 'time'))) {
      71           :                 return true;
      72           :             } else {
      73           :                 return array(PEAR2_PYRUS_TASK_ERROR_WRONG_ATTRIB_VALUE, 'to', $xml['attribs']['to'],
      74           :                     array('name', 'summary', 'channel', 'notes', 'extends', 'description',
      75           :                     'release_notes', 'license', 'release-license', 'license-uri',
      76           :                     'version', 'api-version', 'state', 'api-state', 'release_date',
      77           :                     'date', 'time'));
      78           :             }
      79           :         } else {
      80           :             return array(PEAR2_PYRUS_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'],
      81           :                 array('pear-config', 'package-info', 'php-const'));
      82           :         }
      83           :         return true;
      84           :     }
      85           : 
      86           :     /**
      87           :      * Initialize a task instance with the parameters
      88           :      * @param array raw, parsed xml
      89           :      * @param unused
      90           :      */
      91           :     function init($xml, $attribs, $lastVersion)
      92           :     {
      93         2 :         $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml;
      94         2 :         parent::init($xml, $attribs, $lastVersion);
      95         2 :     }
      96           : 
      97           :     /**
      98           :      * Do a package.xml 1.0 replacement, with additional package-info fields available
      99           :      *
     100           :      * See validateXml() source for the complete list of allowed fields
     101           :      * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
     102           :      * @param string file contents
     103           :      * @param string the eventual final file location (informational only)
     104           :      * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
     105           :      *         (use $this->throwError), otherwise return the new contents
     106           :      */
     107           :     function startSession($pkg, $contents, $dest)
     108           :     {
     109         1 :         $subst_from = $subst_to = array();
     110         1 :         foreach ($this->_replacements as $a) {
     111         1 :             $a = $a['attribs'];
     112         1 :             $to = '';
     113         1 :             if ($a['type'] == 'pear-config') {
     114           :                 if ($this->installphase == PEAR2_PYRUS_TASK_PACKAGE) {
     115           :                     return false;
     116           :                 }
     117           :                 if (false) {// $this->config->isDefinedLayer('ftp')) {
     118           :                     // try the remote config file first
     119           :                     $to = $this->config->{$a['to']};
     120           :                     if (is_null($to)) {
     121           :                         // then default to local
     122           :                         $to = $this->config->{$a['to']};
     123           :                     }
     124           :                 } else {
     125           :                     $to = $this->config->{$a['to']};
     126           :                 }
     127           :                 if (is_null($to)) {
     128           :                     PEAR2_Pyrus_Log::log(0, "$dest: invalid pear-config replacement: $a[to]");
     129           :                     return false;
     130           :                 }
     131         1 :             } elseif ($a['type'] == 'php-const') {
     132           :                 if ($this->installphase == PEAR2_PYRUS_TASK_PACKAGE) {
     133           :                     return false;
     134           :                 }
     135           :                 if (defined($a['to'])) {
     136           :                     $to = constant($a['to']);
     137           :                 } else {
     138           :                     PEAR2_Pyrus_Log::log(0, "$dest: invalid php-const replacement: $a[to]");
     139           :                     return false;
     140           :                 }
     141           :             } else {
     142         1 :                 if ($t = $pkg->{$a['to']}) {
     143         1 :                     if ($a['to'] == 'version') {
     144         1 :                         $t = $t['release'];
     145         1 :                     }
     146         1 :                     $to = $t;
     147         1 :                 } else {
     148           :                     PEAR2_Pyrus_Log::log(0, "$dest: invalid package-info replacement: $a[to]");
     149           :                     return false;
     150           :                 }
     151           :             }
     152         1 :             if (!is_null($to)) {
     153         1 :                 $subst_from[] = $a['from'];
     154         1 :                 $subst_to[] = $to;
     155         1 :             }
     156         1 :         }
     157         1 :         PEAR2_Pyrus_Log::log(3, "doing " . sizeof($subst_from) .
     158         1 :             " substitution(s) for $dest");
     159         1 :         if (sizeof($subst_from)) {
     160         1 :             $contents = str_replace($subst_from, $subst_to, $contents);
     161         1 :         }
     162         1 :         return $contents;
     163           :     }
     164           : }
     165         2 : ?>