PEAR2_Pyrus_Developer-0.3.1PEAR2_Pyrus_Developer-0.3.1/php/PEAR2/Pyrus/Developer/PackageFile/PEAR2SVN.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
 * Create a brand new package.xml from the subversion layout of a PEAR2 package
 *
 * This class assumes:
 *
 *  1. the source layout is:
 *     <pre>
 *     PackageName/src           [role="php"]
 *                /examples      [role="doc"]
 *                /doc           [role="doc"]
 *                /data          [role="data"]
 *                /customrole    [role="customrole"]
 *                /customtask    [role="customtask"]
 *                /customcommand [role="customcommand"]
 *                /tests         [role="test"]
 *                /www           [role="www"]
 *                /scripts       [role="script"]
 *     </pre>
 *  2. if file PackageName/README exists, it contains the summary as the first line,
 *     and the description as the rest of the file
 *  3. if file PackageName/CREDITS exists, it contains the maintainers in this format:
 *     ; comment ignored
 *     Name [handle] <email> (role)
 *     Name2 [handle2] <email> (role/inactive)
 */
namespace PEAR2\Pyrus\Developer\PackageFile;
class PEAR2SVN
{
    protected $path;
    protected $package;
    protected $pxml;
    /**
     * package.xml that will be added as the compatible package.xml for PEAR 1.x
     */
    protected $pxml_compatible;
    protected $doCompatible = false;
    /**
     * Create or update a package.xml from CVS
     *
     * @param string $path full path to the SVN checkout
     * @param string $packagename Package name (PEAR2_Pyrus, for example)
     * @param string $channel Channel (pear2.php.net)
     * @param bool $return if true, creation is not attempted in the constructor,
     *                     otherwise, the constructor writes package.xml to disk
     *                     if possible
     * @param bool $fullpathsused if true, for package PEAR2_Package_Name it is
     *                            assumed that src/PEAR2/Package/ exists in SVN,
     *                            otherwise, we assume src/ is used and baseinstalldir
     *                            should be PEAR2/Package for "/" directory
     */
    function __construct($path, $packagename = '##set me##', $channel = 'pear2.php.net',
                         $return = false, $fullpathsused = true, $doCompatible = true)
    {
        $this->doCompatible = $doCompatible;
        if (file_exists($path . DIRECTORY_SEPARATOR . 'package.xml')) {
            try {
                $this->pxml = new \PEAR2\Pyrus\PackageFile(
                    $path . DIRECTORY_SEPARATOR . 'package.xml',
                    'PEAR2\Pyrus\Developer\PackageFile\v2');
                $this->pxml = $this->pxml->info;
                $this->pxml->setFilelist(array());
            } catch (Exception $e) {
                $this->pxml = new \PEAR2\Pyrus\Developer\PackageFile\v2;
                $this->pxml->name = $packagename;
                $this->pxml->channel = $channel;
            }
        } else {
            $this->pxml = new \PEAR2\Pyrus\Developer\PackageFile\v2;
            $this->pxml->name = $packagename;
            $this->pxml->channel = $channel;
        }
        if ($doCompatible) {
            $this->pxml_compatible = new \PEAR2\Pyrus\Developer\PackageFile\v2;
            $this->pxml_compatible->name = $packagename;
            $this->pxml_compatible->channel = $channel;
        }
        $this->path = $path;
        
        $this->parseREADME();
        $this->parseCREDITS();
        $this->parseRELEASE();
        $this->parseAPI();
        
        $packagepath = explode('_', $packagename);

        if ($fullpathsused) {
            if ($this->pxml->channel == 'pear2.php.net') {
                $packagepath = array('PEAR2');
            } else {
                $packagepath = array('/');
            }
        } else {
            array_pop($packagepath);
        }

        $this->scanFiles($packagepath);

        $this->validate();
        try {
            if (!$return) {
                $this->save();
            }
        } catch (Exception $e) {
            // ignore - we'll let the user do this business
            echo 'WARNING: validation failed in constructor, you must fix the package.xml ' .
                'manually:', $e;
        }
    }
    
