|
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 XPath-defined configuration values to an Agavi configuration 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 WriteconfigurationTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
protected $file = null; |
|
35
|
|
|
protected $path = null; |
|
36
|
|
|
protected $value = null; |
|
37
|
|
|
protected $namespace = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Sets the file to modify. |
|
41
|
|
|
* |
|
42
|
|
|
* @param PhingFile The file to modify. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setFile(PhingFile $file) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->file = $file; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Sets the XPath path to search for in the file. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string The search path. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setPath($path) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->path = $path; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Sets the new value for the configuration element. |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed The new value. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setValue($value) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->value = $value; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets the document namespace for this task. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string The document namespace. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setNamespace($namespace) { |
|
75
|
|
|
$this->namespace = $namespace; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Executes this task. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function main() |
|
82
|
|
|
{ |
|
83
|
|
|
if($this->file === null) { |
|
84
|
|
|
throw new \Agavi\Build\Exception\BuildException('The file attribute must be specified'); |
|
85
|
|
|
} |
|
86
|
|
|
if($this->path === null) { |
|
87
|
|
|
throw new \Agavi\Build\Exception\BuildException('The path attribute must be specified'); |
|
88
|
|
|
} |
|
89
|
|
|
if($this->value === null) { |
|
90
|
|
|
throw new \Agavi\Build\Exception\BuildException('The value attribute must be specified'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$document = new DOMDocument(); |
|
94
|
|
|
$document->preserveWhiteSpace = true; |
|
95
|
|
|
$document->load($this->file->getAbsolutePath()); |
|
96
|
|
|
|
|
97
|
|
|
$path = new DOMXPath($document); |
|
98
|
|
|
$path->registerNamespace('envelope', 'http://agavi.org/agavi/config/global/envelope/1.0'); |
|
99
|
|
|
$path->registerNamespace('envelope10', 'http://agavi.org/agavi/config/global/envelope/1.0'); |
|
100
|
|
|
$path->registerNamespace('envelope11', 'http://agavi.org/agavi/config/global/envelope/1.1'); |
|
101
|
|
|
if($this->namespace !== null) { |
|
102
|
|
|
$path->registerNamespace('document', $this->namespace); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$entries = $path->query($this->path); |
|
106
|
|
|
foreach($entries as $entry) { |
|
107
|
|
|
$entry->nodeValue = (string)$this->value; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$document->save($this->file->getAbsolutePath()); |
|
111
|
|
|
|
|
112
|
|
|
$this->log(sprintf('Writing configuration file %s with new data for %s (%s)', |
|
113
|
|
|
$this->file->getAbsolutePath(), $this->path, $this->value), Project::MSG_INFO); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
?> |
|
|
|
|
|
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.