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
 * FileList represents an explicitly named list of files. FileLists
22
 * are useful when you want to capture a list of files regardless of
23
 * whether they currently exist.
24
 *
25
 * <filelist
26
 *    id="docfiles"
27
 *   dir="${phing.docs.dir}"
28
 *   files="chapters/Installation.html,chapters/Setup.html"/>
29
 *
30
 * OR
31
 *
32
 * <filelist
33
 *         dir="${doc.src.dir}"
34
 *         listfile="${phing.docs.dir}/PhingGuide.book"/>
35
 *
36
 * (or a mixture of files="" and listfile="" can be used)
37
 *
38
 * @author  Hans Lellelid <hans@xmpl.org>
39
 * @package phing.types
40
 */
41
class FileList extends DataType implements IteratorAggregate
42
{
43

44
    // public for "cloning" purposes
45

46
    /**
47
     * Array containing all filenames.
48
     */
49
    public $filenames = [];
50

51
    /**
52
     * Base directory for this file list.
53
     */
54
    public $dir;
55

56
    /**
57
     * @var PhingFile that contains a list of files (one per line).
58
     */
59
    public $listfile;
60

61
    /**
62
     * Construct a new FileList.
63
     *
64
     * @param FileList $filelist
65
     */
66 1
    public function __construct($filelist = null)
67
    {
68 1
        parent::__construct();
69

70 1
        if ($filelist !== null) {
71 0
            $this->dir = $filelist->dir;
72 0
            $this->filenames = $filelist->filenames;
73 0
            $this->listfile = $filelist->listfile;
74
        }
75
    }
76

77 0
    public function getIterator()
78
    {
79 0
        return new ArrayIterator($this->getFiles($this->getProject()));
80
    }
81

82
    /**
83
     * Makes this instance in effect a reference to another FileList
84
     * instance.
85
     *
86
     * @param  Reference $r
87
     * @throws BuildException
88
     */
89 1
    public function setRefid(Reference $r)
90
    {
91 1
        if ($this->dir !== null || count($this->filenames) !== 0) {
92 1
            throw $this->tooManyAttributes();
93
        }
94 0
        parent::setRefid($r);
95
    }
96

97
    /**
98
     * Base directory for files in list.
99
     *
100
     * @param PhingFile $dir
101
     * @throws IOException
102
     * @throws NullPointerException
103
     */
104 1
    public function setDir(PhingFile $dir)
105
    {
106 1
        if ($this->isReference()) {
107 0
            throw $this->tooManyAttributes();
108
        }
109 1
        $this->dir = $dir;
110
    }
111

112
    /**
113
     * Get the basedir for files in list.
114
     *
115
     * @param  Project $p
116
     * @throws BuildException
117
     * @return PhingFile
118
     */
119 1
    public function getDir(Project $p)
120
    {
121 1
        if ($this->isReference()) {
122 0
            $ref = $this->getRef($p);
123

124 0
            return $ref->getDir($p);
125
        }
126

127 1
        return $this->dir;
128
    }
129

130
    /**
131
     * Set the array of files in list.
132
     *
133
     * @param  array $filenames
134
     * @throws BuildException
135
     */
136 1
    public function setFiles($filenames)
137
    {
138 1
        if ($this->isReference()) {
139 0
            throw $this->tooManyAttributes();
140
        }
141 1
        if (!empty($filenames)) {
142 1
            $tok = strtok($filenames, ", \t\n\r");
143 1
            while ($tok !== false) {
144 1
                $fname = trim($tok);
145 1
                if ($fname !== "") {
146 1
                    $this->filenames[] = $tok;
147
                }
148 1
                $tok = strtok(", \t\n\r");
149
            }
150
        }
151
    }
152

153
    /**
154
     * Sets a source "list" file that contains filenames to add -- one per line.
155
     *
156
     * @param string $file
157
     * @throws IOException
158
     * @throws NullPointerException
159
     */
160 1
    public function setListFile($file)
161
    {
162 1
        if ($this->isReference()) {
163 0
            throw $this->tooManyAttributes();
164
        }
165 1
        if (!($file instanceof PhingFile)) {
166 1
            $file = new PhingFile($file);
167
        }
168 1
        $this->listfile = $file;
169
    }
170

171
    /**
172
     * Get the source "list" file that contains file names.
173
     *
174
     * @param  Project $p
175
     * @return PhingFile
176
     */
177 1
    public function getListFile(Project $p)
178
    {
179 1
        if ($this->isReference()) {
180 0
            $ref = $this->getRef($p);
181

182 0
            return $ref->getListFile($p);
183
        }
184

185 1
        return $this->listfile;
186
    }
187

188
    /**
189
     * Returns the list of files represented by this FileList.
190
     *
191
     * @param Project $p
192
     * @return array
193
     * @throws IOException
194
     * @throws BuildException
195
     */
196 1
    public function getFiles(Project $p)
197
    {
198 1
        if ($this->isReference()) {
199 0
            $ret = $this->getRef($p);
200 0
            $ret = $ret->getFiles($p);
201

202 0
            return $ret;
203
        }
204

205 1
        if ($this->dir === null) {
206 1
            throw new BuildException("No directory specified for filelist.");
207
        }
208

209 1
        if ($this->listfile !== null) {
210 0
            $this->readListFile($p);
211
        }
212

213 1
        if (empty($this->filenames)) {
214 1
            throw new BuildException("No files specified for filelist.");
215
        }
216

217 1
        return $this->filenames;
218
    }
219

220
    /**
221
     * Performs the check for circular references and returns the
222
     * referenced FileSet.
223
     *
224
     * @param Project $p
225
     *
226
     * @throws BuildException
227
     *
228
     * @return FileList
229
     */
230 0
    public function getRef(Project $p)
231
    {
232 0
        return $this->getCheckedRef(__CLASS__, $this->getDataTypeName());
233
    }
234

235
    /**
236
     * Reads file names from a file and adds them to the files array.
237
     *
238
     * @param Project $p
239
     *
240
     * @throws BuildException
241
     * @throws IOException
242
     */
243 0
    private function readListFile(Project $p)
244
    {
245 0
        $listReader = null;
246
        try {
247
            // Get a FileReader
248 0
            $listReader = new BufferedReader(new FileReader($this->listfile));
249

250 0
            $line = $listReader->readLine();
251 0
            while ($line !== null) {
252 0
                if (!empty($line)) {
253 0
                    $line = $p->replaceProperties($line);
254 0
                    $this->filenames[] = trim($line);
255
                }
256 0
                $line = $listReader->readLine();
257
            }
258 0
        } catch (Exception $e) {
259 0
            if ($listReader) {
260 0
                $listReader->close();
261
            }
262 0
            throw new BuildException(
263 0
                "An error occurred while reading from list file " . $this->listfile->__toString() . ": " . $e->getMessage()
264
            );
265
        }
266

267 0
        $listReader->close();
268
    }
269
}

Read our documentation on viewing source code .

Loading