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
 * This task sets a property to the name of a temporary file.
22
 * Unlike {@link PhingFile::createTempFile()}, this task does not (by default) actually create the
23
 * temporary file, but it does guarantee that the file did not
24
 * exist when the task was executed.
25
 *
26
 * Examples:
27
 *
28
 * `<tempfile property="temp.file" />`
29
 *
30
 * create a temporary file
31
 *
32
 * `<tempfile property="temp.file" suffix=".xml" />`
33
 *
34
 * create a temporary file with the .xml suffix.
35
 *
36
 * `<tempfile property="temp.file" destDir="build"/>`
37
 *
38
 * create a temp file in the build subdir
39
 *
40
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
41
 * @package phing.tasks.system
42
 */
43
class TempFile extends Task
44
{
45
    /**
46
     * Name of property to set.
47
     */
48
    private $property = '';
49

50
    /**
51
     * Directory to create the file in. Can be null.
52
     */
53
    private $destDir;
54

55
    /**
56
     * Prefix for the file.
57
     */
58
    private $prefix;
59

60
    /**
61
     * Suffix for the file.
62
     */
63
    private $suffix = '';
64

65
    /**
66
     * deleteOnExit flag
67
     */
68
    private $deleteOnExit;
69

70
    /**
71
     * createFile flag
72
     */
73
    private $createFile;
74

75
    /**
76
     * Sets the property you wish to assign the temporary file to.
77
     *
78
     * @param string $property The property to set
79
     */
80 1
    public function setProperty($property)
81
    {
82 1
        $this->property = $property;
83
    }
84

85
    /**
86
     * Sets the destination directory. If not set,
87
     * the basedir directory is used instead.
88
     *
89
     * @param string|PhingFile $destDir The new destDir value
90
     */
91 1
    public function setDestDir($destDir)
92
    {
93 1
        if ($destDir instanceof PhingFile) {
94 0
            $this->destDir = $destDir;
95
        } else {
96 1
            $this->destDir = new PhingFile($destDir);
97
        }
98
    }
99

100

101
    /**
102
     * Sets the optional prefix string for the temp file.
103
     *
104
     * @param string $prefix string to prepend to generated string
105
     */
106 0
    public function setPrefix($prefix)
107
    {
108 0
        $this->prefix = $prefix;
109
    }
110

111

112
    /**
113
     * Sets the optional suffix string for the temp file.
114
     *
115
     * @param string $suffix suffix including any "." , e.g ".xml"
116
     */
117 0
    public function setSuffix($suffix)
118
    {
119 0
        $this->suffix = $suffix;
120
    }
121

122
    /**
123
     * Set whether the tempfile created by this task should be set
124
     * for deletion on normal VM exit.
125
     *
126
     * @param boolean $deleteOnExit boolean flag.
127
     */
128 0
    public function setDeleteOnExit($deleteOnExit)
129
    {
130 0
        $this->deleteOnExit = $deleteOnExit;
131
    }
132

133
    /**
134
     * Learn whether deleteOnExit is set for this tempfile task.
135
     *
136
     * @return boolean deleteOnExit flag.
137
     */
138 0
    public function isDeleteOnExit()
139
    {
140 0
        return $this->deleteOnExit;
141
    }
142

143
    /**
144
     * If set the file is actually created, if not just a name is created.
145
     *
146
     * @param boolean $createFile boolean flag.
147
     */
148 0
    public function setCreateFile($createFile)
149
    {
150 0
        $this->createFile = $createFile;
151
    }
152

153
    /**
154
     * Learn whether createFile flag is set for this tempFile task.
155
     *
156
     * @return boolean the createFile flag.
157
     */
158 0
    public function isCreateFile()
159
    {
160 0
        return $this->createFile;
161
    }
162

163
    /**
164
     * Creates the temporary file.
165
     *
166
     * @throws BuildException if something goes wrong with the build
167
     */
168 1
    public function main()
169
    {
170 1
        if ($this->property === '') {
171 0
            throw new BuildException('no property specified');
172
        }
173 1
        if ($this->destDir === null) {
174 0
            $this->destDir = $this->getProject()->resolveFile('.');
175
        }
176 1
        $fu = new FileUtils();
177 1
        $tmpFile = $fu->createTempFile(
178 1
            $this->prefix,
179 1
            $this->suffix,
180 1
            $this->destDir,
181 1
            $this->deleteOnExit,
182 1
            $this->createFile
183
        );
184 1
        $this->getProject()->setNewProperty($this->property, (string) $tmpFile);
185
    }
186
}

Read our documentation on viewing source code .

Loading