PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/registry/registry.php

https://bitbucket.org/joomla/joomla-platform/
PHP | 513 lines | 216 code | 53 blank | 244 comment | 37 complexity | c0597c3a2fc6d52dd6a377ffc357dea6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Registry
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. JLoader::register('JRegistryFormat', dirname(__FILE__).'/format.php');
  11. /**
  12. * JRegistry class
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Registry
  16. * @since 11.1
  17. */
  18. class JRegistry
  19. {
  20. /**
  21. * Registry Object
  22. *
  23. * @var object
  24. */
  25. protected $data;
  26. /**
  27. * Constructor
  28. *
  29. * @return void
  30. * @since 11.1
  31. */
  32. public function __construct($data = null)
  33. {
  34. // Instantiate the internal data object.
  35. $this->data = new stdClass();
  36. // Optionally load supplied data.
  37. if (is_array($data) || is_object($data)) {
  38. $this->bindData($this->data, $data);
  39. }
  40. elseif (!empty($data) && is_string($data)) {
  41. $this->loadString($data);
  42. }
  43. }
  44. /**
  45. * Magic function to clone the registry object.
  46. */
  47. public function __clone()
  48. {
  49. $this->data = unserialize(serialize($this->data));
  50. }
  51. /**
  52. * Magic function to render this object as a string using default args of toString method.
  53. */
  54. public function __toString()
  55. {
  56. return $this->toString();
  57. }
  58. /**
  59. * Sets a default value if not alreay assigned.
  60. *
  61. * @param string The name of the parameter.
  62. * @param string An optional value for the parameter.
  63. * @param string An optional group for the parameter.
  64. * @return string The value set, or the default if the value was not previously set (or null).
  65. * @since 11.1
  66. */
  67. public function def($key, $default = '')
  68. {
  69. $value = $this->get($key, (string) $default);
  70. $this->set($key, $value);
  71. return $value;
  72. }
  73. /**
  74. * Check if a registry path exists.
  75. *
  76. * @param string Registry path (e.g. joomla.content.showauthor)
  77. * @return boolean
  78. * @since 11.1
  79. */
  80. public function exists($path)
  81. {
  82. // Explode the registry path into an array
  83. if ($nodes = explode('.', $path)) {
  84. // Initialize the current node to be the registry root.
  85. $node = $this->data;
  86. // Traverse the registry to find the correct node for the result.
  87. for ($i = 0,$n = count($nodes); $i < $n; $i++) {
  88. if (isset($node->$nodes[$i])) {
  89. $node = $node->$nodes[$i];
  90. } else {
  91. break;
  92. }
  93. if ($i+1 == $n) {
  94. return true;
  95. }
  96. }
  97. }
  98. return false;
  99. }
  100. /**
  101. * Get a registry value.
  102. *
  103. * @param string Registry path (e.g. joomla.content.showauthor)
  104. * @param mixed Optional default value, returned if the internal value is null.
  105. * @return mixed Value of entry or null
  106. * @since 11.1
  107. */
  108. public function get($path, $default = null)
  109. {
  110. // Initialise variables.
  111. $result = $default;
  112. if(!strpos($path, '.'))
  113. {
  114. return (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') ? $this->data->$path : $default;
  115. }
  116. // Explode the registry path into an array
  117. $nodes = explode('.', $path);
  118. // Initialize the current node to be the registry root.
  119. $node = $this->data;
  120. $found = false;
  121. // Traverse the registry to find the correct node for the result.
  122. foreach ($nodes as $n) {
  123. if (isset($node->$n)) {
  124. $node = $node->$n;
  125. $found = true;
  126. } else {
  127. $found = false;
  128. break;
  129. }
  130. }
  131. if ($found && $node !== null && $node !== '') {
  132. $result = $node;
  133. }
  134. return $result;
  135. }
  136. /**
  137. * Returns a reference to a global JRegistry object, only creating it
  138. * if it doesn't already exist.
  139. *
  140. * This method must be invoked as:
  141. * <pre>$registry = JRegistry::getInstance($id);</pre>
  142. *
  143. * @param string An ID for the registry instance
  144. * @return object The JRegistry object.
  145. * @since 11.1
  146. */
  147. public static function getInstance($id)
  148. {
  149. static $instances;
  150. if (!isset ($instances)) {
  151. $instances = array ();
  152. }
  153. if (empty ($instances[$id])) {
  154. $instances[$id] = new JRegistry();
  155. }
  156. return $instances[$id];
  157. }
  158. /**
  159. * Load a associative array of values into the default namespace
  160. *
  161. * @param array Associative array of value to load
  162. * @param string The name of the namespace
  163. * @return boolean True on success
  164. * @since 11.1
  165. */
  166. public function loadArray($array)
  167. {
  168. $this->bindData($this->data, $array);
  169. return true;
  170. }
  171. /**
  172. * Load the public variables of the object into the default namespace.
  173. *
  174. * @param object The object holding the publics to load
  175. * @param string Namespace to load the INI string into [optional]
  176. * @return boolean True on success
  177. * @since 11.1
  178. */
  179. public function loadObject($object)
  180. {
  181. $this->bindData($this->data, $object);
  182. return true;
  183. }
  184. /**
  185. * Load the contents of a file into the registry
  186. *
  187. * @param string Path to file to load
  188. * @param string Format of the file [optional: defaults to JSON]
  189. * @param mixed Options used by the formatter
  190. * @return boolean True on success
  191. * @since 11.1
  192. */
  193. public function loadFile($file, $format = 'JSON', $options = array())
  194. {
  195. // Get the contents of the file
  196. jimport('joomla.filesystem.file');
  197. $data = JFile::read($file);
  198. return $this->loadString($data, $format, $options);
  199. }
  200. /**
  201. * Load a string into the registry
  202. *
  203. * @param string string to load into the registry
  204. * @param string format of the string
  205. * @param mixed Options used by the formatter
  206. * @return boolean True on success
  207. * @since 11.1
  208. */
  209. public function loadString($data, $format = 'JSON', $options = array())
  210. {
  211. // Load a string into the given namespace [or default namespace if not given]
  212. $handler = JRegistryFormat::getInstance($format);
  213. $obj = $handler->stringToObject($data, $options);
  214. $this->loadObject($obj);
  215. return true;
  216. }
  217. /**
  218. * Merge a JRegistry object into this one
  219. *
  220. * @param object Source JRegistry object ot merge
  221. * @return boolean True on success
  222. * @since 11.1
  223. */
  224. public function merge(&$source)
  225. {
  226. if ($source instanceof JRegistry) {
  227. // Load the variables into the registry's default namespace.
  228. foreach ($source->toArray() as $k => $v) {
  229. if (($v !== null) && ($v !== '')){
  230. $this->data->$k = $v;
  231. }
  232. }
  233. return true;
  234. }
  235. return false;
  236. }
  237. /**
  238. * Set a registry value.
  239. *
  240. * @param string Registry Path (e.g. joomla.content.showauthor)
  241. * @param mixed Value of entry
  242. * @return mixed The value of the that has been set.
  243. * @since 11.1
  244. */
  245. public function set($path, $value)
  246. {
  247. $result = null;
  248. // Explode the registry path into an array
  249. if ($nodes = explode('.', $path)) {
  250. // Initialize the current node to be the registry root.
  251. $node = $this->data;
  252. // Traverse the registry to find the correct node for the result.
  253. for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++) {
  254. if (!isset($node->$nodes[$i]) && ($i != $n)) {
  255. $node->$nodes[$i] = new stdClass();
  256. }
  257. $node = $node->$nodes[$i];
  258. }
  259. // Get the old value if exists so we can return it
  260. $result = $node->$nodes[$i] = $value;
  261. }
  262. return $result;
  263. }
  264. /**
  265. * Transforms a namespace to an array
  266. *
  267. * @param string Namespace to return [optional: null returns the default namespace]
  268. * @return array An associative array holding the namespace data
  269. * @since 11.1
  270. */
  271. public function toArray()
  272. {
  273. return (array) $this->asArray($this->data);
  274. }
  275. /**
  276. * Transforms a namespace to an object
  277. *
  278. * @param string Namespace to return [optional: null returns the default namespace]
  279. * @return object An an object holding the namespace data
  280. * @since 11.1
  281. */
  282. public function toObject()
  283. {
  284. return $this->data;
  285. }
  286. /**
  287. * Get a namespace in a given string format
  288. *
  289. * @param string Format to return the string in
  290. * @param mixed Parameters used by the formatter, see formatters for more info
  291. * @return string Namespace in string format
  292. * @since 11.1
  293. */
  294. public function toString($format = 'JSON', $options = array())
  295. {
  296. // Return a namespace in a given format
  297. $handler = JRegistryFormat::getInstance($format);
  298. return $handler->objectToString($this->data, $options);
  299. }
  300. /**
  301. * Method to recursively bind data to a parent object.
  302. *
  303. * @param object $parent The parent object on which to attach the data values.
  304. * @param mixed $data An array or object of data to bind to the parent object.
  305. *
  306. * @return void
  307. * @since 11.1
  308. */
  309. protected function bindData(& $parent, $data)
  310. {
  311. // Ensure the input data is an array.
  312. if(is_object($data)) {
  313. $data = get_object_vars($data);
  314. } else {
  315. $data = (array) $data;
  316. }
  317. foreach ($data as $k => $v) {
  318. if ((is_array($v) && JArrayHelper::isAssociative($v)) || is_object($v)) {
  319. $parent->$k = new stdClass();
  320. $this->bindData($parent->$k, $v);
  321. } else {
  322. $parent->$k = $v;
  323. }
  324. }
  325. }
  326. /**
  327. * Method to recursively convert an object of data to an array.
  328. *
  329. * @param object $data An object of data to return as an array.
  330. *
  331. * @return array Array representation of the input object.
  332. * @since 11.1
  333. */
  334. protected function asArray($data)
  335. {
  336. $array = array();
  337. foreach (get_object_vars((object) $data) as $k => $v) {
  338. if (is_object($v)) {
  339. $array[$k] = $this->asArray($v);
  340. } else {
  341. $array[$k] = $v;
  342. }
  343. }
  344. return $array;
  345. }
  346. //
  347. // Following methods are deprecated
  348. //
  349. /**
  350. * Load an XML string into the registry into the given namespace [or default if a namespace is not given]
  351. *
  352. * @param string XML formatted string to load into the registry
  353. * @param string Namespace to load the XML string into [optional]
  354. * @return boolean True on success
  355. * @since 11.1
  356. * @deprecated 1.6 - Oct 25, 2010
  357. */
  358. public function loadXML($data, $namespace = null)
  359. {
  360. return $this->loadString($data, 'XML');
  361. }
  362. /**
  363. * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
  364. *
  365. * @param string INI formatted string to load into the registry
  366. * @param string Namespace to load the INI string into [optional]
  367. * @param mixed An array of options for the formatter, or boolean to process sections.
  368. * @return boolean True on success
  369. * @since 11.1
  370. * @deprecated 1.6 - Oct 25, 2010
  371. */
  372. public function loadINI($data, $namespace = null, $options = array())
  373. {
  374. return $this->loadString($data, 'INI', $options);
  375. }
  376. /**
  377. * Load an JSON string into the registry into the given namespace [or default if a namespace is not given]
  378. *
  379. * @param string JSON formatted string to load into the registry
  380. * @return boolean True on success
  381. * @since 11.1
  382. * @deprecated 1.6 - Oct 25, 2010
  383. */
  384. public function loadJSON($data)
  385. {
  386. return $this->loadString($data, 'JSON');
  387. }
  388. /**
  389. * Create a namespace
  390. *
  391. * @param string Name of the namespace to create
  392. * @return boolean True on success
  393. * @since 11.1
  394. * @deprecated 1.6 - Jan 19, 2010
  395. */
  396. public function makeNameSpace($namespace)
  397. {
  398. //$this->_registry[$namespace] = array('data' => new stdClass());
  399. return true;
  400. }
  401. /**
  402. * Get the list of namespaces
  403. *
  404. * @return array List of namespaces
  405. * @deprecated 1.6 - Jan 19, 2010
  406. */
  407. public function getNameSpaces()
  408. {
  409. //return array_keys($this->_registry);
  410. return array();
  411. }
  412. /**
  413. * Get a registry value
  414. *
  415. * @param string Registry path (e.g. joomla.content.showauthor)
  416. * @param mixed Optional default value
  417. * @return mixed Value of entry or null
  418. * @deprecated 1.6 - Jan 19, 2010
  419. */
  420. public function getValue($path, $default=null)
  421. {
  422. $parts = explode('.', $path);
  423. if (count($parts) > 1) {
  424. unset($parts[0]);
  425. $path = implode('.', $parts);
  426. }
  427. return $this->get($path, $default);
  428. }
  429. /**
  430. * Set a registry value
  431. *
  432. * @param string Registry Path (e.g. joomla.content.showauthor)
  433. * @param mixed Value of entry
  434. * @return mixed The value after setting.
  435. * @deprecated 1.6 - Jan 19, 2010
  436. */
  437. public function setValue($path, $value)
  438. {
  439. $parts = explode('.', $path);
  440. if (count($parts) > 1) {
  441. unset($parts[0]);
  442. $path = implode('.', $parts);
  443. }
  444. return $this->set($path, $value);
  445. }
  446. /**
  447. * This method is added as an interim solution for API references in Joomla! 1.6 to the JRegistry
  448. * object where in 1.5 a JParameter object existed. Because many extensions may call this method
  449. * we add it here as a means of "pain relief" until the 1.7 release.
  450. *
  451. * @return boolean True.
  452. *
  453. * @deprecated 1.6 - Jun 17, 2010
  454. * @todo Remove this method for the 1.7 release.
  455. */
  456. public function loadSetupFile()
  457. {
  458. return true;
  459. }
  460. }