3 * @version $Id: registry.php 1971 2009-06-06 08:26:57Z fishbone $
4 * @package Joomla.Framework
6 * @copyright Copyright (C) 2005 - 2008 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.
15 // Check to ensure this file is within the rest of the framework
16 //defined('JPATH_BASE') or die();
18 //Register the session storage class with the loader
19 //JLoader::register('JRegistryFormat', dirname(__FILE__).DS.'format.php');
24 * @package Joomla.Framework
25 * @subpackage Registry
28 class JRegistry extends JObject
34 var $_defaultNameSpace = null;
38 * - actually an array of namespace objects
41 var $_registry = array ();
47 * @param string $namespace Default registry namespace
51 function __construct($namespace = 'default')
53 $this->_defaultNameSpace = $namespace;
54 $this->makeNameSpace($namespace);
58 * Returns a reference to a global JRegistry object, only creating it
59 * if it doesn't already exist.
61 * This method must be invoked as:
62 * <pre>$registry =& JRegistry::getInstance($id[, $namespace]);</pre>
65 * @param string $id An ID for the registry instance
66 * @param string $namespace The default namespace for the registry object [optional]
67 * @return object The JRegistry object.
70 function &getInstance($id, $namespace = 'default')
74 if (!isset ($instances)) {
75 $instances = array ();
78 if (empty ($instances[$id])) {
79 $instances[$id] = new JRegistry($namespace);
82 return $instances[$id];
89 * @param string $namespace Name of the namespace to create
90 * @return boolean True on success
93 function makeNameSpace($namespace)
95 $this->_registry[$namespace] = array('data' => new stdClass());
100 * Get the list of namespaces
103 * @return array List of namespaces
106 function getNameSpaces()
108 return array_keys($this->_registry);
112 * Get a registry value
115 * @param string $regpath Registry path (e.g. joomla.content.showauthor)
116 * @param mixed $default Optional default value
117 * @return mixed Value of entry or null
120 function getValue($regpath, $default=null)
124 // Explode the registry path into an array
125 if ($nodes = explode('.', $regpath))
128 //$namespace = array_shift($nodes);
129 $count = count($nodes);
131 $namespace = $this->_defaultNameSpace;
132 $nodes[1] = $nodes[0];
134 $namespace = $nodes[0];
137 if (isset($this->_registry[$namespace])) {
138 $ns = & $this->_registry[$namespace]['data'];
139 $pathNodes = $count - 1;
141 //for ($i = 0; $i < $pathNodes; $i ++) {
142 for ($i = 1; $i < $pathNodes; $i ++) {
143 if((isset($ns->$nodes[$i]))) $ns =& $ns->$nodes[$i];
146 if(isset($ns->$nodes[$i])) {
147 $result = $ns->$nodes[$i];
155 * Set a registry value
158 * @param string $regpath Registry Path (e.g. joomla.content.showauthor)
159 * @param mixed $value Value of entry
160 * @return mixed Value of old value or boolean false if operation failed
163 function setValue($regpath, $value)
165 // Explode the registry path into an array
166 $nodes = explode('.', $regpath);
169 $count = count($nodes);
172 $namespace = $this->_defaultNameSpace;
174 $namespace = array_shift($nodes);
178 if (!isset($this->_registry[$namespace])) {
179 $this->makeNameSpace($namespace);
182 $ns = & $this->_registry[$namespace]['data'];
184 $pathNodes = $count - 1;
186 if ($pathNodes < 0) {
190 for ($i = 0; $i < $pathNodes; $i ++)
192 // If any node along the registry path does not exist, create it
193 if (!isset($ns->$nodes[$i])) {
194 $ns->$nodes[$i] = new stdClass();
196 $ns =& $ns->$nodes[$i];
199 // Get the old value if exists so we can return it
200 $ns->$nodes[$i] =& $value;
202 return $ns->$nodes[$i];
206 * Load a associative array of values into the default namespace
209 * @param array $array Associative array of value to load
210 * @param string $namepsace The name of the namespace
211 * @return boolean True on success
214 function loadArray($array, $namespace = null)
216 // If namespace is not set, get the default namespace
217 if ($namespace == null) {
218 $namespace = $this->_defaultNameSpace;
221 if (!isset($this->_registry[$namespace])) {
222 // If namespace does not exist, make it and load the data
223 $this->makeNameSpace($namespace);
226 // Load the variables into the registry's default namespace.
227 foreach ($array as $k => $v)
229 $this->_registry[$namespace]['data']->$k = $v;
236 * Load the public variables of the object into the default namespace.
239 * @param object $object The object holding the public vars to load
240 * @param string $namespace Namespace to load the INI string into [optional]
241 * @return boolean True on success
244 function loadObject(&$object, $namespace = null)
246 // If namespace is not set, get the default namespace
247 if ($namespace == null) {
248 $namespace = $this->_defaultNameSpace;
251 if (!isset($this->_registry[$namespace])) {
252 // If namespace does not exist, make it and load the data
253 $this->makeNameSpace($namespace);
257 * We want to leave groups that are already in the namespace and add the
258 * groups loaded into the namespace. This overwrites any existing group
261 if (is_object( $object ))
263 foreach (get_object_vars($object) as $k => $v) {
264 if (substr($k, 0,1) != '_' || $k == '_name') {
265 $this->_registry[$namespace]['data']->$k = $v;
274 * Load the contents of a file into the registry
277 * @param string $file Path to file to load
278 * @param string $format Format of the file [optional: defaults to INI]
279 * @param string $namespace Namespace to load the INI string into [optional]
280 * @return boolean True on success
283 function loadFile($file, $format = 'INI', $namespace = null)
285 // Load a file into the given namespace [or default namespace if not given]
286 $handler =& JRegistryFormat::getInstance($format);
288 // If namespace is not set, get the default namespace
289 if ($namespace == null) {
290 $namespace = $this->_defaultNameSpace;
293 // Get the contents of the file
294 jimport('joomla.filesystem.file');
295 $data = JFile::read($file);
297 if (!isset($this->_registry[$namespace]))
299 // If namespace does not exist, make it and load the data
300 $this->makeNameSpace($namespace);
301 $this->_registry[$namespace]['data'] = $handler->stringToObject($data);
305 // Get the data in object format
306 $ns = $handler->stringToObject($data);
309 * We want to leave groups that are already in the namespace and add the
310 * groups loaded into the namespace. This overwrites any existing group
313 foreach (get_object_vars($ns) as $k => $v) {
314 $this->_registry[$namespace]['data']->$k = $v;
322 * Load an XML string into the registry into the given namespace [or default if a namespace is not given]
325 * @param string $data XML formatted string to load into the registry
326 * @param string $namespace Namespace to load the XML string into [optional]
327 * @return boolean True on success
330 function loadXML($data, $namespace = null)
332 // Load a string into the given namespace [or default namespace if not given]
333 $handler =& JRegistryFormat::getInstance('XML');
335 // If namespace is not set, get the default namespace
336 if ($namespace == null) {
337 $namespace = $this->_defaultNameSpace;
340 if (!isset($this->_registry[$namespace])) {
341 // If namespace does not exist, make it and load the data
342 $this->makeNameSpace($namespace);
343 $this->_registry[$namespace]['data'] =& $handler->stringToObject($data);
345 // Get the data in object format
346 $ns =& $handler->stringToObject($data);
349 * We want to leave groups that are already in the namespace and add the
350 * groups loaded into the namespace. This overwrites any existing group
353 foreach (get_object_vars($ns) as $k => $v) {
354 $this->_registry[$namespace]['data']->$k = $v;
362 * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
365 * @param string $data INI formatted string to load into the registry
366 * @param string $namespace Namespace to load the INI string into [optional]
367 * @return boolean True on success
370 function loadINI($data, $namespace = null)
372 // Load a string into the given namespace [or default namespace if not given]
373 $handler =& JRegistryFormat::getInstance('INI');
375 // If namespace is not set, get the default namespace
376 if ($namespace == null) {
377 $namespace = $this->_defaultNameSpace;
380 if (!isset($this->_registry[$namespace])) {
381 // If namespace does not exist, make it and load the data
382 $this->makeNameSpace($namespace);
383 $this->_registry[$namespace]['data'] =& $handler->stringToObject($data);
385 // Get the data in object format
386 $ns = $handler->stringToObject($data);
389 * We want to leave groups that are already in the namespace and add the
390 * groups loaded into the namespace. This overwrites any existing group
393 foreach (get_object_vars($ns) as $k => $v) {
394 $this->_registry[$namespace]['data']->$k = $v;
402 * Merge a JRegistry object into this one
405 * @param object $source Source JRegistry object ot merge
406 * @return boolean True on success
409 function merge(&$source)
411 if (is_a($source, 'JRegistry'))
413 $sns = $source->getNameSpaces();
414 foreach ($sns as $ns)
416 if (!isset($this->_registry[$ns]))
418 // If namespace does not exist, make it and load the data
419 $this->makeNameSpace($ns);
422 // Load the variables into the registry's default namespace.
423 foreach ($source->toArray($ns) as $k => $v)
426 $this->_registry[$ns]['data']->$k = $v;
436 * Get a namespace in a given string format
439 * @param string $format Format to return the string in
440 * @param string $namespace Namespace to return [optional: null returns the default namespace]
441 * @param mixed $params Parameters used by the formatter, see formatters for more info
442 * @return string Namespace in string format
445 function toString($format = 'INI', $namespace = null, $params = null)
447 // Return a namespace in a given format
448 $handler =& JRegistryFormat::getInstance($format);
450 // If namespace is not set, get the default namespace
451 if ($namespace == null) {
452 $namespace = $this->_defaultNameSpace;
456 $ns = & $this->_registry[$namespace]['data'];
458 return $handler->objectToString($ns, $params);
462 * Transforms a namespace to an array
465 * @param string $namespace Namespace to return [optional: null returns the default namespace]
466 * @return array An associative array holding the namespace data
469 function toArray($namespace = null)
472 // If namespace is not set, get the default namespace
473 if ($namespace == null) {
474 $namespace = $this->_defaultNameSpace;
478 $ns = & $this->_registry[$namespace]['data'];
481 foreach (get_object_vars( $ns ) as $k => $v) {
489 * Transforms a namespace to an object
492 * @param string $namespace Namespace to return [optional: null returns the default namespace]
493 * @return object An an object holding the namespace data
496 function toObject($namespace = null)
498 // If namespace is not set, get the default namespace
499 if ($namespace == null) {
500 $namespace = $this->_defaultNameSpace;
504 $ns = & $this->_registry[$namespace]['data'];
511 $this->_registry = unserialize(serialize($this->_registry));