    /**
     * Scan the directories top populate the package file contents.
     *
     * @param string $packagepath
     */
    function scanFiles($packagepath)
    {
        $this->pxml->setBaseInstallDirs(array(
            'src'     => implode('/', $packagepath),
            'customrole'    => '/',
            'customtask'    => '/',
            'customcommand' => '/',
            'data'    => '/',
            'doc'     => '/',
            'tests'   => '/',
            'scripts' => '/',
            'www'     => '/',
        ));
        $this->pxml_compatible->setBaseInstallDirs(array(
            'src'     => implode('/', $packagepath),
            'customrole'    => '/',
            'customtask'    => '/',
            'customcommand' => '/',
            'data'    => '/',
            'doc'     => '/',
            'tests'   => '/',
            'scripts' => '/',
            'www'     => '/',
        ));

        $rolemap = array(
            'src'           => 'php',
            'data'          => 'data',
            'customrole'    => 'customrole',
            'customtask'    => 'customtask',
            'customcommand' => 'customcommand',
            'doc'           => 'doc',
            'tests'         => 'test',
            'examples'      => 'doc',
            'scripts'       => 'script',
            'www'           => 'www',);

        foreach ($rolemap as $dir => $role) {
            if (file_exists($this->path . DIRECTORY_SEPARATOR . $dir)) {
                $basepath = ($dir === 'examples') ? 'examples' : '';
                foreach (new \PEAR2\Pyrus\Developer\PackageFile\PEAR2SVN\Filter(
                            $this->path . DIRECTORY_SEPARATOR . $dir,
                         new \RecursiveIteratorIterator(
                         new \RecursiveDirectoryIterator($this->path . DIRECTORY_SEPARATOR . $dir),
                         \RecursiveIteratorIterator::LEAVES_ONLY), $role) as $file) {
                    $curpath = str_replace($this->path . DIRECTORY_SEPARATOR . $dir, '',
                        $file->getPathName());
                    if ($curpath && $curpath[0] === DIRECTORY_SEPARATOR) {
                        $curpath = substr($curpath, 1);
                    }
                    $curpath = $dir . '/' . $curpath;
                    $curpath = str_replace('\\', '/', $curpath);
                    $curpath = str_replace('//', '/', $curpath);
                    $this->pxml->files[$curpath] =
                        array(
                            'attribs' => array('role' => $role)
                        );

                    $roleobject = \PEAR2\Pyrus\Installer\Role::factory($this->pxml->type, $role);
                    if ($role == 'customcommand' || $role == 'customrole' || $role == 'customtask') {
                        $compatiblerole = 'data';
                    } else {
                        $compatiblerole = $role;
                    }

                    $attribs = array('name' => $curpath, 'role' => $compatiblerole);
                    $baseinstalldir = $this->pxml_compatible->getBaseinstallDir($curpath);
                    if ($baseinstalldir && $baseinstalldir != '/') {
                        $attribs['baseinstalldir'] = $baseinstalldir;
                    }

                    $curpath = $roleobject->getPackagingLocation($this->pxml_compatible,
                                                                 $attribs);
                    $packagepath = $roleobject->getCompatibleInstallAs($this->pxml_compatible,
                                                                       $attribs);
                    $this->pxml_compatible->files[$curpath] =
                        array(
                            'attribs' => array('role' => $compatiblerole)
                        );
                    $this->pxml_compatible->release[0]->installAs($curpath, $packagepath);
                }
            }
        }
        $this->pxml_compatible->dependencies['required']->pearinstaller->min = '1.4.8';
    }
    
    /**
     * Parse the README file to populate the package summary and description.
     *
     */
    function parseREADME()
    {
        $description = '';
        if (file_exists($this->path . DIRECTORY_SEPARATOR . 'README')) {
            $a = new \SplFileInfo($this->path . DIRECTORY_SEPARATOR . 'README');
            foreach ($a->openFile('r') as $num => $line) {
                if (!$num) {
                    $this->pxml->summary = $line;
                    if ($this->doCompatible) {
                        $this->pxml_compatible->summary = $line;
                    }
                    continue;
                }
                $description .= $line;
            }
            $this->pxml->description = $description;
            if ($this->doCompatible) {
                $this->pxml_compatible->description = $description;
            }
        }
    }
    
    /**
     * Parse the CREDITS file to populate the package developers, roles and email.
     *
     */
    function parseCREDITS()
    {
        if (file_exists($this->path . DIRECTORY_SEPARATOR . 'CREDITS')) {
            $a = new \SplFileInfo($this->path . DIRECTORY_SEPARATOR . 'CREDITS');
            foreach ($a->openFile('r') as $line) {
                if ($line && $line[0] === ';') {
                    continue;
                }
                if (preg_match('/^(.+) \[(.+)\] \<(.+)\> \((.+?)(\/inactive)?\)/', $line, $match)) {
                    $this->pxml->maintainer[$match[2]]
                        ->role($match[4])
                        ->name($match[1])
                        ->email($match[3])
                        ->active(isset($match[5]) ? 'no' : 'yes');
                    if ($this->doCompatible) {
                        $this->pxml_compatible->maintainer[$match[2]]
                            ->role($match[4])
                            ->name($match[1])
                            ->email($match[3])
                            ->active(isset($match[5]) ? 'no' : 'yes');
                    }
                }
            }
        }
    }

