-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFileTask.php
More file actions
465 lines (430 loc) · 12.3 KB
/
Copy pathIniFileTask.php
File metadata and controls
465 lines (430 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
namespace Phing\Task\Ext\IniFile;
use Phing\Exception\BuildException;
use Phing\Project;
use Phing\Task;
use Phing\Util\StringHelper;
use RuntimeException;
use Throwable;
/**
* InifileTask
*
* @author Ken Guest <ken@linux.ie>
*/
class IniFileTask extends Task
{
/**
* Source file
*
* @var string|null
*/
protected $source;
/**
* Dest file
*
* @var string|null
*/
protected $dest;
/**
* Whether to halt phing on error.
*
* @var bool
*/
protected $haltonerror = false;
/**
* Gets
*
* @var array
*/
protected $gets = [];
/**
* Sets
*
* @var array
*/
protected $sets = [];
/**
* Removals
*
* @var array
*/
protected $removals = [];
/**
* IniFileConfig instance
*
* @var IniFileConfig
*/
protected $ini;
/**
* Taskname for logger
*
* @var string
*/
protected $taskName = 'IniFile';
/**
* Verbose
*
* @var bool
*/
protected $verbose = false;
/**
* Check file to be read from
*
* @param string|null $readFile Filename
*
* @return bool
*/
public function checkReadFile(?string $readFile): bool
{
if (null === $readFile) {
return false;
}
if (!file_exists($readFile)) {
$msg = "$readFile does not exist.";
if ($this->haltonerror) {
throw new BuildException($msg);
}
$this->log($msg, Project::MSG_ERR);
return false;
}
if (!is_readable($readFile)) {
$msg = "$readFile is not readable.";
if ($this->haltonerror) {
throw new BuildException($msg);
}
$this->log($msg, Project::MSG_ERR);
return false;
}
$this->ini->read($readFile);
$this->log("Read from $readFile");
return true;
}
/**
* Check file to write to
*
* @param string $writeFile Filename
*
* @return bool
*/
public function checkWriteFile(string $writeFile): bool
{
if (file_exists($writeFile) && !is_writable($writeFile)) {
$msg = "$writeFile is not writable";
if ($this->haltonerror) {
throw new BuildException($msg);
}
$this->log($msg, Project::MSG_ERR);
return false;
}
return true;
}
/**
* The main entry point method.
*/
public function main(): void
{
$this->ini = new IniFileConfig();
$readFile = null;
$writeFile = null;
if (null !== $this->source && null === $this->dest) {
$readFile = $this->source;
} elseif (null !== $this->dest && null === $this->source) {
$readFile = $this->dest;
} else {
$readFile = $this->source;
}
if (null !== $this->dest) {
$writeFile = $this->dest;
} elseif (null !== $this->source) {
$writeFile = $this->source;
} else {
$writeFile = $this->dest;
}
if ($readFile === null && $writeFile === null) {
$msg = "Neither source nor dest is set";
if ($this->haltonerror) {
throw new BuildException($msg);
}
$this->log($msg, Project::MSG_ERR);
return;
}
if (!$this->checkReadFile($readFile)) {
return;
}
if (!$this->checkWriteFile($writeFile)) {
return;
}
$this->enumerateGets();
$this->enumerateSets();
$this->enumerateRemoves();
if (count($this->sets) || count($this->removals)) {
try {
$this->ini->write($writeFile);
$this->log("Wrote to $writeFile");
} catch (Throwable $ex) {
$msg = $ex->getMessage();
if ($this->haltonerror) {
throw new BuildException($msg);
}
$this->log($msg, Project::MSG_ERR);
}
}
}
/**
* Work through all Get commands.
*
* @return void
*/
public function enumerateGets(): void
{
foreach ($this->gets as $get) {
$outProperty = $get->getOutputProperty();
$property = $get->getProperty();
$section = $get->getSection();
$value = '';
if ($property === null) {
throw new BuildException("property must be set");
}
if ($outProperty === null) {
throw new BuildException("outputproperty must be set");
}
if ($section === null) {
throw new BuildException("section must be set");
}
try {
$value = $this->ini->get($section, $property);
} catch (RuntimeException $ex) {
$this->logDebugOrMore(
sprintf(
'%s: section = %s; key = %s',
$ex->getMessage(),
$section,
$property
)
);
} finally {
if ($value === '') {
$value = $get->getDefault();
}
}
$project = $this->getProject();
$project->setProperty($outProperty, $value);
$this->logDebugOrMore(
sprintf(
'Set property %s to value \'%s\' read from key %s in section %s',
$outProperty,
$value,
$property,
$section
)
);
}
}
/**
* Work through all Set commands.
*
* @return void
*/
public function enumerateSets(): void
{
foreach ($this->sets as $set) {
$value = $set->getValue();
$key = $set->getProperty();
$section = $set->getSection();
$operation = $set->getOperation();
if ($value !== null) {
try {
$this->ini->set($section, $key, $value);
$this->logDebugOrMore("[$section] $key set to $value");
} catch (Throwable $ex) {
$this->log(
"Error setting value for section '" . $section .
"', key '" . $key . "'",
Project::MSG_ERR
);
$this->logDebugOrMore($ex->getMessage());
}
} elseif ($operation !== null) {
$v = $this->ini->get($section, $key);
// value might be wrapped in quotes with a semicolon at the end
if (!is_numeric($v)) {
if (preg_match('/^"(\d*)";?$/', $v, $match)) {
$v = $match[1];
} elseif (preg_match("/^'(\d*)';?$/", $v, $match)) {
$v = $match[1];
} else {
$this->log(
"Value $v is not numeric. Skipping $operation operation."
);
continue;
}
}
if ($operation === '+') {
++$v;
} elseif ($operation === '-') {
--$v;
} elseif (($operation !== '-') && ($operation !== '+')) {
$msg = "Unrecognised operation $operation";
if ($this->haltonerror) {
throw new BuildException($msg);
}
$this->log($msg, Project::MSG_ERR);
}
try {
$this->ini->set($section, $key, $v);
$this->logDebugOrMore("[$section] $key set to $v");
} catch (Throwable $ex) {
$this->log(
"Error setting value for section '" . $section .
"', key '" . $key . "'"
);
$this->logDebugOrMore($ex->getMessage());
}
} else {
$this->log(
"Set: value and operation are both not set",
Project::MSG_ERR
);
}
}
}
/**
* Work through all Remove commands.
*
* @return void
*/
public function enumerateRemoves(): void
{
foreach ($this->removals as $remove) {
$key = $remove->getProperty();
$section = $remove->getSection();
if ($section === '') {
$this->log(
"Remove: section must be set",
Project::MSG_ERR
);
continue;
}
$this->ini->remove($section, $key);
if (($section !== '') && ($key !== '')) {
$this->logDebugOrMore(
"$key in section [$section] has been removed."
);
} elseif (($section !== '') && ($key === '')) {
$this->logDebugOrMore("[$section] has been removed.");
}
}
}
/**
* Set Source property
*
* @param string $source Name of originating ini file to parse
*
* @return void
*/
public function setSource(string $source): void
{
$this->source = $source;
}
/**
* Set Dest property
*
* @param string $dest Destination filename to write ini contents to.
*
* @return void
*/
public function setDest(string $dest): void
{
$this->dest = $dest;
}
/**
* Set haltonerror attribute.
*
* @param string $halt 'yes', or '1' to halt.
*
* @return void
*/
public function setHaltonerror(string $halt): void
{
$this->haltonerror = StringHelper::booleanValue($halt);
}
/**
* Set verbose attribute.
*
* Screech like a Camaar fishwife...
*
* @param boolean $verbose Verbose?
*
* @return void
*/
public function setVerbose(bool $verbose): void
{
$this->verbose = StringHelper::booleanValue($verbose);
}
/**
* Create a Get method
*
* @return IniFileGet
*/
public function createGet(): IniFileGet
{
$get = new IniFileGet();
$this->gets[] = $get;
return $get;
}
/**
* Create a Set method
*
* @return IniFileSet
*/
public function createSet(): IniFileSet
{
$set = new IniFileSet();
$this->sets[] = $set;
return $set;
}
/**
* Create a Remove method
*
* @return IniFileRemove
*/
public function createRemove(): IniFileRemove
{
$remove = new IniFileRemove();
$this->removals[] = $remove;
return $remove;
}
/**
* Log message at Debug level. If verbose prop is set, also log it at normal
*
* @param string $message Message to log
*
* @return bool False if message is only logged at debug level.
*/
public function logDebugOrMore(string $message): bool
{
$this->log($message, Project::MSG_DEBUG);
if ($this->verbose) {
$this->log($message);
return true;
}
return false;
}
}