PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/registry/registry.php

https://github.com/dg482/joomla-platform
PHP | 629 lines | 258 code | 61 blank | 310 comment | 32 complexity | 3e9d929b4860b93705d9faf17c42394d MD5 | raw file
  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. * @since 11.1
  25. */
  26. protected $data;
  27. /**
  28. * Constructor
  29. *
  30. * @param mixed $data The data to bind to the new JRegistry object.
  31. *
  32. * @return void
  33. *
  34. * @since 11.1
  35. */
  36. public function __construct($data = null)
  37. {
  38. // Instantiate the internal data object.
  39. $this->data = new stdClass;
  40. // Optionally load supplied data.
  41. if (is_array($data) || is_object($data))
  42. {
  43. $this->bindData($this->data, $data);
  44. }
  45. elseif (!empty($data) && is_string($data))
  46. {
  47. $this->loadString($data);
  48. }
  49. }
  50. /**
  51. * Magic function to clone the registry object.
  52. *
  53. * @return JRegistry
  54. *
  55. * @since 11.1
  56. */
  57. public function __clone()
  58. {
  59. $this->data = unserialize(serialize($this->data));
  60. }
  61. /**
  62. * Magic function to render this object as a string using default args of toString method.
  63. *
  64. * @return string
  65. *
  66. * @since 11.1
  67. */
  68. public function __toString()
  69. {
  70. return $this->toString();
  71. }
  72. /**
  73. * Sets a default value if not already assigned.
  74. *
  75. * @param string $key The name of the parameter.
  76. * @param string $default An optional value for the parameter.
  77. *
  78. * @return string The value set, or the default if the value was not previously set (or null).
  79. *
  80. * @since 11.1
  81. */
  82. public function def($key, $default = '')
  83. {
  84. $value = $this->get($key, (string) $default);
  85. $this->set($key, $value);
  86. return $value;
  87. }
  88. /**
  89. * Check if a registry path exists.
  90. *
  91. * @param string $path Registry path (e.g. joomla.content.showauthor)
  92. *
  93. * @return boolean
  94. *
  95. * @since 11.1
  96. */
  97. public function exists($path)
  98. {
  99. // Explode the registry path into an array
  100. if ($nodes = explode('.', $path))
  101. {
  102. // Initialize the current node to be the registry root.
  103. $node = $this->data;
  104. // Traverse the registry to find the correct node for the result.
  105. for ($i = 0, $n = count($nodes); $i < $n; $i++)
  106. {
  107. if (isset($node->$nodes[$i]))
  108. {
  109. $node = $node->$nodes[$i];
  110. }
  111. else
  112. {
  113. break;
  114. }
  115. if ($i + 1 == $n)
  116. {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Get a registry value.
  125. *
  126. * @param string $path Registry path (e.g. joomla.content.showauthor)
  127. * @param mixed $default Optional default value, returned if the internal value is null.
  128. *
  129. * @return mixed Value of entry or null
  130. *
  131. * @since 11.1
  132. */
  133. public function get($path, $default = null)
  134. {
  135. // Initialise variables.
  136. $result = $default;
  137. if (!strpos($path, '.'))
  138. {
  139. return (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') ? $this->data->$path : $default;
  140. }
  141. // Explode the registry path into an array
  142. $nodes = explode('.', $path);
  143. // Initialize the current node to be the registry root.
  144. $node = $this->data;
  145. $found = false;
  146. // Traverse the registry to find the correct node for the result.
  147. foreach ($nodes as $n)
  148. {
  149. if (isset($node->$n))
  150. {
  151. $node = $node->$n;
  152. $found = true;
  153. }
  154. else
  155. {
  156. $found = false;
  157. break;
  158. }
  159. }
  160. if ($found && $node !== null && $node !== '')
  161. {
  162. $result = $node;
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Returns a reference to a global JRegistry object, only creating it
  168. * if it doesn't already exist.
  169. *
  170. * This method must be invoked as:
  171. * <pre>$registry = JRegistry::getInstance($id);</pre>
  172. *
  173. * @param string $id An ID for the registry instance
  174. *
  175. * @return object The JRegistry object.
  176. *
  177. * @since 11.1
  178. */
  179. public static function getInstance($id)
  180. {
  181. static $instances;
  182. if (!isset($instances))
  183. {
  184. $instances = array();
  185. }
  186. if (empty($instances[$id]))
  187. {
  188. $instances[$id] = new JRegistry;
  189. }
  190. return $instances[$id];
  191. }
  192. /**
  193. * Load a associative array of values into the default namespace
  194. *
  195. * @param array $array Associative array of value to load
  196. *
  197. * @return boolean True on success
  198. *
  199. * @since 11.1
  200. */
  201. public function loadArray($array)
  202. {
  203. $this->bindData($this->data, $array);
  204. return true;
  205. }
  206. /**
  207. * Load the public variables of the object into the default namespace.
  208. *
  209. * @param object $object The object holding the publics to load
  210. *
  211. * @return boolean True on success
  212. *
  213. * @since 11.1
  214. */
  215. public function loadObject($object)
  216. {
  217. $this->bindData($this->data, $object);
  218. return true;
  219. }
  220. /**
  221. * Load the contents of a file into the registry
  222. *
  223. * @param string $file Path to file to load
  224. * @param string $format Format of the file [optional: defaults to JSON]
  225. * @param mixed $options Options used by the formatter
  226. *
  227. * @return boolean True on success
  228. *
  229. * @since 11.1
  230. */
  231. public function loadFile($file, $format = 'JSON', $options = array())
  232. {
  233. // Get the contents of the file
  234. jimport('joomla.filesystem.file');
  235. $data = JFile::read($file);
  236. return $this->loadString($data, $format, $options);
  237. }
  238. /**
  239. * Load a string into the registry
  240. *
  241. * @param string $data String to load into the registry
  242. * @param string $format Format of the string
  243. * @param mixed $options Options used by the formatter
  244. *
  245. * @return boolean True on success
  246. *
  247. * @since 11.1
  248. */
  249. public function loadString($data, $format = 'JSON', $options = array())
  250. {
  251. // Load a string into the given namespace [or default namespace if not given]
  252. $handler = JRegistryFormat::getInstance($format);
  253. $obj = $handler->stringToObject($data, $options);
  254. $this->loadObject($obj);
  255. return true;
  256. }
  257. /**
  258. * Merge a JRegistry object into this one
  259. *
  260. * @param object &$source Source JRegistry object to merge.
  261. *
  262. * @return boolean True on success
  263. *
  264. * @since 11.1
  265. */
  266. public function merge(&$source)
  267. {
  268. if ($source instanceof JRegistry)
  269. {
  270. // Load the variables into the registry's default namespace.
  271. foreach ($source->toArray() as $k => $v)
  272. {
  273. if (($v !== null) && ($v !== ''))
  274. {
  275. $this->data->$k = $v;
  276. }
  277. }
  278. return true;
  279. }
  280. return false;
  281. }
  282. /**
  283. * Set a registry value.
  284. *
  285. * @param string $path Registry Path (e.g. joomla.content.showauthor)
  286. * @param mixed $value Value of entry
  287. *
  288. * @return mixed The value of the that has been set.
  289. *
  290. * @since 11.1
  291. */
  292. public function set($path, $value)
  293. {
  294. $result = null;
  295. // Explode the registry path into an array
  296. if ($nodes = explode('.', $path))
  297. {
  298. // Initialize the current node to be the registry root.
  299. $node = $this->data;
  300. // Traverse the registry to find the correct node for the result.
  301. for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
  302. {
  303. if (!isset($node->$nodes[$i]) && ($i != $n))
  304. {
  305. $node->$nodes[$i] = new stdClass;
  306. }
  307. $node = $node->$nodes[$i];
  308. }
  309. // Get the old value if exists so we can return it
  310. $result = $node->$nodes[$i] = $value;
  311. }
  312. return $result;
  313. }
  314. /**
  315. * Transforms a namespace to an array
  316. *
  317. * @return array An associative array holding the namespace data
  318. *
  319. * @since 11.1
  320. */
  321. public function toArray()
  322. {
  323. return (array) $this->asArray($this->data);
  324. }
  325. /**
  326. * Transforms a namespace to an object
  327. *
  328. * @return object An an object holding the namespace data
  329. *
  330. * @since 11.1
  331. */
  332. public function toObject()
  333. {
  334. return $this->data;
  335. }
  336. /**
  337. * Get a namespace in a given string format
  338. *
  339. * @param string $format Format to return the string in
  340. * @param mixed $options Parameters used by the formatter, see formatters for more info
  341. *
  342. * @return string Namespace in string format
  343. *
  344. * @since 11.1
  345. */
  346. public function toString($format = 'JSON', $options = array())
  347. {
  348. // Return a namespace in a given format
  349. $handler = JRegistryFormat::getInstance($format);
  350. return $handler->objectToString($this->data, $options);
  351. }
  352. /**
  353. * Method to recursively bind data to a parent object.
  354. *
  355. * @param object &$parent The parent object on which to attach the data values.
  356. * @param mixed $data An array or object of data to bind to the parent object.
  357. *
  358. * @return void
  359. *
  360. * @since 11.1
  361. */
  362. protected function bindData(&$parent, $data)
  363. {
  364. // Ensure the input data is an array.
  365. if (is_object($data))
  366. {
  367. $data = get_object_vars($data);
  368. }
  369. else
  370. {
  371. $data = (array) $data;
  372. }
  373. foreach ($data as $k => $v)
  374. {
  375. if ((is_array($v) && JArrayHelper::isAssociative($v)) || is_object($v))
  376. {
  377. $parent->$k = new stdClass;
  378. $this->bindData($parent->$k, $v);
  379. }
  380. else
  381. {
  382. $parent->$k = $v;
  383. }
  384. }
  385. }
  386. /**
  387. * Method to recursively convert an object of data to an array.
  388. *
  389. * @param object $data An object of data to return as an array.
  390. *
  391. * @return array Array representation of the input object.
  392. *
  393. * @since 11.1
  394. */
  395. protected function asArray($data)
  396. {
  397. $array = array();
  398. foreach (get_object_vars((object) $data) as $k => $v)
  399. {
  400. if (is_object($v))
  401. {
  402. $array[$k] = $this->asArray($v);
  403. }
  404. else
  405. {
  406. $array[$k] = $v;
  407. }
  408. }
  409. return $array;
  410. }
  411. //
  412. // Following methods are deprecated
  413. //
  414. /**
  415. * Load an XML string into the registry into the given namespace [or default if a namespace is not given]
  416. *
  417. * @param string $data XML formatted string to load into the registry
  418. * @param string $namespace Namespace to load the XML string into [optional]
  419. *
  420. * @return boolean True on success
  421. *
  422. * @since 11.1
  423. *
  424. * @deprecated 12.1 Use loadString passing XML as the format instead.
  425. * @note
  426. */
  427. public function loadXML($data, $namespace = null)
  428. {
  429. // Deprecation warning.
  430. JLog::add('JRegistry::loadXML() is deprecated.', JLog::WARNING, 'deprecated');
  431. return $this->loadString($data, 'XML');
  432. }
  433. /**
  434. * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
  435. *
  436. * @param string $data INI formatted string to load into the registry
  437. * @param string $namespace Namespace to load the INI string into [optional]
  438. * @param mixed $options An array of options for the formatter, or boolean to process sections.
  439. *
  440. * @return boolean True on success
  441. *
  442. * @since 11.1
  443. *
  444. * @deprecated 12.1 Use loadString passing INI as the format instead.
  445. */
  446. public function loadINI($data, $namespace = null, $options = array())
  447. {
  448. // Deprecation warning.
  449. JLog::add('JRegistry::loadINI() is deprecated.', JLog::WARNING, 'deprecated');
  450. return $this->loadString($data, 'INI', $options);
  451. }
  452. /**
  453. * Load an JSON string into the registry into the given namespace [or default if a namespace is not given]
  454. *
  455. * @param string $data JSON formatted string to load into the registry
  456. *
  457. * @return boolean True on success
  458. *
  459. * @deprecated 12.1 Use loadString passing JSON as the format instead.
  460. * @note Use loadString instead.
  461. * @since 11.1
  462. */
  463. public function loadJSON($data)
  464. {
  465. // Deprecation warning.
  466. JLog::add('JRegistry::loadJSON() is deprecated.', JLog::WARNING, 'deprecated');
  467. return $this->loadString($data, 'JSON');
  468. }
  469. /**
  470. * Create a namespace
  471. *
  472. * @param string $namespace Name of the namespace to create
  473. *
  474. * @return boolean True on success
  475. *
  476. * @deprecated 12.1
  477. * @note Namespaces are no longer supported.
  478. * @since 11.1
  479. */
  480. public function makeNameSpace($namespace)
  481. {
  482. // Deprecation warning.
  483. JLog::add('JRegistry::makeNameSpace() is deprecated.', JLog::WARNING, 'deprecated');
  484. //$this->_registry[$namespace] = array('data' => new stdClass());
  485. return true;
  486. }
  487. /**
  488. * Get the list of namespaces
  489. *
  490. * @return array List of namespaces
  491. *
  492. * @deprecated 12.1
  493. * @note Namespaces are no longer supported.
  494. * @since 11.1
  495. */
  496. public function getNameSpaces()
  497. {
  498. // Deprecation warning.
  499. JLog::add('JRegistry::getNameSpaces() is deprecated.', JLog::WARNING, 'deprecated');
  500. //return array_keys($this->_registry);
  501. return array();
  502. }
  503. /**
  504. * Get a registry value
  505. *
  506. * @param string $path Registry path (e.g. joomla.content.showauthor)
  507. * @param mixed $default Optional default value
  508. *
  509. * @return mixed Value of entry or null
  510. *
  511. * @deprecated 12.1
  512. * @note Use get instead.
  513. * @since 11.1
  514. */
  515. public function getValue($path, $default = null)
  516. {
  517. // Deprecation warning.
  518. JLog::add('JRegistry::getValue() is deprecated.', JLog::WARNING, 'deprecated');
  519. $parts = explode('.', $path);
  520. if (count($parts) > 1)
  521. {
  522. unset($parts[0]);
  523. $path = implode('.', $parts);
  524. }
  525. return $this->get($path, $default);
  526. }
  527. /**
  528. * Set a registry value
  529. *
  530. * @param string $path Registry Path (e.g. joomla.content.showauthor)
  531. * @param mixed $value Value of entry
  532. *
  533. * @return mixed The value after setting.
  534. *
  535. * @deprecated 12.1
  536. * @note Use set instead.
  537. * @since 11.1
  538. */
  539. public function setValue($path, $value)
  540. {
  541. // Deprecation warning.
  542. JLog::add('JRegistry::setValue() is deprecated.', JLog::WARNING, 'deprecated');
  543. $parts = explode('.', $path);
  544. if (count($parts) > 1)
  545. {
  546. unset($parts[0]);
  547. $path = implode('.', $parts);
  548. }
  549. return $this->set($path, $value);
  550. }
  551. /**
  552. * This method is added as an interim solution for API references in the Joomla! CMS 1.6 to the JRegistry
  553. * object where in 1.5 a JParameter object existed. Because many extensions may call this method
  554. * we add it here as a means of "pain relief" until the 1.8 release.
  555. *
  556. * @return boolean True.
  557. *
  558. * @deprecated 12.1
  559. * @note Load no longer supported.
  560. * @since 11.1
  561. */
  562. public function loadSetupFile()
  563. {
  564. // Deprecation warning.
  565. JLog::add('JRegistry::loadXML() is deprecated.', JLog::WARNING, 'deprecated');
  566. return true;
  567. }
  568. }