Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 1
<?php
2
/**
3
 * <tasks:replace>
4
 *
5
 * PHP versions 4 and 5
6
 *
187 mathias 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
 * @link      http://pear.php.net/package/PEAR
13
 * @since     File available since Release 1.4.0a1
94 jpm 14
 */
15
/**
16
 * Base class
17
 */
18
require_once 'PEAR/Task/Common.php';
19
/**
20
 * Implements the replace file task.
21
 * @category   pear
22
 * @package    PEAR
23
 * @author     Greg Beaver <cellog@php.net>
187 mathias 24
 * @copyright  1997-2009 The Authors
25
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
26
 * @version    Release: 1.10.1
94 jpm 27
 * @link       http://pear.php.net/package/PEAR
28
 * @since      Class available since Release 1.4.0a1
29
 */
30
class PEAR_Task_Replace extends PEAR_Task_Common
31
{
187 mathias 32
    public $type = 'simple';
33
    public $phase = PEAR_TASK_PACKAGEANDINSTALL;
34
    public $_replacements;
94 jpm 35
 
36
    /**
37
     * Validate the raw xml at parsing-time.
187 mathias 38
     *
39
     * @param  PEAR_PackageFile_v2
40
     * @param  array raw, parsed xml
41
     * @param  PEAR_Config
94 jpm 42
     */
187 mathias 43
    public static function validateXml($pkg, $xml, $config, $fileXml)
94 jpm 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'],
187 mathias 60
                    $config->getKeys(), );
94 jpm 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'],
187 mathias 67
                    array('valid PHP constant'), );
94 jpm 68
            }
69
        } elseif ($xml['attribs']['type'] == 'package-info') {
187 mathias 70
            if (in_array(
71
                $xml['attribs']['to'],
94 jpm 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',
187 mathias 75
                    'date', 'time', )
76
            )) {
94 jpm 77
                return true;
78
            } else {
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',
187 mathias 83
                    'date', 'time', ), );
94 jpm 84
            }
85
        } else {
86
            return array(PEAR_TASK_ERROR_WRONG_ATTRIB_VALUE, 'type', $xml['attribs']['type'],
187 mathias 87
                array('pear-config', 'package-info', 'php-const'), );
94 jpm 88
        }
187 mathias 89
 
94 jpm 90
        return true;
91
    }
92
 
93
    /**
94
     * Initialize a task instance with the parameters
95
     * @param array raw, parsed xml
96
     * @param unused
187 mathias 97
     * @param unused
94 jpm 98
     */
187 mathias 99
    public function init($xml, $attribs, $lastVersion = null)
94 jpm 100
    {
101
        $this->_replacements = isset($xml['attribs']) ? array($xml) : $xml;
102
    }
103
 
104
    /**
105
     * Do a package.xml 1.0 replacement, with additional package-info fields available
106
     *
107
     * See validateXml() source for the complete list of allowed fields
187 mathias 108
     *
109
     * @param  PEAR_PackageFile_v1|PEAR_PackageFile_v2
110
     * @param  string file contents
111
     * @param  string the eventual final file location (informational only)
94 jpm 112
     * @return string|false|PEAR_Error false to skip this file, PEAR_Error to fail
187 mathias 113
     *                                 (use $this->throwError), otherwise return the new contents
94 jpm 114
     */
187 mathias 115
    public function startSession($pkg, $contents, $dest)
94 jpm 116
    {
117
        $subst_from = $subst_to = array();
118
        foreach ($this->_replacements as $a) {
119
            $a = $a['attribs'];
120
            $to = '';
121
            if ($a['type'] == 'pear-config') {
122
                if ($this->installphase == PEAR_TASK_PACKAGE) {
123
                    return false;
124
                }
125
                if ($a['to'] == 'master_server') {
126
                    $chan = $this->registry->getChannel($pkg->getChannel());
127
                    if (!PEAR::isError($chan)) {
128
                        $to = $chan->getServer();
129
                    } else {
130
                        $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
187 mathias 131
 
94 jpm 132
                        return false;
133
                    }
134
                } else {
135
                    if ($this->config->isDefinedLayer('ftp')) {
136
                        // try the remote config file first
137
                        $to = $this->config->get($a['to'], 'ftp', $pkg->getChannel());
138
                        if (is_null($to)) {
139
                            // then default to local
140
                            $to = $this->config->get($a['to'], null, $pkg->getChannel());
141
                        }
142
                    } else {
143
                        $to = $this->config->get($a['to'], null, $pkg->getChannel());
144
                    }
145
                }
146
                if (is_null($to)) {
147
                    $this->logger->log(0, "$dest: invalid pear-config replacement: $a[to]");
187 mathias 148
 
94 jpm 149
                    return false;
150
                }
151
            } elseif ($a['type'] == 'php-const') {
152
                if ($this->installphase == PEAR_TASK_PACKAGE) {
153
                    return false;
154
                }
155
                if (defined($a['to'])) {
156
                    $to = constant($a['to']);
157
                } else {
158
                    $this->logger->log(0, "$dest: invalid php-const replacement: $a[to]");
187 mathias 159
 
94 jpm 160
                    return false;
161
                }
162
            } else {
163
                if ($t = $pkg->packageInfo($a['to'])) {
164
                    $to = $t;
165
                } else {
166
                    $this->logger->log(0, "$dest: invalid package-info replacement: $a[to]");
187 mathias 167
 
94 jpm 168
                    return false;
169
                }
170
            }
171
            if (!is_null($to)) {
172
                $subst_from[] = $a['from'];
173
                $subst_to[] = $to;
174
            }
175
        }
187 mathias 176
        $this->logger->log(
177
            3, "doing ".sizeof($subst_from).
178
            " substitution(s) for $dest"
179
        );
94 jpm 180
        if (sizeof($subst_from)) {
181
            $contents = str_replace($subst_from, $subst_to, $contents);
182
        }
187 mathias 183
 
94 jpm 184
        return $contents;
185
    }
186
}