| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
class sfParameterHolder |
|---|
| 25 |
{ |
|---|
| 26 |
protected $default_namespace = null; |
|---|
| 27 |
protected $parameters = array(); |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
* The constructor for sfParameterHolder. |
|---|
| 31 |
* |
|---|
| 32 |
* The default namespace may be overridden at initialization as follows: |
|---|
| 33 |
* <code> |
|---|
| 34 |
* <?php |
|---|
| 35 |
* $mySpecialPH = new sfParameterHolder('symfony/special'); |
|---|
| 36 |
* ?> |
|---|
| 37 |
* </code> |
|---|
| 38 |
*/ |
|---|
| 39 |
public function __construct($namespace = 'symfony/default') |
|---|
| 40 |
{ |
|---|
| 41 |
$this->default_namespace = $namespace; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* Get the default namespace value. |
|---|
| 46 |
* |
|---|
| 47 |
* The $default_namespace is defined as 'symfony/default'. |
|---|
| 48 |
* |
|---|
| 49 |
* @return string The default namespace. |
|---|
| 50 |
*/ |
|---|
| 51 |
public function getDefaultNamespace() |
|---|
| 52 |
{ |
|---|
| 53 |
return $this->default_namespace; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
* Clear all parameters associated with this request. |
|---|
| 58 |
* |
|---|
| 59 |
* @return void |
|---|
| 60 |
*/ |
|---|
| 61 |
public function clear() |
|---|
| 62 |
{ |
|---|
| 63 |
$this->parameters = null; |
|---|
| 64 |
$this->parameters = array(); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
* Retrieve a parameter with an optionally specified namespace. |
|---|
| 69 |
* |
|---|
| 70 |
* An isolated namespace may be identified by providing a value for the third |
|---|
| 71 |
* argument. If not specified, the default namespace 'symfony/default' is |
|---|
| 72 |
* used. |
|---|
| 73 |
* |
|---|
| 74 |
* @param string A parameter name. |
|---|
| 75 |
* @param mixed A default parameter value. |
|---|
| 76 |
* @param string A parameter namespace. |
|---|
| 77 |
* |
|---|
| 78 |
* @return mixed A parameter value, if the parameter exists, otherwise null. |
|---|
| 79 |
*/ |
|---|
| 80 |
public function & get($name, $default = null, $ns = null) |
|---|
| 81 |
{ |
|---|
| 82 |
if (!$ns) |
|---|
| 83 |
{ |
|---|
| 84 |
$ns = $this->default_namespace; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
if (isset($this->parameters[$ns][$name])) |
|---|
| 88 |
{ |
|---|
| 89 |
$value = & $this->parameters[$ns][$name]; |
|---|
| 90 |
} |
|---|
| 91 |
else if (isset($this->parameters[$ns])) |
|---|
| 92 |
{ |
|---|
| 93 |
$value = sfToolkit::getArrayValueForPath($this->parameters[$ns], $name, $default); |
|---|
| 94 |
} |
|---|
| 95 |
else |
|---|
| 96 |
{ |
|---|
| 97 |
$value = $default; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
return $value; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
* Retrieve an array of parameter names from an optionally specified namespace. |
|---|
| 105 |
* |
|---|
| 106 |
* @param string A parameter namespace. |
|---|
| 107 |
* |
|---|
| 108 |
* @return array An indexed array of parameter names, if the namespace exists, otherwise null. |
|---|
| 109 |
*/ |
|---|
| 110 |
public function getNames($ns = null) |
|---|
| 111 |
{ |
|---|
| 112 |
if (!$ns) |
|---|
| 113 |
{ |
|---|
| 114 |
$ns = $this->default_namespace; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
if (isset($this->parameters[$ns])) |
|---|
| 118 |
{ |
|---|
| 119 |
return array_keys($this->parameters[$ns]); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
return array(); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
* Retrieve an array of parameter namespaces. |
|---|
| 127 |
* |
|---|
| 128 |
* @return array An indexed array of parameter namespaces. |
|---|
| 129 |
*/ |
|---|
| 130 |
public function getNamespaces() |
|---|
| 131 |
{ |
|---|
| 132 |
return array_keys($this->parameters); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
* Retrieve an array of parameters, within a namespace. |
|---|
| 137 |
* |
|---|
| 138 |
* This method is limited to a namespace. Without any argument, |
|---|
| 139 |
* it returns the parameters of the default namespace. If a |
|---|
| 140 |
* namespace is passed as an argument, only the parameters of the |
|---|
| 141 |
* specified namespace are returned. |
|---|
| 142 |
* |
|---|
| 143 |
* @param string A parameter namespace. |
|---|
| 144 |
* |
|---|
| 145 |
* @return array An associative array of parameters. |
|---|
| 146 |
*/ |
|---|
| 147 |
public function & getAll($ns = null) |
|---|
| 148 |
{ |
|---|
| 149 |
if (!$ns) |
|---|
| 150 |
{ |
|---|
| 151 |
$ns = $this->default_namespace; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
$parameters = array(); |
|---|
| 155 |
|
|---|
| 156 |
if (isset($this->parameters[$ns])) |
|---|
| 157 |
{ |
|---|
| 158 |
$parameters = $this->parameters[$ns]; |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
return $parameters; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
* Indicates whether or not a parameter exists. |
|---|
| 166 |
* |
|---|
| 167 |
* @param string A parameter name. |
|---|
| 168 |
* @param string A parameter namespace. |
|---|
| 169 |
* |
|---|
| 170 |
* @return bool true, if the parameter exists, otherwise false. |
|---|
| 171 |
*/ |
|---|
| 172 |
public function has($name, $ns = null) |
|---|
| 173 |
{ |
|---|
| 174 |
if (!$ns) |
|---|
| 175 |
{ |
|---|
| 176 |
$ns = $this->default_namespace; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
if (false !== ($offset = strpos($name, '['))) |
|---|
| 180 |
{ |
|---|
| 181 |
if (isset($this->parameters[$ns][substr($name, 0, $offset)])) |
|---|
| 182 |
{ |
|---|
| 183 |
$array = $this->parameters[$ns][substr($name, 0, $offset)]; |
|---|
| 184 |
|
|---|
| 185 |
while ($pos = strpos($name, '[', $offset)) |
|---|
| 186 |
{ |
|---|
| 187 |
$end = strpos($name, ']', $pos); |
|---|
| 188 |
if ($end == $pos + 1) |
|---|
| 189 |
{ |
|---|
| 190 |
|
|---|
| 191 |
return true; |
|---|
| 192 |
} |
|---|
| 193 |
else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) |
|---|
| 194 |
{ |
|---|
| 195 |
return false; |
|---|
| 196 |
} |
|---|
| 197 |
$array = $array[substr($name, $pos + 1, $end - $pos - 1)]; |
|---|
| 198 |
$offset = $end; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
return true; |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
elseif (isset($this->parameters[$ns][$name])) |
|---|
| 205 |
{ |
|---|
| 206 |
return true; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
return false; |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
* Indicates whether or not A parameter namespace exists. |
|---|
| 214 |
* |
|---|
| 215 |
* @param string A parameter namespace. |
|---|
| 216 |
* |
|---|
| 217 |
* @return bool true, if the namespace exists, otherwise false. |
|---|
| 218 |
*/ |
|---|
| 219 |
public function hasNamespace($ns) |
|---|
| 220 |
{ |
|---|
| 221 |
return isset($this->parameters[$ns]); |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
* Remove a parameter. |
|---|
| 226 |
* |
|---|
| 227 |
* @param string A parameter name. |
|---|
| 228 |
* @param string A parameter namespace. |
|---|
| 229 |
* |
|---|
| 230 |
* @return string A parameter value, if the parameter was removed, otherwise null. |
|---|
| 231 |
*/ |
|---|
| 232 |
public function & remove($name, $ns = null) |
|---|
| 233 |
{ |
|---|
| 234 |
if (!$ns) |
|---|
| 235 |
{ |
|---|
| 236 |
$ns = $this->default_namespace; |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
$retval = null; |
|---|
| 240 |
|
|---|
| 241 |
if (isset($this->parameters[$ns]) && isset($this->parameters[$ns][$name])) |
|---|
| 242 |
{ |
|---|
| 243 |
$retval =& $this->parameters[$ns][$name]; |
|---|
| 244 |
unset($this->parameters[$ns][$name]); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
return $retval; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
* Remove A parameter namespace and all of its associated parameters. |
|---|
| 252 |
* |
|---|
| 253 |
* @param string A parameter namespace. |
|---|
| 254 |
* |
|---|
| 255 |
* @return void |
|---|
| 256 |
*/ |
|---|
| 257 |
public function & removeNamespace($ns = null) |
|---|
| 258 |
{ |
|---|
| 259 |
if (!$ns) |
|---|
| 260 |
{ |
|---|
| 261 |
$ns = $this->default_namespace; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
$retval = null; |
|---|
| 265 |
|
|---|
| 266 |
if (isset($this->parameters[$ns])) |
|---|
| 267 |
{ |
|---|
| 268 |
$retval =& $this->parameters[$ns]; |
|---|
| 269 |
unset($this->parameters[$ns]); |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
return $retval; |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
* Set a parameter. |
|---|
| 277 |
* |
|---|
| 278 |
* If a parameter with the name already exists the value will be overridden. |
|---|
| 279 |
* |
|---|
| 280 |
* @param string A parameter name. |
|---|
| 281 |
* @param mixed A parameter value. |
|---|
| 282 |
* @param string A parameter namespace. |
|---|
| 283 |
* |
|---|
| 284 |
* @return void |
|---|
| 285 |
*/ |
|---|
| 286 |
public function set($name, $value, $ns = null) |
|---|
| 287 |
{ |
|---|
| 288 |
if (!$ns) |
|---|
| 289 |
{ |
|---|
| 290 |
$ns = $this->default_namespace; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
if (!isset($this->parameters[$ns])) |
|---|
| 294 |
{ |
|---|
| 295 |
$this->parameters[$ns] = array(); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
$this->parameters[$ns][$name] = $value; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
* Set a parameter by reference. |
|---|
| 303 |
* |
|---|
| 304 |
* If a parameter with the name already exists the value will be overridden. |
|---|
| 305 |
* |
|---|
| 306 |
* @param string A parameter name. |
|---|
| 307 |
* @param mixed A reference to a parameter value. |
|---|
| 308 |
* @param string A parameter namespace. |
|---|
| 309 |
* |
|---|
| 310 |
* @return void |
|---|
| 311 |
*/ |
|---|
| 312 |
public function setByRef($name, & $value, $ns = null) |
|---|
| 313 |
{ |
|---|
| 314 |
if (!$ns) |
|---|
| 315 |
{ |
|---|
| 316 |
$ns = $this->default_namespace; |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
if (!isset($this->parameters[$ns])) |
|---|
| 320 |
{ |
|---|
| 321 |
$this->parameters[$ns] = array(); |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
$this->parameters[$ns][$name] =& $value; |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
* Set an array of parameters. |
|---|
| 329 |
* |
|---|
| 330 |
* If an existing parameter name matches any of the keys in the supplied |
|---|
| 331 |
* array, the associated value will be overridden. |
|---|
| 332 |
* |
|---|
| 333 |
* @param array An associative array of parameters and their associated values. |
|---|
| 334 |
* @param string A parameter namespace. |
|---|
| 335 |
* |
|---|
| 336 |
* @return void |
|---|
| 337 |
*/ |
|---|
| 338 |
public function add($parameters, $ns = null) |
|---|
| 339 |
{ |
|---|
| 340 |
if ($parameters === null) return; |
|---|
| 341 |
|
|---|
| 342 |
if (!$ns) |
|---|
| 343 |
{ |
|---|
| 344 |
$ns = $this->default_namespace; |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
if (!isset($this->parameters[$ns])) |
|---|
| 348 |
{ |
|---|
| 349 |
$this->parameters[$ns] = array(); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
foreach ($parameters as $key => $value) |
|---|
| 353 |
{ |
|---|
| 354 |
$this->parameters[$ns][$key] = $value; |
|---|
| 355 |
} |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
* Set an array of parameters by reference. |
|---|
| 360 |
* |
|---|
| 361 |
* If an existing parameter name matches any of the keys in the supplied |
|---|
| 362 |
* array, the associated value will be overridden. |
|---|
| 363 |
* |
|---|
| 364 |
* @param array An associative array of parameters and references to their associated values. |
|---|
| 365 |
* @param string A parameter namespace. |
|---|
| 366 |
* |
|---|
| 367 |
* @return void |
|---|
| 368 |
*/ |
|---|
| 369 |
public function addByRef(& $parameters, $ns = null) |
|---|
| 370 |
{ |
|---|
| 371 |
if (!$ns) |
|---|
| 372 |
{ |
|---|
| 373 |
$ns = $this->default_namespace; |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
if (!isset($this->parameters[$ns])) |
|---|
| 377 |
{ |
|---|
| 378 |
$this->parameters[$ns] = array(); |
|---|
| 379 |
} |
|---|
| 380 |
|
|---|
| 381 |
foreach ($parameters as $key => &$value) |
|---|
| 382 |
{ |
|---|
| 383 |
$this->parameters[$ns][$key] =& $value; |
|---|
| 384 |
} |
|---|
| 385 |
} |
|---|
| 386 |
} |
|---|
| 387 |
|
|---|