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