1. sfNamespacedParameterHolder.class.php
  2. /** * sfNamespacedParameterHolder provides a class for managing parameters * with support for namespaces. * * Parameters, in this case, are used to extend classes with additional data * that requires no additional logic to manage. * * @package symfony * @subpackage util * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfNamespacedParameterHolder.class.php 23922 2009-11-14 14:58:38Z fabien $ */
  3. class sfNamespacedParameterHolder extends sfParameterHolder
  4. {
  5. protected $default_namespace = null;
  6. protected $parameters = array();
  7. /**
  8. * The constructor for sfNamespacedParameterHolder.
  9. *
  10. * The default namespace may be overridden at initialization as follows:
  11. * <code>
  12. * <?php
  13. * $mySpecialPH = new sfNamespacedParameterHolder('symfony/special');
  14. * ?>
  15. * </code>
  16. */
  17. public function __construct($namespace = 'symfony/default')
  18. {
  19. $this->default_namespace = $namespace;
  20. }
  21. /**
  22. * Sets the default namespace value.
  23. *
  24. * @param string $namespace Default namespace
  25. * @param bool $move Move all values of the old default namespace to the new one or not
  26. */
  27. public function setDefaultNamespace($namespace, $move = true)
  28. {
  29. if ($move)
  30. {
  31. $values = $this->removeNamespace();
  32. $this->addByRef($values, $namespace);
  33. }
  34. $this->default_namespace = $namespace;
  35. }
  36. /**
  37. * Get the default namespace value.
  38. *
  39. * The $default_namespace is defined as 'symfony/default'.
  40. *
  41. * @return string The default namespace
  42. */
  43. public function getDefaultNamespace()
  44. {
  45. return $this->default_namespace;
  46. }
  47. /**
  48. * Clear all parameters associated with this request.
  49. */
  50. public function clear()
  51. {
  52. $this->parameters = null;
  53. $this->parameters = array();
  54. }
  55. /**
  56. * Retrieve a parameter with an optionally specified namespace.
  57. *
  58. * An isolated namespace may be identified by providing a value for the third
  59. * argument. If not specified, the default namespace 'symfony/default' is
  60. * used.
  61. *
  62. * @param string $name A parameter name
  63. * @param mixed $default A default parameter value
  64. * @param string $ns A parameter namespace
  65. *
  66. * @return mixed A parameter value, if the parameter exists, otherwise null
  67. */
  68. public function & get($name, $default = null, $ns = null)
  69. {
  70. if (!$ns)
  71. {
  72. $ns = $this->default_namespace;
  73. }
  74. if (isset($this->parameters[$ns][$name]))
  75. {
  76. $value = & $this->parameters[$ns][$name];
  77. }
  78. else
  79. {
  80. $value = $default;
  81. }
  82. return $value;
  83. }
  84. /**
  85. * Retrieve an array of parameter names from an optionally specified namespace.
  86. *
  87. * @param string $ns A parameter namespace.
  88. *
  89. * @return array An indexed array of parameter names, if the namespace exists, otherwise null
  90. */
  91. public function getNames($ns = null)
  92. {
  93. if (!$ns)
  94. {
  95. $ns = $this->default_namespace;
  96. }
  97. if (isset($this->parameters[$ns]))
  98. {
  99. return array_keys($this->parameters[$ns]);
  100. }
  101. return array();
  102. }
  103. /**
  104. * Retrieve an array of parameter namespaces.
  105. *
  106. * @return array An indexed array of parameter namespaces
  107. */
  108. public function getNamespaces()
  109. {
  110. return array_keys($this->parameters);
  111. }
  112. /**
  113. * Retrieve an array of parameters, within a namespace.
  114. *
  115. * This method is limited to a namespace. Without any argument,
  116. * it returns the parameters of the default namespace. If a
  117. * namespace is passed as an argument, only the parameters of the
  118. * specified namespace are returned.
  119. *
  120. * @param string $ns A parameter namespace
  121. *
  122. * @return array An associative array of parameters
  123. */
  124. public function & getAll($ns = null)
  125. {
  126. if (!$ns)
  127. {
  128. $ns = $this->default_namespace;
  129. }
  130. $parameters = array();
  131. if (isset($this->parameters[$ns]))
  132. {
  133. $parameters = $this->parameters[$ns];
  134. }
  135. return $parameters;
  136. }
  137. /**
  138. * Indicates whether or not a parameter exists.
  139. *
  140. * @param string $name A parameter name
  141. * @param string $ns A parameter namespace
  142. *
  143. * @return bool true, if the parameter exists, otherwise false
  144. */
  145. public function has($name, $ns = null)
  146. {
  147. if (!$ns)
  148. {
  149. $ns = $this->default_namespace;
  150. }
  151. return isset($this->parameters[$ns][$name]);
  152. }
  153. /**
  154. * Indicates whether or not A parameter namespace exists.
  155. *
  156. * @param string $ns A parameter namespace
  157. *
  158. * @return bool true, if the namespace exists, otherwise false
  159. */
  160. public function hasNamespace($ns)
  161. {
  162. return isset($this->parameters[$ns]);
  163. }
  164. /**
  165. * Remove a parameter.
  166. *
  167. * @param string $name A parameter name
  168. * @param mixed $default A default parameter value
  169. * @param string $ns A parameter namespace
  170. *
  171. * @return string A parameter value, if the parameter was removed, otherwise null
  172. */
  173. public function remove($name, $default = null, $ns = null)
  174. {
  175. if (!$ns)
  176. {
  177. $ns = $this->default_namespace;
  178. }
  179. $retval = $default;
  180. if (isset($this->parameters[$ns]) && array_key_exists($name, $this->parameters[$ns]))
  181. {
  182. $retval = $this->parameters[$ns][$name];
  183. unset($this->parameters[$ns][$name]);
  184. }
  185. return $retval;
  186. }
  187. /**
  188. * Remove A parameter namespace and all of its associated parameters.
  189. *
  190. * @param string $ns A parameter namespace.
  191. */
  192. public function & removeNamespace($ns = null)
  193. {
  194. if (!$ns)
  195. {
  196. $ns = $this->default_namespace;
  197. }
  198. $retval = null;
  199. if (isset($this->parameters[$ns]))
  200. {
  201. $retval =& $this->parameters[$ns];
  202. unset($this->parameters[$ns]);
  203. }
  204. return $retval;
  205. }
  206. /**
  207. * Set a parameter.
  208. *
  209. * If a parameter with the name already exists the value will be overridden.
  210. *
  211. * @param string $name A parameter name
  212. * @param mixed $value A parameter value
  213. * @param string $ns A parameter namespace
  214. */
  215. public function set($name, $value, $ns = null)
  216. {
  217. if (!$ns)
  218. {
  219. $ns = $this->default_namespace;
  220. }
  221. if (!isset($this->parameters[$ns]))
  222. {
  223. $this->parameters[$ns] = array();
  224. }
  225. $this->parameters[$ns][$name] = $value;
  226. }
  227. /**
  228. * Set a parameter by reference.
  229. *
  230. * If a parameter with the name already exists the value will be overridden.
  231. *
  232. * @param string $name A parameter name
  233. * @param mixed $value A reference to a parameter value
  234. * @param string $ns A parameter namespace
  235. */
  236. public function setByRef($name, & $value, $ns = null)
  237. {
  238. if (!$ns)
  239. {
  240. $ns = $this->default_namespace;
  241. }
  242. if (!isset($this->parameters[$ns]))
  243. {
  244. $this->parameters[$ns] = array();
  245. }
  246. $this->parameters[$ns][$name] =& $value;
  247. }
  248. /**
  249. * Set an array of parameters.
  250. *
  251. * If an existing parameter name matches any of the keys in the supplied
  252. * array, the associated value will be overridden.
  253. *
  254. * @param array $parameters An associative array of parameters and their associated values
  255. * @param string $ns A parameter namespace
  256. */
  257. public function add($parameters, $ns = null)
  258. {
  259. if ($parameters === null) return;
  260. if (!$ns)
  261. {
  262. $ns = $this->default_namespace;
  263. }
  264. if (!isset($this->parameters[$ns]))
  265. {
  266. $this->parameters[$ns] = array();
  267. }
  268. foreach ($parameters as $key => $value)
  269. {
  270. $this->parameters[$ns][$key] = $value;
  271. }
  272. }
  273. /**
  274. * Set an array of parameters by reference.
  275. *
  276. * If an existing parameter name matches any of the keys in the supplied
  277. * array, the associated value will be overridden.
  278. *
  279. * @param array $parameters An associative array of parameters and references to their associated values
  280. * @param string $ns A parameter namespace
  281. */
  282. public function addByRef(& $parameters, $ns = null)
  283. {
  284. if (!$ns)
  285. {
  286. $ns = $this->default_namespace;
  287. }
  288. if (!isset($this->parameters[$ns]))
  289. {
  290. $this->parameters[$ns] = array();
  291. }
  292. foreach ($parameters as $key => &$value)
  293. {
  294. $this->parameters[$ns][$key] =& $value;
  295. }
  296. }
  297. /**
  298. * Serializes the current instance.
  299. *
  300. * @return array Objects instance
  301. */
  302. public function serialize()
  303. {
  304. return serialize(array($this->default_namespace, $this->parameters));
  305. }
  306. /**
  307. * Unserializes a sfNamespacedParameterHolder instance.
  308. *
  309. * @param string $serialized A serialized sfNamespacedParameterHolder instance
  310. */
  311. public function unserialize($serialized)
  312. {
  313. $data = unserialize($serialized);
  314. $this->default_namespace = $data[0];
  315. $this->parameters = $data[1];
  316. }
  317. }

Debug toolbar