| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: PropelConfiguration.php 1262 2009-10-26 20:54:39Z francois $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information please see
|
|
|
19 |
* <http://propel.phpdb.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* PropelConfiguration is a container for all Propel's configuration data.
|
|
|
24 |
*
|
|
|
25 |
* PropelConfiguration implements ArrayAccess interface so the configuration
|
|
|
26 |
* can be accessed as an array or using a simple getter and setter. The whole
|
|
|
27 |
* configuration can also be retrieved as a nested arrays, flat array or as a
|
|
|
28 |
* PropelConfiguration instance.
|
|
|
29 |
*
|
|
|
30 |
* @author Veikko Mäkinen <veikko@veikko.fi>
|
|
|
31 |
* @version $Revision: 1262 $
|
|
|
32 |
* @package propel
|
|
|
33 |
*/
|
|
|
34 |
class PropelConfiguration implements ArrayAccess
|
|
|
35 |
{
|
|
|
36 |
const TYPE_ARRAY = 1;
|
|
|
37 |
|
|
|
38 |
const TYPE_ARRAY_FLAT = 2;
|
|
|
39 |
|
|
|
40 |
const TYPE_OBJECT = 3;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @var array An array of parameters
|
|
|
44 |
*/
|
|
|
45 |
protected $parameters = array();
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Construct a new configuration container
|
|
|
49 |
*
|
|
|
50 |
* @param array $parameters
|
|
|
51 |
*/
|
|
|
52 |
public function __construct(array $parameters = array())
|
|
|
53 |
{
|
|
|
54 |
$this->parameters = $parameters;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* @see http://www.php.net/ArrayAccess
|
|
|
59 |
*/
|
|
|
60 |
public function offsetExists($offset)
|
|
|
61 |
{
|
|
|
62 |
return isset($this->parameter[$offset]) || array_key_exists($offset, $this->parameters);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @see http://www.php.net/ArrayAccess
|
|
|
67 |
*/
|
|
|
68 |
public function offsetSet($offset, $value)
|
|
|
69 |
{
|
|
|
70 |
$this->parameter[$offset] = $value;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* @see http://www.php.net/ArrayAccess
|
|
|
75 |
*/
|
|
|
76 |
public function offsetGet($offset)
|
|
|
77 |
{
|
|
|
78 |
return $this->parameters[$offset];
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* @see http://www.php.net/ArrayAccess
|
|
|
83 |
*/
|
|
|
84 |
public function offsetUnset($offset)
|
|
|
85 |
{
|
|
|
86 |
unset($this->parameters[$offset]);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Get parameter value from the container
|
|
|
91 |
*
|
|
|
92 |
* @param string $name Parameter name
|
|
|
93 |
* @param mixed $default Default value to be used if the
|
|
|
94 |
* requested value is not found
|
|
|
95 |
* @return mixed Parameter value or the default
|
|
|
96 |
*/
|
|
|
97 |
public function getParameter($name, $default = null)
|
|
|
98 |
{
|
|
|
99 |
$ret = $this->parameters;
|
|
|
100 |
$parts = explode('.', $name); //name.space.name
|
|
|
101 |
while ($part = array_shift($parts)) {
|
|
|
102 |
if (array_key_exists($part, $ret)) {
|
|
|
103 |
$ret = $ret[$part];
|
|
|
104 |
} else {
|
|
|
105 |
return $default;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
return $ret;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Store a value to the container
|
|
|
113 |
*
|
|
|
114 |
* @param string $name Configuration item name (name.space.name)
|
|
|
115 |
* @param mixed $value Value to be stored
|
|
|
116 |
*/
|
|
|
117 |
public function setParameter($name, $value)
|
|
|
118 |
{
|
|
|
119 |
$param = &$this->parameters;
|
|
|
120 |
$parts = explode('.', $name); //name.space.name
|
|
|
121 |
while ($part = array_shift($parts)) {
|
|
|
122 |
$param = &$param[$part];
|
|
|
123 |
}
|
|
|
124 |
$param = $value;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
*
|
|
|
129 |
*
|
|
|
130 |
* @param int $type
|
|
|
131 |
* @return mixed
|
|
|
132 |
*/
|
|
|
133 |
public function getParameters($type = PropelConfiguration::TYPE_ARRAY)
|
|
|
134 |
{
|
|
|
135 |
switch ($type) {
|
|
|
136 |
case PropelConfiguration::TYPE_ARRAY:
|
|
|
137 |
return $this->parameters;
|
|
|
138 |
case PropelConfiguration::TYPE_ARRAY_FLAT:
|
|
|
139 |
return $this->toFlatArray();
|
|
|
140 |
case PropelConfiguration::TYPE_OBJECT:
|
|
|
141 |
return $this;
|
|
|
142 |
default:
|
|
|
143 |
throw new PropelException('Unknown configuration type: '. var_export($type, true));
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Get the configuration as a flat array. ($array['name.space.item'] = 'value')
|
|
|
151 |
*
|
|
|
152 |
* @return array
|
|
|
153 |
*/
|
|
|
154 |
protected function toFlatArray()
|
|
|
155 |
{
|
|
|
156 |
$result = array();
|
|
|
157 |
$it = new PropelConfigurationIterator(new RecursiveArrayIterator($this->parameters), RecursiveIteratorIterator::SELF_FIRST);
|
|
|
158 |
foreach($it as $key => $value) {
|
|
|
159 |
$ns = $it->getDepth() ? $it->getNamespace() . '.'. $key : $key;
|
|
|
160 |
if ($it->getNodeType() == PropelConfigurationIterator::NODE_ITEM) {
|
|
|
161 |
$result[$ns] = $value;
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
return $result;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
?>
|