1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * A task to create a PEAR package.xml version 2.0 file.
22
 *
23
 * This class uses the PEAR_PackageFileManager2 class to perform the work.
24
 *
25
 * This class is designed to be very flexible -- i.e. account for changes to the package.xml w/o
26
 * requiring changes to this class.  We've accomplished this by having generic <option> and <mapping>
27
 * nested elements.  All options are set using PEAR_PackageFileManager2::setOptions().
28
 *
29
 * The <option> tag is used to set a simple option value.
30
 * <code>
31
 * <option name="option_name" value="option_value"/>
32
 * or <option name="option_name">option_value</option>
33
 * </code>
34
 *
35
 * The <mapping> tag represents a complex data type.  You can use nested <element> (and nested <element> with
36
 * <element> tags) to represent the full complexity of the structure.  Bear in mind that what you are creating
37
 * will be mapped to an associative array that will be passed in via PEAR_PackageFileManager2::setOptions().
38
 * <code>
39
 * <mapping name="option_name">
40
 *  <element key="key_name" value="key_val"/>
41
 *  <element key="key_name" value="key_val"/>
42
 * </mapping>
43
 * </code>
44
 *
45
 * Here's an over-simple example of how this could be used:
46
 * <code>
47
 * <pearpkg2 name="phing" dir="${build.src.dir}">
48
 *  <fileset dir="src">
49
 *      <include name="**"/>
50
 *  </fileset>
51
 *  <option name="outputdirectory" value="./build"/>
52
 *  <option name="packagefile" value="package2.xml"/>
53
 *  <option name="packagedirectory" value="./${build.dist.dir}"/>
54
 *  <option name="baseinstalldir" value="${pkg.prefix}"/>
55
 *  <option name="channel" value="my.pear-channel.com"/>
56
 *  <option name="summary" value="${pkg.summary}"/>
57
 *  <option name="description" value="${pkg.description}"/>
58
 *  <option name="apiversion" value="${pkg.version}"/>
59
 *  <option name="apistability" value="beta"/>
60
 *  <option name="releaseversion" value="${pkg.version}"/>
61
 *  <option name="releasestability" value="beta"/>
62
 *  <option name="license" value="none"/>
63
 *  <option name="phpdep" value="5.0.0"/>
64
 *  <option name="pearinstallerdep" value="1.4.6"/>
65
 *  <option name="packagetype" value="php"/>
66
 *  <option name="notes" value="${pkg.relnotes}"/>
67
 *  <mapping name="maintainers">
68
 *   <element>
69
 *    <element key="handle" value="hlellelid"/>
70
 *    <element key="name" value="Hans"/>
71
 *    <element key="email" value="hans@xmpl.org"/>
72
 *    <element key="role" value="lead"/>
73
 *    <element key="active" value="yes"/>
74
 *   </element>
75
 *  </mapping>
76
 * </pearpkg2>
77
 * </code>
78
 *
79
 * Look at the build.xml in the Phing base directory (assuming you have the full distro / CVS version of Phing) to
80
 * see a more complete example of how to call this script.
81
 *
82
 * @author  Stuart Binge <stuart.binge@complinet.com>
83
 * @author  Hans Lellelid <hans@xmpl.org>
84
 * @package phing.tasks.ext
85
 */
