PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/Quản lý website các món ăn và ẩm thực php/bbqvietnam.com/libraries/joomla/registry/registry.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 512 lines | 229 code | 64 blank | 219 comment | 47 complexity | 22d0bfec2fb57ca1139cbc50e5c27b75 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: registry.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Registry
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. //Register the session storage class with the loader
  17. JLoader::register('JRegistryFormat', dirname(__FILE__).DS.'format.php');
  18. /**
  19. * JRegistry class
  20. *
  21. * @package Joomla.Framework
  22. * @subpackage Registry
  23. * @since 1.5
  24. */
  25. class JRegistry extends JObject
  26. {
  27. /**
  28. * Default NameSpace
  29. * @var string
  30. */
  31. var $_defaultNameSpace = null;
  32. /**
  33. * Registry Object
  34. * - actually an array of namespace objects
  35. * @var array
  36. */
  37. var $_registry = array ();
  38. /**
  39. * Constructor
  40. *
  41. * @access protected
  42. * @param string $namespace Default registry namespace
  43. * @return void
  44. * @since 1.5
  45. */
  46. function __construct($namespace = 'default')
  47. {
  48. $this->_defaultNameSpace = $namespace;
  49. $this->makeNameSpace($namespace);
  50. }
  51. /**
  52. * Returns a reference to a global JRegistry object, only creating it
  53. * if it doesn't already exist.
  54. *
  55. * This method must be invoked as:
  56. * <pre>$registry =& JRegistry::getInstance($id[, $namespace]);</pre>
  57. *
  58. * @static
  59. * @param string $id An ID for the registry instance
  60. * @param string $namespace The default namespace for the registry object [optional]
  61. * @return object The JRegistry object.
  62. * @since 1.5
  63. */
  64. function &getInstance($id, $namespace = 'default')
  65. {
  66. static $instances;
  67. if (!isset ($instances)) {
  68. $instances = array ();
  69. }
  70. if (empty ($instances[$id])) {
  71. $instances[$id] = new JRegistry($namespace);
  72. }
  73. return $instances[$id];
  74. }
  75. /**
  76. * Create a namespace
  77. *
  78. * @access public
  79. * @param string $namespace Name of the namespace to create
  80. * @return boolean True on success
  81. * @since 1.5
  82. */
  83. function makeNameSpace($namespace)
  84. {
  85. $this->_registry[$namespace] = array('data' => new stdClass());
  86. return true;
  87. }
  88. /**
  89. * Get the list of namespaces
  90. *
  91. * @access public
  92. * @return array List of namespaces
  93. * @since 1.5
  94. */
  95. function getNameSpaces()
  96. {
  97. return array_keys($this->_registry);
  98. }
  99. /**
  100. * Get a registry value
  101. *
  102. * @access public
  103. * @param string $regpath Registry path (e.g. joomla.content.showauthor)
  104. * @param mixed $default Optional default value
  105. * @return mixed Value of entry or null
  106. * @since 1.5
  107. */
  108. function getValue($regpath, $default=null)
  109. {
  110. $result = $default;
  111. // Explode the registry path into an array
  112. if ($nodes = explode('.', $regpath))
  113. {
  114. // Get the namespace
  115. //$namespace = array_shift($nodes);
  116. $count = count($nodes);
  117. if ($count < 2) {
  118. $namespace = $this->_defaultNameSpace;
  119. $nodes[1] = $nodes[0];
  120. } else {
  121. $namespace = $nodes[0];
  122. }
  123. if (isset($this->_registry[$namespace])) {
  124. $ns = & $this->_registry[$namespace]['data'];
  125. $pathNodes = $count - 1;
  126. //for ($i = 0; $i < $pathNodes; $i ++) {
  127. for ($i = 1; $i < $pathNodes; $i ++) {
  128. if((isset($ns->$nodes[$i]))) $ns =& $ns->$nodes[$i];
  129. }
  130. if(isset($ns->$nodes[$i])) {
  131. $result = $ns->$nodes[$i];
  132. }
  133. }
  134. }
  135. return $result;
  136. }
  137. /**
  138. * Set a registry value
  139. *
  140. * @access public
  141. * @param string $regpath Registry Path (e.g. joomla.content.showauthor)
  142. * @param mixed $value Value of entry
  143. * @return mixed Value of old value or boolean false if operation failed
  144. * @since 1.5
  145. */
  146. function setValue($regpath, $value)
  147. {
  148. // Explode the registry path into an array
  149. $nodes = explode('.', $regpath);
  150. // Get the namespace
  151. $count = count($nodes);
  152. if ($count < 2) {
  153. $namespace = $this->_defaultNameSpace;
  154. } else {
  155. $namespace = array_shift($nodes);
  156. $count--;
  157. }
  158. if (!isset($this->_registry[$namespace])) {
  159. $this->makeNameSpace($namespace);
  160. }
  161. $ns = & $this->_registry[$namespace]['data'];
  162. $pathNodes = $count - 1;
  163. if ($pathNodes < 0) {
  164. $pathNodes = 0;
  165. }
  166. for ($i = 0; $i < $pathNodes; $i ++)
  167. {
  168. // If any node along the registry path does not exist, create it
  169. if (!isset($ns->$nodes[$i])) {
  170. $ns->$nodes[$i] = new stdClass();
  171. }
  172. $ns =& $ns->$nodes[$i];
  173. }
  174. // Get the old value if exists so we can return it
  175. $ns->$nodes[$i] =& $value;
  176. return $ns->$nodes[$i];
  177. }
  178. /**
  179. * Load a associative array of values into the default namespace
  180. *
  181. * @access public
  182. * @param array $array Associative array of value to load
  183. * @param string $namepsace The name of the namespace
  184. * @return boolean True on success
  185. * @since 1.5
  186. */
  187. function loadArray($array, $namespace = null)
  188. {
  189. // If namespace is not set, get the default namespace
  190. if ($namespace == null) {
  191. $namespace = $this->_defaultNameSpace;
  192. }
  193. if (!isset($this->_registry[$namespace])) {
  194. // If namespace does not exist, make it and load the data
  195. $this->makeNameSpace($namespace);
  196. }
  197. // Load the variables into the registry's default namespace.
  198. foreach ($array as $k => $v)
  199. {
  200. $this->_registry[$namespace]['data']->$k = $v;
  201. }
  202. return true;
  203. }
  204. /**
  205. * Load the public variables of the object into the default namespace.
  206. *
  207. * @access public
  208. * @param object $object The object holding the public vars to load
  209. * @param string $namespace Namespace to load the INI string into [optional]
  210. * @return boolean True on success
  211. * @since 1.5
  212. */
  213. function loadObject(&$object, $namespace = null)
  214. {
  215. // If namespace is not set, get the default namespace
  216. if ($namespace == null) {
  217. $namespace = $this->_defaultNameSpace;
  218. }
  219. if (!isset($this->_registry[$namespace])) {
  220. // If namespace does not exist, make it and load the data
  221. $this->makeNameSpace($namespace);
  222. }
  223. /*
  224. * We want to leave groups that are already in the namespace and add the
  225. * groups loaded into the namespace. This overwrites any existing group
  226. * with the same name
  227. */
  228. if (is_object( $object ))
  229. {
  230. foreach (get_object_vars($object) as $k => $v) {
  231. if (substr($k, 0,1) != '_' || $k == '_name') {
  232. $this->_registry[$namespace]['data']->$k = $v;
  233. }
  234. }
  235. }
  236. return true;
  237. }
  238. /**
  239. * Load the contents of a file into the registry
  240. *
  241. * @access public
  242. * @param string $file Path to file to load
  243. * @param string $format Format of the file [optional: defaults to INI]
  244. * @param string $namespace Namespace to load the INI string into [optional]
  245. * @return boolean True on success
  246. * @since 1.5
  247. */
  248. function loadFile($file, $format = 'INI', $namespace = null)
  249. {
  250. // Load a file into the given namespace [or default namespace if not given]
  251. $handler =& JRegistryFormat::getInstance($format);
  252. // If namespace is not set, get the default namespace
  253. if ($namespace == null) {
  254. $namespace = $this->_defaultNameSpace;
  255. }
  256. // Get the contents of the file
  257. jimport('joomla.filesystem.file');
  258. $data = JFile::read($file);
  259. if (!isset($this->_registry[$namespace]))
  260. {
  261. // If namespace does not exist, make it and load the data
  262. $this->makeNameSpace($namespace);
  263. $this->_registry[$namespace]['data'] = $handler->stringToObject($data);
  264. }
  265. else
  266. {
  267. // Get the data in object format
  268. $ns = $handler->stringToObject($data);
  269. /*
  270. * We want to leave groups that are already in the namespace and add the
  271. * groups loaded into the namespace. This overwrites any existing group
  272. * with the same name
  273. */
  274. foreach (get_object_vars($ns) as $k => $v) {
  275. $this->_registry[$namespace]['data']->$k = $v;
  276. }
  277. }
  278. return true;
  279. }
  280. /**
  281. * Load an XML string into the registry into the given namespace [or default if a namespace is not given]
  282. *
  283. * @access public
  284. * @param string $data XML formatted string to load into the registry
  285. * @param string $namespace Namespace to load the XML string into [optional]
  286. * @return boolean True on success
  287. * @since 1.5
  288. */
  289. function loadXML($data, $namespace = null)
  290. {
  291. // Load a string into the given namespace [or default namespace if not given]
  292. $handler =& JRegistryFormat::getInstance('XML');
  293. // If namespace is not set, get the default namespace
  294. if ($namespace == null) {
  295. $namespace = $this->_defaultNameSpace;
  296. }
  297. if (!isset($this->_registry[$namespace])) {
  298. // If namespace does not exist, make it and load the data
  299. $this->makeNameSpace($namespace);
  300. $this->_registry[$namespace]['data'] =& $handler->stringToObject($data);
  301. } else {
  302. // Get the data in object format
  303. $ns =& $handler->stringToObject($data);
  304. /*
  305. * We want to leave groups that are already in the namespace and add the
  306. * groups loaded into the namespace. This overwrites any existing group
  307. * with the same name
  308. */
  309. foreach (get_object_vars($ns) as $k => $v) {
  310. $this->_registry[$namespace]['data']->$k = $v;
  311. }
  312. }
  313. return true;
  314. }
  315. /**
  316. * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
  317. *
  318. * @access public
  319. * @param string $data INI formatted string to load into the registry
  320. * @param string $namespace Namespace to load the INI string into [optional]
  321. * @return boolean True on success
  322. * @since 1.5
  323. */
  324. function loadINI($data, $namespace = null)
  325. {
  326. // Load a string into the given namespace [or default namespace if not given]
  327. $handler =& JRegistryFormat::getInstance('INI');
  328. // If namespace is not set, get the default namespace
  329. if ($namespace == null) {
  330. $namespace = $this->_defaultNameSpace;
  331. }
  332. if (!isset($this->_registry[$namespace])) {
  333. // If namespace does not exist, make it and load the data
  334. $this->makeNameSpace($namespace);
  335. $this->_registry[$namespace]['data'] =& $handler->stringToObject($data);
  336. } else {
  337. // Get the data in object format
  338. $ns = $handler->stringToObject($data);
  339. /*
  340. * We want to leave groups that are already in the namespace and add the
  341. * groups loaded into the namespace. This overwrites any existing group
  342. * with the same name
  343. */
  344. foreach (get_object_vars($ns) as $k => $v) {
  345. $this->_registry[$namespace]['data']->$k = $v;
  346. }
  347. }
  348. return true;
  349. }
  350. /**
  351. * Merge a JRegistry object into this one
  352. *
  353. * @access public
  354. * @param object $source Source JRegistry object ot merge
  355. * @return boolean True on success
  356. * @since 1.5
  357. */
  358. function merge(&$source)
  359. {
  360. if (is_a($source, 'JRegistry'))
  361. {
  362. $sns = $source->getNameSpaces();
  363. foreach ($sns as $ns)
  364. {
  365. if (!isset($this->_registry[$ns]))
  366. {
  367. // If namespace does not exist, make it and load the data
  368. $this->makeNameSpace($ns);
  369. }
  370. // Load the variables into the registry's default namespace.
  371. foreach ($source->toArray($ns) as $k => $v)
  372. {
  373. if ($v != null) {
  374. $this->_registry[$ns]['data']->$k = $v;
  375. }
  376. }
  377. }
  378. return true;
  379. }
  380. return false;
  381. }
  382. /**
  383. * Get a namespace in a given string format
  384. *
  385. * @access public
  386. * @param string $format Format to return the string in
  387. * @param string $namespace Namespace to return [optional: null returns the default namespace]
  388. * @param mixed $params Parameters used by the formatter, see formatters for more info
  389. * @return string Namespace in string format
  390. * @since 1.5
  391. */
  392. function toString($format = 'INI', $namespace = null, $params = null)
  393. {
  394. // Return a namespace in a given format
  395. $handler =& JRegistryFormat::getInstance($format);
  396. // If namespace is not set, get the default namespace
  397. if ($namespace == null) {
  398. $namespace = $this->_defaultNameSpace;
  399. }
  400. // Get the namespace
  401. $ns = & $this->_registry[$namespace]['data'];
  402. return $handler->objectToString($ns, $params);
  403. }
  404. /**
  405. * Transforms a namespace to an array
  406. *
  407. * @access public
  408. * @param string $namespace Namespace to return [optional: null returns the default namespace]
  409. * @return array An associative array holding the namespace data
  410. * @since 1.5
  411. */
  412. function toArray($namespace = null)
  413. {
  414. // If namespace is not set, get the default namespace
  415. if ($namespace == null) {
  416. $namespace = $this->_defaultNameSpace;
  417. }
  418. // Get the namespace
  419. $ns = & $this->_registry[$namespace]['data'];
  420. $array = array();
  421. foreach (get_object_vars( $ns ) as $k => $v) {
  422. $array[$k] = $v;
  423. }
  424. return $array;
  425. }
  426. /**
  427. * Transforms a namespace to an object
  428. *
  429. * @access public
  430. * @param string $namespace Namespace to return [optional: null returns the default namespace]
  431. * @return object An an object holding the namespace data
  432. * @since 1.5
  433. */
  434. function toObject($namespace = null)
  435. {
  436. // If namespace is not set, get the default namespace
  437. if ($namespace == null) {
  438. $namespace = $this->_defaultNameSpace;
  439. }
  440. // Get the namespace
  441. $ns = & $this->_registry[$namespace]['data'];
  442. return $ns;
  443. }
  444. function __clone()
  445. {
  446. $this->_registry = unserialize(serialize($this->_registry));
  447. }
  448. }