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\Filter; |
22
|
|
|
|
23
|
|
|
use Phing\Exception\BuildException; |
24
|
|
|
use Phing\Io\BufferedReader; |
25
|
|
|
use Phing\Io\File; |
26
|
|
|
use Phing\Io\FileReader; |
27
|
|
|
use Phing\Io\IOException; |
28
|
|
|
use Phing\Io\Reader; |
29
|
|
|
use Phing\Type\Parameter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Concats a file before and/or after the file. |
33
|
|
|
* |
34
|
|
|
* Example: |
35
|
|
|
* ``` |
36
|
|
|
* <copy todir="build"> |
37
|
|
|
* <fileset dir="src" includes="*.php"/> |
38
|
|
|
* <filterchain> |
39
|
|
|
* <concatfilter prepend="license.txt"/> |
40
|
|
|
* </filterchain> |
41
|
|
|
* </copy> |
42
|
|
|
* ``` |
43
|
|
|
* |
44
|
|
|
* Copies all php sources from `src` to `build` and adds the |
45
|
|
|
* content of `license.txt` add the beginning of each |
46
|
|
|
* file. |
47
|
|
|
* |
48
|
|
|
* @author Siad.ardroumli <[email protected]> |
49
|
|
|
*/ |
50
|
|
|
class ConcatFilter extends BaseParamFilterReader implements ChainableReader |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* File to add before the content. |
54
|
|
|
* |
55
|
|
|
* @var File |
56
|
|
|
*/ |
57
|
|
|
private $prepend; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* File to add after the content. |
61
|
|
|
* |
62
|
|
|
* @var File|string |
63
|
|
|
*/ |
64
|
|
|
private $append; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Reader for prepend-file. |
68
|
|
|
* |
69
|
|
|
* @var BufferedReader |
70
|
|
|
*/ |
71
|
|
|
private $prependReader; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Reader for append-file. |
75
|
|
|
* |
76
|
|
|
* @var BufferedReader |
77
|
|
|
*/ |
78
|
|
|
private $appendReader; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Reader $in |
82
|
|
|
*/ |
83
|
1 |
|
public function __construct(Reader $in = null) |
84
|
|
|
{ |
85
|
1 |
|
parent::__construct($in); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the next character in the filtered stream. If the desired |
90
|
|
|
* number of lines have already been read, the resulting stream is |
91
|
|
|
* effectively at an end. Otherwise, the next character from the |
92
|
|
|
* underlying stream is read and returned. |
93
|
|
|
* |
94
|
|
|
* @param int $len |
95
|
|
|
* |
96
|
|
|
* @throws IOException if the underlying stream throws an IOException |
97
|
|
|
* during reading |
98
|
|
|
* @throws BuildException |
99
|
|
|
* @throws \InvalidArgumentException |
100
|
|
|
* |
101
|
|
|
* @return int|string the next character in the resulting stream, or -1 |
102
|
|
|
* if the end of the resulting stream has been reached |
103
|
|
|
*/ |
104
|
1 |
|
public function read($len = null) |
105
|
|
|
{ |
106
|
|
|
// do the "singleton" initialization |
107
|
1 |
|
if (!$this->getInitialized()) { |
108
|
1 |
|
$this->initialize(); |
109
|
1 |
|
$this->setInitialized(true); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
$ch = -1; |
113
|
|
|
|
114
|
|
|
// The readers return -1 if they end. So simply read the "prepend" |
115
|
|
|
// after that the "content" and at the end the "append" file. |
116
|
1 |
|
if (null !== $this->prependReader) { |
117
|
1 |
|
$ch = $this->prependReader->read(); |
118
|
1 |
|
if (-1 === $ch) { |
119
|
|
|
// I am the only one so I have to close the reader |
120
|
1 |
|
$this->prependReader->close(); |
121
|
1 |
|
$this->prependReader = null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
1 |
|
if (-1 === $ch) { |
125
|
1 |
|
$ch = parent::read(); |
126
|
|
|
} |
127
|
1 |
|
if (-1 === $ch && null !== $this->appendReader) { |
128
|
|
|
$ch = $this->appendReader->read(); |
129
|
|
|
if (-1 === $ch) { |
130
|
|
|
// I am the only one so I have to close the reader |
131
|
|
|
$this->appendReader->close(); |
132
|
|
|
$this->appendReader = null; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
return $ch; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Creates a new ConcatReader using the passed in |
141
|
|
|
* Reader for instantiation. |
142
|
|
|
* |
143
|
|
|
* @param Reader $reader A Reader object providing the underlying stream. |
144
|
|
|
* Must not be <code>null</code>. |
145
|
|
|
* |
146
|
|
|
* @throws IOException |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
* |
149
|
|
|
* @return ConcatFilter a new filter based on this configuration, but filtering |
150
|
|
|
* the specified reader |
151
|
|
|
*/ |
152
|
1 |
|
public function chain(Reader $reader): Reader |
153
|
|
|
{ |
154
|
1 |
|
$newFilter = new ConcatFilter($reader); |
155
|
1 |
|
$newFilter->setProject($this->getProject()); |
156
|
1 |
|
$newFilter->setPrepend($this->getPrepend()); |
157
|
1 |
|
$newFilter->setAppend($this->getAppend()); |
158
|
|
|
|
159
|
1 |
|
return $newFilter; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns `prepend` attribute. |
164
|
|
|
* |
165
|
|
|
* @return File prepend attribute |
166
|
|
|
*/ |
167
|
1 |
|
public function getPrepend() |
168
|
|
|
{ |
169
|
1 |
|
return $this->prepend; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sets `prepend` attribute. |
174
|
|
|
* |
175
|
|
|
* @param File|string $prepend prepend new value |
176
|
|
|
* |
177
|
|
|
* @throws IOException |
178
|
|
|
* @throws \InvalidArgumentException |
179
|
|
|
*/ |
180
|
1 |
|
public function setPrepend($prepend) |
181
|
|
|
{ |
182
|
1 |
|
if ($prepend instanceof File) { |
183
|
1 |
|
$this->prepend = $prepend; |
184
|
|
|
|
185
|
1 |
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
$this->prepend = new File($prepend); |
189
|
1 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns `append` attribute. |
193
|
|
|
* |
194
|
|
|
* @return File|string append attribute |
195
|
|
|
*/ |
196
|
1 |
|
public function getAppend() |
197
|
|
|
{ |
198
|
1 |
|
return $this->append; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Sets `append` attribute. |
203
|
|
|
* |
204
|
|
|
* @param File|string $append append new value |
205
|
|
|
*/ |
206
|
1 |
|
public function setAppend($append) |
207
|
|
|
{ |
208
|
1 |
|
$this->append = $append; |
209
|
1 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Scans the parameters list for the "lines" parameter and uses |
213
|
|
|
* it to set the number of lines to be returned in the filtered stream. |
214
|
|
|
* also scan for skip parameter. |
215
|
|
|
* |
216
|
|
|
* @throws IOException |
217
|
|
|
* @throws \InvalidArgumentException |
218
|
|
|
*/ |
219
|
1 |
|
private function initialize() |
220
|
|
|
{ |
221
|
|
|
// get parameters |
222
|
1 |
|
$params = $this->getParameters(); |
223
|
1 |
|
if (null !== $params) { |
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @var Parameter $param |
226
|
|
|
*/ |
227
|
1 |
|
foreach ($params as $param) { |
228
|
|
|
if ('prepend' === $param->getName()) { |
229
|
|
|
$this->setPrepend(new File($param->getValue())); |
230
|
|
|
|
231
|
|
|
continue; |
232
|
|
|
} |
233
|
|
|
if ('append' === $param->getName()) { |
234
|
|
|
$this->setAppend(new File($param->getValue())); |
235
|
|
|
|
236
|
|
|
continue; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
1 |
|
if (null !== $this->prepend) { |
241
|
1 |
|
if (!$this->prepend->isAbsolute()) { |
242
|
1 |
|
$this->prepend = new File($this->getProject()->getBasedir(), $this->prepend->getPath()); |
243
|
|
|
} |
244
|
1 |
|
$this->prependReader = new BufferedReader(new FileReader($this->prepend)); |
245
|
|
|
} |
246
|
1 |
|
if (null !== $this->append) { |
247
|
|
|
if (!$this->append->isAbsolute()) { |
248
|
|
|
$this->append = new File($this->getProject()->getBasedir(), $this->append->getPath()); |
249
|
|
|
} |
250
|
|
|
$this->appendReader = new BufferedReader(new FileReader($this->append)); |
251
|
|
|
} |
252
|
1 |
|
} |
253
|
|
|
} |
254
|
|
|
|