1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Task\System; |
22
|
|
|
|
23
|
|
|
use Phing\Exception\BuildException; |
24
|
|
|
use Phing\Io\File; |
25
|
|
|
use Phing\Io\FileUtils; |
26
|
|
|
use Phing\Task; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* This task sets a property to the name of a temporary file. |
30
|
|
|
* Unlike {@link File::createTempFile()}, this task does not (by default) actually create the |
31
|
|
|
* temporary file, but it does guarantee that the file did not |
32
|
|
|
* exist when the task was executed. |
33
|
|
|
* |
34
|
|
|
* Examples: |
35
|
|
|
* |
36
|
|
|
* `<tempfile property="temp.file" />` |
37
|
|
|
* |
38
|
|
|
* create a temporary file |
39
|
|
|
* |
40
|
|
|
* `<tempfile property="temp.file" suffix=".xml" />` |
41
|
|
|
* |
42
|
|
|
* create a temporary file with the .xml suffix. |
43
|
|
|
* |
44
|
|
|
* `<tempfile property="temp.file" destDir="build"/>` |
45
|
|
|
* |
46
|
|
|
* create a temp file in the build subdir |
47
|
|
|
* |
48
|
|
|
* @author Siad Ardroumli <[email protected]> |
49
|
|
|
*/ |
50
|
|
|
class TempFile extends Task |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Name of property to set. |
54
|
|
|
*/ |
55
|
|
|
private $property = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Directory to create the file in. Can be null. |
59
|
|
|
*/ |
60
|
|
|
private $destDir; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Prefix for the file. |
64
|
|
|
*/ |
65
|
|
|
private $prefix; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Suffix for the file. |
69
|
|
|
*/ |
70
|
|
|
private $suffix = ''; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* deleteOnExit flag. |
74
|
|
|
*/ |
75
|
|
|
private $deleteOnExit; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* createFile flag. |
79
|
|
|
*/ |
80
|
|
|
private $createFile; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the property you wish to assign the temporary file to. |
84
|
|
|
* |
85
|
|
|
* @param string $property The property to set |
86
|
|
|
*/ |
87
|
2 |
|
public function setProperty($property) |
88
|
|
|
{ |
89
|
2 |
|
$this->property = $property; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the destination directory. If not set, |
94
|
|
|
* the basedir directory is used instead. |
95
|
|
|
* |
96
|
|
|
* @param File|string $destDir The new destDir value |
97
|
|
|
*/ |
98
|
1 |
|
public function setDestDir($destDir) |
99
|
|
|
{ |
100
|
1 |
|
if ($destDir instanceof File) { |
101
|
|
|
$this->destDir = $destDir; |
102
|
|
|
} else { |
103
|
1 |
|
$this->destDir = new File($destDir); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the optional prefix string for the temp file. |
109
|
|
|
* |
110
|
|
|
* @param string $prefix string to prepend to generated string |
111
|
|
|
*/ |
112
|
|
|
public function setPrefix($prefix) |
113
|
|
|
{ |
114
|
|
|
$this->prefix = $prefix; |
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 bool $deleteOnExit bool 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 bool 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 bool $createFile bool 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 bool 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
|
2 |
|
public function main() |
174
|
|
|
{ |
175
|
2 |
|
if ('' === $this->property) { |
176
|
|
|
throw new BuildException('no property specified'); |
177
|
|
|
} |
178
|
2 |
|
if (null === $this->destDir) { |
179
|
1 |
|
$this->destDir = $this->getProject()->resolveFile('.'); |
180
|
|
|
} |
181
|
2 |
|
$fu = new FileUtils(); |
182
|
2 |
|
$tmpFile = $fu->createTempFile( |
183
|
2 |
|
$this->prefix, |
184
|
2 |
|
$this->suffix, |
185
|
2 |
|
$this->destDir, |
186
|
2 |
|
$this->deleteOnExit, |
187
|
2 |
|
$this->createFile |
188
|
2 |
|
); |
189
|
2 |
|
$this->getProject()->setNewProperty($this->property, (string) $tmpFile); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|