|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------------+ |
|
4
|
|
|
// | This file is part of the Agavi package. | |
|
5
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
|
6
|
|
|
// | | |
|
7
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
|
8
|
|
|
// | file that was distributed with this source code. You can also view the | |
|
9
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|
10
|
|
|
// | vi: set noexpandtab: | |
|
11
|
|
|
// | Local Variables: | |
|
12
|
|
|
// | indent-tabs-mode: t | |
|
13
|
|
|
// | End: | |
|
14
|
|
|
// +---------------------------------------------------------------------------+ |
|
15
|
|
|
|
|
16
|
|
|
require_once(__DIR__ . '/AgaviTask.php'); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Writes properties to a given file. |
|
20
|
|
|
* |
|
21
|
|
|
* @package agavi |
|
22
|
|
|
* @subpackage build |
|
23
|
|
|
* |
|
24
|
|
|
* @author Noah Fontes <[email protected]> |
|
25
|
|
|
* @copyright Authors |
|
26
|
|
|
* @copyright The Agavi Project |
|
27
|
|
|
* |
|
28
|
|
|
* @since 1.0.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @version $Id$ |
|
31
|
|
|
*/ |
|
32
|
|
|
class WritepropertiesTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
protected $file = null; |
|
35
|
|
|
protected $update = false; |
|
36
|
|
|
protected $properties = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets the file that this property will modify. |
|
40
|
|
|
* |
|
41
|
|
|
* @param PhingFile The file to modify. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setFile(PhingFile $file) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->file = $file; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets whether this task will overwrite or update the file. |
|
50
|
|
|
* |
|
51
|
|
|
* @param boolean Whether the file should be updated or replaced. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setUpdate($update) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->update = StringHelper::booleanValue($update); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Creates a new property to be added to the property list. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function createProperty() |
|
62
|
|
|
{ |
|
63
|
|
|
$property = new AgaviPropertyType(); |
|
64
|
|
|
$this->properties[] = $property; |
|
65
|
|
|
return $property; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Executes the task. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function main() |
|
72
|
|
|
{ |
|
73
|
|
|
if($this->file === null) { |
|
74
|
|
|
throw new \Agavi\Build\Exception\BuildException('The file attribute must be set'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$properties = new Properties(); |
|
78
|
|
|
if($this->update === true) { |
|
79
|
|
|
/* Load existing properties. */ |
|
80
|
|
|
try { |
|
81
|
|
|
$properties->load($this->file); |
|
82
|
|
|
} |
|
83
|
|
|
catch(IOException $ioe) { |
|
84
|
|
|
/* File doesn't exist or isn't readable, so don't worry here. */ |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/* Add new properties. */ |
|
89
|
|
|
foreach($this->properties as $property) { |
|
90
|
|
|
foreach($property->resolve()->getProperties() as $name => $value) { |
|
91
|
|
|
$properties->setProperty($name, $value); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$properties->store($this->file, 'Automatically-updated properties file ' . |
|
96
|
|
|
'generated by the Agavi write-properties task'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
?> |
|
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.