/lib/util/sfNamespacedParameterHolder.class.php

https://github.com/bheneka/gitta · PHP · 384 lines · 180 code · 45 blank · 159 comment · 23 complexity · 31deaac3b2d9a1a3286ee7ea5293de14 MD5 · raw file

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