sfNamespacedParameterHolder.class.php 9.3 KB

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