1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------------+ |
4
|
|
|
// | This file is part of the Agavi package. | |
5
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
6
|
|
|
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr. | |
7
|
|
|
// | | |
8
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
9
|
|
|
// | file that was distributed with this source code. You can also view the | |
10
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
11
|
|
|
// | vi: set noexpandtab: | |
12
|
|
|
// | Local Variables: | |
13
|
|
|
// | indent-tabs-mode: t | |
14
|
|
|
// | End: | |
15
|
|
|
// +---------------------------------------------------------------------------+ |
16
|
|
|
|
17
|
|
|
namespace Agavi\Config; |
18
|
|
|
|
19
|
|
|
use Agavi\Config\Util\Dom\XmlConfigDomElement; |
20
|
|
|
use Agavi\Exception\ParseException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ConfigParser parses XML files using XmlConfigParser, but returns |
24
|
|
|
* old-style ConfigValueHolders. |
25
|
|
|
* |
26
|
|
|
* @package agavi |
27
|
|
|
* @subpackage config |
28
|
|
|
* |
29
|
|
|
* @author David Zülke <[email protected]> |
30
|
|
|
* @copyright Authors |
31
|
|
|
* @copyright The Agavi Project |
32
|
|
|
* |
33
|
|
|
* @since 0.11.0 |
34
|
|
|
* |
35
|
|
|
* @deprecated Superseded by XmlConfigParser, will be removed in Agavi 1.1 |
36
|
|
|
* |
37
|
|
|
* @version $Id$ |
38
|
|
|
*/ |
39
|
|
|
class ConfigParser |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var string The encoding of the DOMDocument |
43
|
|
|
*/ |
44
|
|
|
protected $encoding = 'utf-8'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string The filesystem path to the configuration file. |
48
|
|
|
*/ |
49
|
|
|
protected $config = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $config An absolute filesystem path to a configuration file. |
53
|
|
|
* @param array $validationFile An associative array of validation information. |
54
|
|
|
* |
55
|
|
|
* @return ConfigValueHolder The data handlers use to perform tasks. |
56
|
|
|
* |
57
|
|
|
* @author David Zülke <[email protected]> |
58
|
|
|
* @since 0.11.0 |
59
|
|
|
*/ |
60
|
|
|
public function parse($config, $validationFile = null) |
61
|
|
|
{ |
62
|
|
|
// copy path in case convertEncoding() needs to complain about a missing ICONV extension |
63
|
|
|
$this->config = $config; |
64
|
|
|
|
65
|
|
|
$parser = new XmlConfigParser($config, Config::get('core.environment'), null); |
66
|
|
|
|
67
|
|
|
$validation = array( |
68
|
|
|
XmlConfigParser::STEP_TRANSFORMATIONS_BEFORE => array(), |
69
|
|
|
XmlConfigParser::STEP_TRANSFORMATIONS_AFTER => array( |
70
|
|
|
XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA => array(), |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
if ($validationFile !== null) { |
74
|
|
|
$validation[XmlConfigParser::STEP_TRANSFORMATIONS_AFTER][XmlConfigParser::VALIDATION_TYPE_XMLSCHEMA][] = $validationFile; |
75
|
|
|
} |
76
|
|
|
$doc = $parser->execute(array(), $validation); |
77
|
|
|
|
78
|
|
|
// remember encoding for convertEncoding() |
79
|
|
|
$this->encoding = strtolower($doc->encoding); |
80
|
|
|
|
81
|
|
|
$rootRes = new ConfigValueHolder(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
if ($doc->documentElement) { |
84
|
|
|
$this->parseNodes(array($doc->documentElement), $rootRes); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $rootRes; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Iterates through a list of nodes and stores to each node in the |
92
|
|
|
* ConfigValueHolder |
93
|
|
|
* |
94
|
|
|
* @param mixed $nodes An array or an object that can be iterated over |
95
|
|
|
* @param ConfigValueHolder $parentVh The storage for the info from the nodes |
96
|
|
|
* @param bool $isSingular Whether this list is the singular form of the parent node |
97
|
|
|
* |
98
|
|
|
* @author David Zülke <[email protected]> |
99
|
|
|
* @author Dominik del Bondio <[email protected]> |
100
|
|
|
* @since 0.11.0 |
101
|
|
|
*/ |
102
|
|
|
protected function parseNodes($nodes, ConfigValueHolder $parentVh, $isSingular = false) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
/** @var XmlConfigDomElement $node */ |
105
|
|
|
foreach ($nodes as $node) { |
106
|
|
|
if ($node->nodeType == XML_ELEMENT_NODE && (!$node->namespaceURI || $node->namespaceURI == XmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_0_11)) { |
107
|
|
|
$vh = new ConfigValueHolder(); |
|
|
|
|
108
|
|
|
$nodeName = $this->convertEncoding($node->localName); |
109
|
|
|
$vh->setName($nodeName); |
110
|
|
|
$parentVh->addChildren($nodeName, $vh); |
111
|
|
|
|
112
|
|
|
foreach ($node->attributes as $attribute) { |
113
|
|
|
if ((!$attribute->namespaceURI || $attribute->namespaceURI == XmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_0_11)) { |
114
|
|
|
$vh->setAttribute($this->convertEncoding($attribute->localName), $this->convertEncoding($attribute->nodeValue)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// there are no child nodes so we set the node text contents as the value for the valueholder |
119
|
|
|
if ($node->getElementsByTagName('*')->length == 0) { |
120
|
|
|
$vh->setValue($this->convertEncoding($node->nodeValue)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($node->hasChildNodes()) { |
124
|
|
|
$this->parseNodes($node->childNodes, $vh); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Handle encoding for a value, i.e. translate from UTF-8 if necessary. |
132
|
|
|
* |
133
|
|
|
* @param string $value A UTF-8 string value from the DomDocument. |
134
|
|
|
* |
135
|
|
|
* @return string A value in the correct encoding of the parsed document. |
136
|
|
|
* |
137
|
|
|
* @author David Zülke <[email protected]> |
138
|
|
|
* @since 0.11.0 |
139
|
|
|
*/ |
140
|
|
|
protected function convertEncoding($value) |
141
|
|
|
{ |
142
|
|
|
if ($this->encoding == 'utf-8') { |
143
|
|
|
return $value; |
144
|
|
|
} elseif ($this->encoding == 'iso-8859-1') { |
145
|
|
|
return utf8_decode($value); |
146
|
|
|
} elseif (function_exists('iconv')) { |
147
|
|
|
return iconv('UTF-8', $this->encoding, $value); |
148
|
|
|
} else { |
149
|
|
|
throw new ParseException('No iconv module available, configuration file "' . $this->config . '" with input encoding "' . $this->encoding . '" cannot be parsed.'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.