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