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
|
|
|
namespace Phing\Task\System; |
21
|
|
|
|
22
|
|
|
use Phing\Exception\BuildException; |
23
|
|
|
use Phing\Io\File; |
24
|
|
|
use Phing\Io\FileReader; |
25
|
|
|
use Phing\Io\FileUtils; |
26
|
|
|
use Phing\Io\IOException; |
27
|
|
|
use Phing\Project; |
28
|
|
|
use Phing\Task; |
29
|
|
|
use Phing\Type\Element\FilterChainAware; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* LoadFileTask |
33
|
|
|
* |
34
|
|
|
* Loads a (text) file and stores the contents in a property. |
35
|
|
|
* Supports filterchains. |
36
|
|
|
* |
37
|
|
|
* @author Michiel Rook <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class LoadFileTask extends Task |
40
|
|
|
{ |
41
|
|
|
use FilterChainAware; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* File to read |
45
|
|
|
* |
46
|
|
|
* @var File file |
47
|
|
|
*/ |
48
|
|
|
private $file; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Property to be set |
52
|
|
|
* |
53
|
|
|
* @var string $property |
54
|
|
|
*/ |
55
|
|
|
private $property; |
56
|
|
|
|
57
|
|
|
private $failOnError = true; |
58
|
|
|
|
59
|
|
|
private $quiet = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param bool $fail |
63
|
|
|
*/ |
64
|
|
|
public function setFailOnError($fail) |
65
|
|
|
{ |
66
|
|
|
$this->failOnError = $fail; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param bool $quiet |
71
|
|
|
*/ |
72
|
|
|
public function setQuiet($quiet) |
73
|
|
|
{ |
74
|
|
|
$this->quiet = $quiet; |
75
|
|
|
if ($quiet) { |
76
|
|
|
$this->failOnError = false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set file to read |
82
|
|
|
*/ |
83
|
3 |
|
public function setFile(File $file) |
84
|
|
|
{ |
85
|
3 |
|
$this->file = $file; |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Convenience setter to maintain Ant compatibility (@see setFile()) |
90
|
|
|
*/ |
91
|
|
|
public function setSrcFile(File $srcFile) |
92
|
|
|
{ |
93
|
|
|
$this->file = $srcFile; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set name of property to be set |
98
|
|
|
* |
99
|
|
|
* @param string $property |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
3 |
|
public function setProperty($property) |
103
|
|
|
{ |
104
|
3 |
|
$this->property = $property; |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Main method |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
* @throws BuildException |
112
|
|
|
*/ |
113
|
3 |
|
public function main() |
114
|
|
|
{ |
115
|
3 |
|
if (empty($this->file)) { |
116
|
|
|
throw new BuildException("Attribute 'file' required", $this->getLocation()); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
if (empty($this->property)) { |
120
|
|
|
throw new BuildException("Attribute 'property' required", $this->getLocation()); |
121
|
|
|
} |
122
|
3 |
|
if ($this->quiet && $this->failOnError) { |
123
|
|
|
throw new BuildException("quiet and failonerror cannot both be set to true"); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
try { |
127
|
3 |
|
if (is_string($this->file)) { |
|
|
|
|
128
|
|
|
$this->file = new File($this->file); |
129
|
|
|
} |
130
|
3 |
|
if (!$this->file->exists()) { |
131
|
|
|
$message = (string) $this->file . ' doesn\'t exist'; |
132
|
|
|
if ($this->failOnError) { |
133
|
|
|
throw new BuildException($message); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->log($message, $this->quiet ? Project::MSG_WARN : Project::MSG_ERR); |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
$this->log("loading " . (string) $this->file . " into property " . $this->property, Project::MSG_VERBOSE); |
141
|
|
|
// read file (through filterchains) |
142
|
3 |
|
$contents = ""; |
143
|
|
|
|
144
|
3 |
|
if ($this->file->length() > 0) { |
145
|
3 |
|
$reader = FileUtils::getChainedReader(new FileReader($this->file), $this->filterChains, $this->project); |
146
|
3 |
|
while (-1 !== ($buffer = $reader->read())) { |
147
|
3 |
|
$contents .= $buffer; |
148
|
|
|
} |
149
|
3 |
|
$reader->close(); |
150
|
|
|
} else { |
151
|
|
|
$this->log( |
152
|
|
|
"Do not set property " . $this->property . " as its length is 0.", |
153
|
|
|
$this->quiet ? Project::MSG_VERBOSE : Project::MSG_INFO |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// publish as property |
158
|
3 |
|
if ($contents !== '') { |
159
|
3 |
|
$this->project->setNewProperty($this->property, $contents); |
160
|
3 |
|
$this->log("loaded " . strlen($contents) . " characters", Project::MSG_VERBOSE); |
161
|
3 |
|
$this->log($this->property . " := " . $contents, Project::MSG_DEBUG); |
162
|
|
|
} |
163
|
|
|
} catch (IOException $ioe) { |
164
|
|
|
$message = "Unable to load resource: " . $ioe->getMessage(); |
165
|
|
|
if ($this->failOnError) { |
166
|
|
|
throw new BuildException($message, $ioe, $this->getLocation()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR); |
170
|
|
|
} catch (BuildException $be) { |
171
|
|
|
if ($this->failOnError) { |
172
|
|
|
throw $be; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR); |
176
|
|
|
} |
177
|
3 |
|
} |
178
|
|
|
} |
179
|
|
|
|