Passed
Push — master ( b5ca42...8fe651 )
by Siad
05:05
created

TempFile::setSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
use Phing\Exception\BuildException;
21
use Phing\Io\FileUtils;
22
use Phing\Io\File;
23
use Phing\Task;
24
25
/**
26
 * This task sets a property to the name of a temporary file.
27
 * Unlike {@link File::createTempFile()}, this task does not (by default) actually create the
28
 * temporary file, but it does guarantee that the file did not
29
 * exist when the task was executed.
30
 *
31
 * Examples:
32
 *
33
 * `<tempfile property="temp.file" />`
34
 *
35
 * create a temporary file
36
 *
37
 * `<tempfile property="temp.file" suffix=".xml" />`
38
 *
39
 * create a temporary file with the .xml suffix.
40
 *
41
 * `<tempfile property="temp.file" destDir="build"/>`
42
 *
43
 * create a temp file in the build subdir
44
 *
45
 * @author  Siad Ardroumli <[email protected]>
46
 * @package phing.tasks.system
47
 */
48
class TempFile extends Task
49
{
50
    /**
51
     * Name of property to set.
52
     */
53
    private $property = '';
54
55
    /**
56
     * Directory to create the file in. Can be null.
57
     */
58
    private $destDir;
59
60
    /**
61
     * Prefix for the file.
62
     */
63
    private $prefix;
64
65
    /**
66
     * Suffix for the file.
67
     */
68
    private $suffix = '';
69
70
    /**
71
     * deleteOnExit flag
72
     */
73
    private $deleteOnExit;
74
75
    /**
76
     * createFile flag
77
     */
78
    private $createFile;
79
80
    /**
81
     * Sets the property you wish to assign the temporary file to.
82
     *
83
     * @param string $property The property to set
84
     */
85 1
    public function setProperty($property)
86
    {
87 1
        $this->property = $property;
88 1
    }
89
90
    /**
91
     * Sets the destination directory. If not set,
92
     * the basedir directory is used instead.
93
     *
94
     * @param string|File $destDir The new destDir value
95
     */
96 1
    public function setDestDir($destDir)
97
    {
98 1
        if ($destDir instanceof File) {
99
            $this->destDir = $destDir;
100
        } else {
101 1
            $this->destDir = new File($destDir);
102
        }
103 1
    }
104
105
106
    /**
107
     * Sets the optional prefix string for the temp file.
108
     *
109
     * @param string $prefix string to prepend to generated string
110
     */
111
    public function setPrefix($prefix)
112
    {
113
        $this->prefix = $prefix;
114
    }
115
116
117
    /**
118
     * Sets the optional suffix string for the temp file.
119
     *
120
     * @param string $suffix suffix including any "." , e.g ".xml"
121
     */
122
    public function setSuffix($suffix)
123
    {
124
        $this->suffix = $suffix;
125
    }
126
127
    /**
128
     * Set whether the tempfile created by this task should be set
129
     * for deletion on normal VM exit.
130
     *
131
     * @param boolean $deleteOnExit boolean flag.
132
     */
133
    public function setDeleteOnExit($deleteOnExit)
134
    {
135
        $this->deleteOnExit = $deleteOnExit;
136
    }
137
138
    /**
139
     * Learn whether deleteOnExit is set for this tempfile task.
140
     *
141
     * @return boolean deleteOnExit flag.
142
     */
143
    public function isDeleteOnExit()
144
    {
145
        return $this->deleteOnExit;
146
    }
147
148
    /**
149
     * If set the file is actually created, if not just a name is created.
150
     *
151
     * @param boolean $createFile boolean flag.
152
     */
153
    public function setCreateFile($createFile)
154
    {
155
        $this->createFile = $createFile;
156
    }
157
158
    /**
159
     * Learn whether createFile flag is set for this tempFile task.
160
     *
161
     * @return boolean the createFile flag.
162
     */
163
    public function isCreateFile()
164
    {
165
        return $this->createFile;
166
    }
167
168
    /**
169
     * Creates the temporary file.
170
     *
171
     * @throws BuildException if something goes wrong with the build
172
     */
173 1
    public function main()
174
    {
175 1
        if ($this->property === '') {
176
            throw new BuildException('no property specified');
177
        }
178 1
        if ($this->destDir === null) {
179
            $this->destDir = $this->getProject()->resolveFile('.');
180
        }
181 1
        $fu = new FileUtils();
182 1
        $tmpFile = $fu->createTempFile(
183 1
            $this->prefix,
184 1
            $this->suffix,
185 1
            $this->destDir,
186 1
            $this->deleteOnExit,
187 1
            $this->createFile
188
        );
189 1
        $this->getProject()->setNewProperty($this->property, (string) $tmpFile);
190 1
    }
191
}
192