    /**
     * Parse the RELEASE file to populate the release notes.
     *
     */
    function parseRELEASE()
    {
        $files = array();
        foreach (new \RegexIterator(
                                   new \DirectoryIterator($this->path),
                                   '/^RELEASE\-(.+)$/', \RegexIterator::GET_MATCH) as $file) {
            $files[$file[1]] = $file;
        }
        if (count($files)) {
            uksort($files, 'version_compare');
            list($releasenotesfile, $releaseversion) = array_pop($files);
            $stability = $this->guessStabilityFromVersion($releaseversion);

            $this->pxml->version['release']   = $releaseversion;
            $this->pxml->stability['release'] = $stability;
            $this->pxml->notes                = file_get_contents($this->path . DIRECTORY_SEPARATOR . $releasenotesfile);

            $apistability = $stability;
            $apiversion   = $this->pxml->version['api'];

            if ($stability == 'beta') {
                if ($apiversion == '0.1.0') {
                    $apiversion = '1.0.0';
                }
                $apistability = 'stable';
            }

            $this->pxml->version['api']   = $apiversion;
            $this->pxml->stability['api'] = $apistability;

            if ($this->doCompatible) {
                $this->pxml_compatible->version['release']   = $releaseversion;
                $this->pxml_compatible->stability['release'] = $stability;
                $this->pxml_compatible->version['api']       = $apiversion;
                $this->pxml_compatible->stability['api']     = $apistability;
                $this->pxml_compatible->notes                = $this->pxml->notes;
            }
        }
    }

    /**
     * Parse the API file to populate the API version (if present).
     *
     */
    function parseAPI()
    {
        $files = array();
        foreach (new \RegexIterator(
                                   new \DirectoryIterator($this->path),
                                   '/^API\-(.+)$/', \RegexIterator::GET_MATCH) as $file) {
            $files[$file[1]] = $file;
        }
        if (count($files)) {
            uksort($files, 'version_compare');
            list($apinotesfile, $apiversion) = array_pop($files);
            $stability = $this->guessStabilityFromVersion($apiversion);

            $this->pxml->version['api']   = $apiversion;
            $this->pxml->stability['api'] = $stability;

            $this->pxml->notes = $this->pxml->notes .
                "\n\n" . file_get_contents($this->path . DIRECTORY_SEPARATOR . $apinotesfile);

            if ($this->doCompatible) {
                $this->pxml_compatible->version['api']   = $apiversion;
                $this->pxml_compatible->stability['api'] = $stability;
                $this->pxml_compatible->notes            = $this->pxml->notes;
            }
        }
    }

    function guessStabilityFromVersion($version)
    {
        if (false !== strpos($version, 'beta')) {
            return 'beta';
        }
        if (false !== strpos($version, 'RC')) {
            return 'beta';
        }
        if (false !== strpos($version, 'b')) {
            return 'beta';
        }
        if (false !== strpos($version, 'alpha')) {
            return 'alpha';
        }
        if (false !== strpos($version, 'a')) {
            return 'alpha';
        }
        if (false !== strpos($version, 'devel')) {
            return 'devel';
        }
        if (false !== strpos($version, 'dev')) {
            return 'devel';
        }
        if (false !== strpos($version, 'devel')) {
            return 'devel';
        }
        $components = explode('.', $version);
        if ('0' === $components[0]) {
            return 'alpha';
        }
        return 'stable';
    }

    function validate()
    {
        $package = new \PEAR2\Pyrus\Package(false);
        $xmlcontainer = new \PEAR2\Pyrus\PackageFile($this->pxml);
        $xml = new \PEAR2\Pyrus\Package\Xml($this->path . '/package.xml', $package, $xmlcontainer);
        $package->setInternalPackage($xml);

        $this->pxml->getValidator()->validate($package);
    }

    function __toString()
    {
        return (string) $this->pxml;
    }

    function save()
    {
        file_put_contents($this->path . '/package.xml', $this->pxml);
        if ($this->doCompatible) {
            $this->pxml_compatible->date = date('Y-m-d');
            $this->pxml_compatible->time = date('H:i:s');
            $info = $this->pxml_compatible->toArray(true);
            $stuff = new \PEAR2\Pyrus\XMLWriter($info);
            file_put_contents($this->path . '/package_compatible.xml', $stuff);
        }
    }

    function __set($var, $value)
    {
        $this->pxml->$var = $value;
        if ($this->doCompatible) {
            $this->pxml_compatible->$var = $value;
        }
    }

    function __get($var)
    {
        if ($this->doCompatible) {
            if ($var == 'compatiblepackagefile') {
                return $this->pxml_compatible;
            }
        } else {
            if ($var == 'compatiblepackagefile') {
                return null;
            }
        }
        if ($var == 'packagefile') {
            return $this->pxml;
        }
        if ($var == 'path') {
            return $this->path;
        }
        return $this->pxml->$var;
    }
}
EOF