summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/types/PearPackageFileSet.php
blob: 8cf99b2822a95068b12faeee6d9a6e46ae4338ca (plain)
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
<?php
/**
 * Part of phing, the PHP build tool
 *
 * PHP version 5
 *
 * @category Types
 * @package  phing.types
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
 * @version  SVN: $Id: 5ba010b83645d0ea709761a3d8260fc013239458 $
 * @link     http://www.phing.info/
 */
require_once 'phing/types/FileSet.php';
require_once 'PEAR/Config.php';
require_once 'phing/util/PearPackageScanner.php';

/**
 * Fileset that contains files of an installed PEAR package.
 * It can be used to package up PEAR package dependencies in own
 * release files (zip, tgz, phar).
 *
 * @internal
 * A normal fileset is used that way in CopyTask, rSTTask:
 * <code>
 *  $ds = $fs->getDirectoryScanner($project);
 *  $fromDir  = $fs->getDir($project);
 *  $srcFiles = $ds->getIncludedFiles();
 *  $srcDirs  = $ds->getIncludedDirectories();
 * </code>
 * The scanner is used as follows:
 * <code>
 *  $ds->getBaseDir()
 *  $ds->scan()
 * </code>
 *
 * @category Types
 * @package  phing.types
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
 * @link     http://www.phing.info/
 */
class PearPackageFileSet extends FileSet
{
    /**
     * Name of channel the package is from, e.g. "pear.php.net".
     *
     * @var string
     */
    protected $channel;

    /**
     * Package name to get files from, e.g. "Console_CommandLine"
     *
     * @var string
     */
    protected $package;

    /**
     * Use files of that role only.
     * Multiple roles are not supported, and you always have to specify one.
     *
     * @var string
     */
    protected $role = 'php';

    /**
     * Prefix to prepend to the file paths in the zip
     */
    protected $prefix;

    /**
     * Full path to a PEAR config file.
     * If none provided, default one is used.
     */
    protected $config;

    /**
     * @var PearPackageScanner instance
     */
    protected $pps;


    /**
     * Creates and returns the pear package scanner.
     * Scanner already has scan() called.
     *
     * @param Project $project Current phing project
     *
     * @return PearPackageScanner
     */
    public function getDirectoryScanner(Project $project)
    {
        if ($this->isReference()) {
            $obj = $this->getRef($project);
            return $obj->getDirectoryScanner($project);
        }

        $this->loadPearPackageScanner();
        return $this->pps;
    }

    /**
     * Returns the base directory all package files are relative to
     *
     * @return PhingFile Base directory
     */
    public function getDir()
    {
        if ($this->pps === null) {
            $this->loadPearPackageScanner();
        }
        return new PhingFile((string) $this->pps->getBaseDir());
    }

    /**
     * Loads the package scanner instance into $this->pps
     *
     * @return void
     */
    protected function loadPearPackageScanner()
    {
        $this->pps = new PearPackageScanner();
        $this->pps->setPackage($this->package);
        $this->pps->setChannel($this->channel);
        $this->pps->setRole($this->role);
        $this->pps->setConfig($this->config);
        $this->pps->scan();
    }

    /**
     * Sets the package name.
     * If no channel is given, "pear.php.net" is used.
     *
     * @param string $package Single package name, or "channel/name" combination
     *
     * @return void
     */
    public function setPackage($package)
    {
        $parts = explode('/', $package);
        if (count($parts) > 2) {
            throw new BuildException('Invalid package name: ' . $package);
        }

        if (count($parts) == 1) {
            $this->channel = 'pear.php.net';
            $this->package = $parts[0];
        } else {
            $this->channel = $parts[0];
            $this->package = $parts[1];
        }
    }

    /**
     * Sets the role of files that should be included.
     * Examples are php,doc,script
     *
     * @param string $role PEAR file role
     *
     * @return void
     */
    public function setRole($role)
    {
        $this->role = $role;
    }

    /**
     * Sets the full path to the PEAR configuration file
     *
     * @param string $config Configuration file
     *
     * @return void
     */
    public function setConfig($config)
    {
        $this->config = $config;
    }
}