86
class PearPackage2Task extends PearPackageTask
87
{
88 0
    public function init()
89
    {
90 0
        include_once 'PEAR/PackageFileManager2.php';
91 0
        if (!class_exists('PEAR_PackageFileManager2')) {
92 0
            throw new BuildException("You must have installed PEAR_PackageFileManager in order to create a PEAR package.xml version 2.0 file.");
93
        }
94
    }
95

96 0
    protected function setVersion2Options()
97
    {
98 0
        $this->pkg->setPackage($this->package);
99 0
        $this->pkg->setDate(strftime('%Y-%m-%d'));
100 0
        $this->pkg->setTime(strftime('%H:%M:%S'));
101

102 0
        $newopts = [];
103 0
        foreach ($this->options as $opt) {
104 0
            switch ($opt->getName()) {
105 0
                case 'summary':
106 0
                    $this->pkg->setSummary($opt->getValue());
107 0
                    break;
108

109 0
                case 'description':
110 0
                    $this->pkg->setDescription($opt->getValue());
111 0
                    break;
112

113 0
                case 'uri':
114 0
                    $this->pkg->setUri($opt->getValue());
115 0
                    break;
116

117 0
                case 'license':
118 0
                    $this->pkg->setLicense($opt->getValue());
119 0
                    break;
120

121 0
                case 'channel':
122 0
                    $this->pkg->setChannel($opt->getValue());
123 0
                    break;
124

125 0
                case 'apiversion':
126 0
                    $this->pkg->setAPIVersion($opt->getValue());
127 0
                    break;
128

129 0
                case 'releaseversion':
130 0
                    $this->pkg->setReleaseVersion($opt->getValue());
131 0
                    break;
132

133 0
                case 'releasestability':
134 0
                    $this->pkg->setReleaseStability($opt->getValue());
135 0
                    break;
136

137 0
                case 'apistability':
138 0
                    $this->pkg->setAPIStability($opt->getValue());
139 0
                    break;
140

141 0
                case 'notes':
142 0
                    $this->pkg->setNotes($opt->getValue());
143 0
                    break;
144

145 0
                case 'packagetype':
146 0
                    $this->pkg->setPackageType($opt->getValue());
147 0
                    break;
148

149 0
                case 'phpdep':
150 0
                    $this->pkg->setPhpDep($opt->getValue());
151 0
                    break;
152

153 0
                case 'pearinstallerdep':
154 0
                    $this->pkg->setPearinstallerDep($opt->getValue());
155 0
                    break;
156

157
                default:
158 0
                    $newopts[] = $opt;
159 0
                    break;
160
            }
161
        }
162 0
        $this->options = $newopts;
163

164 0
        $newmaps = [];
165 0
        foreach ($this->mappings as $map) {
166 0
            switch ($map->getName()) {
167 0
                case 'deps':
168 0
                    $deps = $map->getValue();
169 0
                    foreach ($deps as $dep) {
170 0
                        $type = isset($dep['optional']) ? 'optional' : 'required';
171 0
                        $min = $dep['min'] ?? $dep['version'];
172 0
                        $max = $dep['max'] ?? null;
173 0
                        $rec = $dep['recommended'] ?? null;
174 0
                        $channel = $dep['channel'] ?? false;
175 0
                        $uri = $dep['uri'] ?? false;
176

177 0
                        if (!empty($channel)) {
178 0
                            $this->pkg->addPackageDepWithChannel(
179 0
                                $type,
180 0
                                $dep['name'],
181
                                $channel,
182
                                $min,
183
                                $max,
184
                                $rec
185
                            );
186 0
                        } elseif (!empty($uri)) {
187 0
                            $this->pkg->addPackageDepWithUri(
188 0
                                $type,
189 0
                                $dep['name'],
190
                                $uri
191
                            );
192
                        }
193
                    }
194 0
                    break;
195

196 0
                case 'extdeps':
197 0
                    $deps = $map->getValue();
198 0
                    foreach ($deps as $dep) {
199 0
                        $type = isset($dep['optional']) ? 'optional' : 'required';
200 0
                        $min = $dep['min'] ?? $dep['version'];
201 0
                        $max = $dep['max'] ?? $dep['version'];
202 0
                        $rec = $dep['recommended'] ?? $dep['version'];
203

204 0
                        $this->pkg->addExtensionDep(
205 0
                            $type,
206 0
                            $dep['name'],
207
                            $min,
208
                            $max,
209
                            $rec
210
                        );
211
                    }
212 0
                    break;
213

214 0
                case 'maintainers':
215 0
                    $maintainers = $map->getValue();
216

217 0
                    foreach ($maintainers as $maintainer) {
218 0
                        if (!isset($maintainer['active'])) {
219 0
                            $maintainer['active'] = 'yes';
220
                        } else {
221 0
                            $maintainer['active'] = $maintainer['active'] === false ? 'no' : 'yes';
222
                        }
223 0
                        $this->pkg->addMaintainer(
224 0
                            $maintainer['role'],
225 0
                            $maintainer['handle'],
226 0
                            $maintainer['name'],
227 0
                            $maintainer['email'],
228 0
                            $maintainer['active']
229
                        );
230
                    }
231 0
                    break;
232

233 0
                case 'replacements':
234 0
                    $replacements = $map->getValue();
235

236 0
                    foreach ($replacements as $replacement) {
237 0
                        $this->pkg->addReplacement(
238 0
                            $replacement['path'],
239 0
                            $replacement['type'],
240 0
                            $replacement['from'],
241 0
                            $replacement['to']
242
                        );
243
                    }
244 0
                    break;
245

246 0
                case 'role':
247 0
                    foreach ($map->getValue() as $role) {
248 0
                        $this->pkg->addRole($role['extension'], $role['role']);
249
                    }
250 0
                    break;
251

252
                default:
253 0
                    $newmaps[] = $map;
254
            }
255
        }
256 0
        $this->mappings = $newmaps;
257
    }
258

259
    /**
260
     * Main entry point.
261
     *
262
     * @throws BuildException
263
     * @return void
264
     */
265 0
    public function main()
266
    {
267 0
        if ($this->dir === null) {
268 0
            throw new BuildException("You must specify the \"dir\" attribute for PEAR package 2 task.");
269
        }
270

271 0
        if ($this->package === null) {
272 0
            throw new BuildException("You must specify the \"name\" attribute for PEAR package 2 task.");
273
        }
274

275 0
        $this->pkg = new PEAR_PackageFileManager2();
276

277 0
        $this->setVersion2Options();
278 0
        $this->setOptions();
279

280 0
        $this->pkg->addRelease();
281 0
        $this->pkg->generateContents();
282 0
        $e = $this->pkg->writePackageFile();
283 0
        if (@PEAR::isError($e)) {
284 0
            throw new BuildException("Unable to write package file.", new Exception($e->getMessage()));
285
        }
286
    }
287
}

Read our documentation on viewing source code .

Loading