You've already forked joomla_test
first commit
This commit is contained in:
89
libraries/joomla/registry/format.php
Normal file
89
libraries/joomla/registry/format.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* Abstract Format for JRegistry
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
* @since 11.1
|
||||
*/
|
||||
abstract class JRegistryFormat
|
||||
{
|
||||
/**
|
||||
* @var array JRegistryFormat instances container.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected static $instances = array();
|
||||
|
||||
/**
|
||||
* Returns a reference to a Format object, only creating it
|
||||
* if it doesn't already exist.
|
||||
*
|
||||
* @param string $type The format to load
|
||||
*
|
||||
* @return JRegistryFormat Registry format handler
|
||||
*
|
||||
* @since 11.1
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function getInstance($type)
|
||||
{
|
||||
// Sanitize format type.
|
||||
$type = strtolower(preg_replace('/[^A-Z0-9_]/i', '', $type));
|
||||
|
||||
// Only instantiate the object if it doesn't already exist.
|
||||
if (!isset(self::$instances[$type]))
|
||||
{
|
||||
// Only load the file if the class does not exist.
|
||||
$class = 'JRegistryFormat' . $type;
|
||||
if (!class_exists($class))
|
||||
{
|
||||
$path = __DIR__ . '/format/' . $type . '.php';
|
||||
if (is_file($path))
|
||||
{
|
||||
include_once $path;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidArgumentException('Unable to load format class.', 500);
|
||||
}
|
||||
}
|
||||
|
||||
self::$instances[$type] = new $class;
|
||||
}
|
||||
return self::$instances[$type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an object into a formatted string.
|
||||
*
|
||||
* @param object $object Data Source Object.
|
||||
* @param array $options An array of options for the formatter.
|
||||
*
|
||||
* @return string Formatted string.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
abstract public function objectToString($object, $options = null);
|
||||
|
||||
/**
|
||||
* Converts a formatted string into an object.
|
||||
*
|
||||
* @param string $data Formatted string
|
||||
* @param array $options An array of options for the formatter.
|
||||
*
|
||||
* @return object Data Object
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
abstract public function stringToObject($data, array $options = array());
|
||||
}
|
1
libraries/joomla/registry/format/index.html
Normal file
1
libraries/joomla/registry/format/index.html
Normal file
@ -0,0 +1 @@
|
||||
<!DOCTYPE html><title></title>
|
230
libraries/joomla/registry/format/ini.php
Normal file
230
libraries/joomla/registry/format/ini.php
Normal file
@ -0,0 +1,230 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* INI format handler for JRegistry.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
* @since 11.1
|
||||
*/
|
||||
class JRegistryFormatINI extends JRegistryFormat
|
||||
{
|
||||
protected static $cache = array();
|
||||
|
||||
/**
|
||||
* Converts an object into an INI formatted string
|
||||
* - Unfortunately, there is no way to have ini values nested further than two
|
||||
* levels deep. Therefore we will only go through the first two levels of
|
||||
* the object.
|
||||
*
|
||||
* @param object $object Data source object.
|
||||
* @param array $options Options used by the formatter.
|
||||
*
|
||||
* @return string INI formatted string.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function objectToString($object, $options = array())
|
||||
{
|
||||
$local = array();
|
||||
$global = array();
|
||||
|
||||
// Iterate over the object to set the properties.
|
||||
foreach (get_object_vars($object) as $key => $value)
|
||||
{
|
||||
// If the value is an object then we need to put it in a local section.
|
||||
if (is_object($value))
|
||||
{
|
||||
// Add the section line.
|
||||
$local[] = '';
|
||||
$local[] = '[' . $key . ']';
|
||||
|
||||
// Add the properties for this section.
|
||||
foreach (get_object_vars($value) as $k => $v)
|
||||
{
|
||||
$local[] = $k . '=' . $this->getValueAsINI($v);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not in a section so add the property to the global array.
|
||||
$global[] = $key . '=' . $this->getValueAsINI($value);
|
||||
}
|
||||
}
|
||||
|
||||
return implode("\n", array_merge($global, $local));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an INI formatted string and convert it into an object.
|
||||
*
|
||||
* @param string $data INI formatted string to convert.
|
||||
* @param mixed $options An array of options used by the formatter, or a boolean setting to process sections.
|
||||
*
|
||||
* @return object Data object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function stringToObject($data, array $options = array())
|
||||
{
|
||||
$sections = (isset($options['processSections'])) ? $options['processSections'] : false;
|
||||
|
||||
// Check the memory cache for already processed strings.
|
||||
$hash = md5($data . ':' . (int) $sections);
|
||||
if (isset(self::$cache[$hash]))
|
||||
{
|
||||
return self::$cache[$hash];
|
||||
}
|
||||
|
||||
// If no lines present just return the object.
|
||||
if (empty($data))
|
||||
{
|
||||
return new stdClass;
|
||||
}
|
||||
|
||||
$obj = new stdClass;
|
||||
$section = false;
|
||||
$lines = explode("\n", $data);
|
||||
|
||||
// Process the lines.
|
||||
foreach ($lines as $line)
|
||||
{
|
||||
// Trim any unnecessary whitespace.
|
||||
$line = trim($line);
|
||||
|
||||
// Ignore empty lines and comments.
|
||||
if (empty($line) || ($line{0} == ';'))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($sections)
|
||||
{
|
||||
$length = strlen($line);
|
||||
|
||||
// If we are processing sections and the line is a section add the object and continue.
|
||||
if (($line[0] == '[') && ($line[$length - 1] == ']'))
|
||||
{
|
||||
$section = substr($line, 1, $length - 2);
|
||||
$obj->$section = new stdClass;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
elseif ($line{0} == '[')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check that an equal sign exists and is not the first character of the line.
|
||||
if (!strpos($line, '='))
|
||||
{
|
||||
// Maybe throw exception?
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the key and value for the line.
|
||||
list ($key, $value) = explode('=', $line, 2);
|
||||
|
||||
// Validate the key.
|
||||
if (preg_match('/[^A-Z0-9_]/i', $key))
|
||||
{
|
||||
// Maybe throw exception?
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the value is quoted then we assume it is a string.
|
||||
$length = strlen($value);
|
||||
if ($length && ($value[0] == '"') && ($value[$length - 1] == '"'))
|
||||
{
|
||||
// Strip the quotes and Convert the new line characters.
|
||||
$value = stripcslashes(substr($value, 1, ($length - 2)));
|
||||
$value = str_replace('\n', "\n", $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the value is not quoted, we assume it is not a string.
|
||||
|
||||
// If the value is 'false' assume boolean false.
|
||||
if ($value == 'false')
|
||||
{
|
||||
$value = false;
|
||||
}
|
||||
// If the value is 'true' assume boolean true.
|
||||
elseif ($value == 'true')
|
||||
{
|
||||
$value = true;
|
||||
}
|
||||
// If the value is numeric than it is either a float or int.
|
||||
elseif (is_numeric($value))
|
||||
{
|
||||
// If there is a period then we assume a float.
|
||||
if (strpos($value, '.') !== false)
|
||||
{
|
||||
$value = (float) $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$value = (int) $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a section is set add the key/value to the section, otherwise top level.
|
||||
if ($section)
|
||||
{
|
||||
$obj->$section->$key = $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$obj->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache the string to save cpu cycles -- thus the world :)
|
||||
self::$cache[$hash] = clone ($obj);
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a value in an INI format.
|
||||
*
|
||||
* @param mixed $value The value to convert to INI format.
|
||||
*
|
||||
* @return string The value in INI format.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
protected function getValueAsINI($value)
|
||||
{
|
||||
$string = '';
|
||||
|
||||
switch (gettype($value))
|
||||
{
|
||||
case 'integer':
|
||||
case 'double':
|
||||
$string = $value;
|
||||
break;
|
||||
|
||||
case 'boolean':
|
||||
$string = $value ? 'true' : 'false';
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
// Sanitize any CRLF characters..
|
||||
$string = '"' . str_replace(array("\r\n", "\n"), '\\n', $value) . '"';
|
||||
break;
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
62
libraries/joomla/registry/format/json.php
Normal file
62
libraries/joomla/registry/format/json.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* JSON format handler for JRegistry.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
* @since 11.1
|
||||
*/
|
||||
class JRegistryFormatJSON extends JRegistryFormat
|
||||
{
|
||||
/**
|
||||
* Converts an object into a JSON formatted string.
|
||||
*
|
||||
* @param object $object Data source object.
|
||||
* @param array $options Options used by the formatter.
|
||||
*
|
||||
* @return string JSON formatted string.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function objectToString($object, $options = array())
|
||||
{
|
||||
return json_encode($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a JSON formatted string and convert it into an object.
|
||||
*
|
||||
* If the string is not in JSON format, this method will attempt to parse it as INI format.
|
||||
*
|
||||
* @param string $data JSON formatted string to convert.
|
||||
* @param array $options Options used by the formatter.
|
||||
*
|
||||
* @return object Data object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function stringToObject($data, array $options = array('processSections' => false))
|
||||
{
|
||||
$data = trim($data);
|
||||
if ((substr($data, 0, 1) != '{') && (substr($data, -1, 1) != '}'))
|
||||
{
|
||||
$ini = JRegistryFormat::getInstance('INI');
|
||||
$obj = $ini->stringToObject($data, $options);
|
||||
}
|
||||
else
|
||||
{
|
||||
$obj = json_decode($data);
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
}
|
106
libraries/joomla/registry/format/php.php
Normal file
106
libraries/joomla/registry/format/php.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* PHP class format handler for JRegistry
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
* @since 11.1
|
||||
*/
|
||||
class JRegistryFormatPHP extends JRegistryFormat
|
||||
{
|
||||
/**
|
||||
* Converts an object into a php class string.
|
||||
* - NOTE: Only one depth level is supported.
|
||||
*
|
||||
* @param object $object Data Source Object
|
||||
* @param array $params Parameters used by the formatter
|
||||
*
|
||||
* @return string Config class formatted string
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function objectToString($object, $params = array())
|
||||
{
|
||||
// Build the object variables string
|
||||
$vars = '';
|
||||
foreach (get_object_vars($object) as $k => $v)
|
||||
{
|
||||
if (is_scalar($v))
|
||||
{
|
||||
$vars .= "\tpublic $" . $k . " = '" . addcslashes($v, '\\\'') . "';\n";
|
||||
}
|
||||
elseif (is_array($v) || is_object($v))
|
||||
{
|
||||
$vars .= "\tpublic $" . $k . " = " . $this->getArrayString((array) $v) . ";\n";
|
||||
}
|
||||
}
|
||||
|
||||
$str = "<?php\nclass " . $params['class'] . " {\n";
|
||||
$str .= $vars;
|
||||
$str .= "}";
|
||||
|
||||
// Use the closing tag if it not set to false in parameters.
|
||||
if (!isset($params['closingtag']) || $params['closingtag'] !== false)
|
||||
{
|
||||
$str .= "\n?>";
|
||||
}
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a PHP class formatted string and convert it into an object.
|
||||
*
|
||||
* @param string $data PHP Class formatted string to convert.
|
||||
* @param array $options Options used by the formatter.
|
||||
*
|
||||
* @return object Data object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function stringToObject($data, array $options = array())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array as an exported string.
|
||||
*
|
||||
* @param array $a The array to get as a string.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
protected function getArrayString($a)
|
||||
{
|
||||
$s = 'array(';
|
||||
$i = 0;
|
||||
foreach ($a as $k => $v)
|
||||
{
|
||||
$s .= ($i) ? ', ' : '';
|
||||
$s .= '"' . $k . '" => ';
|
||||
if (is_array($v) || is_object($v))
|
||||
{
|
||||
$s .= $this->getArrayString((array) $v);
|
||||
}
|
||||
else
|
||||
{
|
||||
$s .= '"' . addslashes($v) . '"';
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
$s .= ')';
|
||||
return $s;
|
||||
}
|
||||
}
|
151
libraries/joomla/registry/format/xml.php
Normal file
151
libraries/joomla/registry/format/xml.php
Normal file
@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* XML format handler for JRegistry.
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
* @since 11.1
|
||||
*/
|
||||
class JRegistryFormatXML extends JRegistryFormat
|
||||
{
|
||||
/**
|
||||
* Converts an object into an XML formatted string.
|
||||
* - If more than two levels of nested groups are necessary, since INI is not
|
||||
* useful, XML or another format should be used.
|
||||
*
|
||||
* @param object $object Data source object.
|
||||
* @param array $options Options used by the formatter.
|
||||
*
|
||||
* @return string XML formatted string.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function objectToString($object, $options = array())
|
||||
{
|
||||
$rootName = (isset($options['name'])) ? $options['name'] : 'registry';
|
||||
$nodeName = (isset($options['nodeName'])) ? $options['nodeName'] : 'node';
|
||||
|
||||
// Create the root node.
|
||||
$root = simplexml_load_string('<' . $rootName . ' />');
|
||||
|
||||
// Iterate over the object members.
|
||||
$this->getXmlChildren($root, $object, $nodeName);
|
||||
|
||||
return $root->asXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a XML formatted string and convert it into an object.
|
||||
*
|
||||
* @param string $data XML formatted string to convert.
|
||||
* @param array $options Options used by the formatter.
|
||||
*
|
||||
* @return object Data object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function stringToObject($data, array $options = array())
|
||||
{
|
||||
$obj = new stdClass;
|
||||
|
||||
// Parse the XML string.
|
||||
$xml = simplexml_load_string($data);
|
||||
|
||||
foreach ($xml->children() as $node)
|
||||
{
|
||||
$obj->$node['name'] = $this->getValueFromNode($node);
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a PHP native value for a SimpleXMLElement object. -- called recursively
|
||||
*
|
||||
* @param object $node SimpleXMLElement object for which to get the native value.
|
||||
*
|
||||
* @return mixed Native value of the SimpleXMLElement object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
protected function getValueFromNode($node)
|
||||
{
|
||||
switch ($node['type'])
|
||||
{
|
||||
case 'integer':
|
||||
$value = (string) $node;
|
||||
return (int) $value;
|
||||
break;
|
||||
case 'string':
|
||||
return (string) $node;
|
||||
break;
|
||||
case 'boolean':
|
||||
$value = (string) $node;
|
||||
return (bool) $value;
|
||||
break;
|
||||
case 'double':
|
||||
$value = (string) $node;
|
||||
return (float) $value;
|
||||
break;
|
||||
case 'array':
|
||||
$value = array();
|
||||
foreach ($node->children() as $child)
|
||||
{
|
||||
$value[(string) $child['name']] = $this->getValueFromNode($child);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$value = new stdClass;
|
||||
foreach ($node->children() as $child)
|
||||
{
|
||||
$value->$child['name'] = $this->getValueFromNode($child);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to build a level of the XML string -- called recursively
|
||||
*
|
||||
* @param SimpleXMLElement $node SimpleXMLElement object to attach children.
|
||||
* @param object $var Object that represents a node of the XML document.
|
||||
* @param string $nodeName The name to use for node elements.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
protected function getXmlChildren(SimpleXMLElement $node, $var, $nodeName)
|
||||
{
|
||||
// Iterate over the object members.
|
||||
foreach ((array) $var as $k => $v)
|
||||
{
|
||||
if (is_scalar($v))
|
||||
{
|
||||
$n = $node->addChild($nodeName, $v);
|
||||
$n->addAttribute('name', $k);
|
||||
$n->addAttribute('type', gettype($v));
|
||||
}
|
||||
else
|
||||
{
|
||||
$n = $node->addChild($nodeName);
|
||||
$n->addAttribute('name', $k);
|
||||
$n->addAttribute('type', gettype($v));
|
||||
|
||||
$this->getXmlChildren($n, $v, $nodeName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
libraries/joomla/registry/index.html
Normal file
1
libraries/joomla/registry/index.html
Normal file
@ -0,0 +1 @@
|
||||
<!DOCTYPE html><title></title>
|
470
libraries/joomla/registry/registry.php
Normal file
470
libraries/joomla/registry/registry.php
Normal file
@ -0,0 +1,470 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
jimport('joomla.utilities.arrayhelper');
|
||||
|
||||
/**
|
||||
* JRegistry class
|
||||
*
|
||||
* @package Joomla.Platform
|
||||
* @subpackage Registry
|
||||
* @since 11.1
|
||||
*/
|
||||
class JRegistry implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Registry Object
|
||||
*
|
||||
* @var object
|
||||
* @since 11.1
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @var array JRegistry instances container.
|
||||
* @since 11.3
|
||||
*/
|
||||
protected static $instances = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed $data The data to bind to the new JRegistry object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function __construct($data = null)
|
||||
{
|
||||
// Instantiate the internal data object.
|
||||
$this->data = new stdClass;
|
||||
|
||||
// Optionally load supplied data.
|
||||
if (is_array($data) || is_object($data))
|
||||
{
|
||||
$this->bindData($this->data, $data);
|
||||
}
|
||||
elseif (!empty($data) && is_string($data))
|
||||
{
|
||||
$this->loadString($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function to clone the registry object.
|
||||
*
|
||||
* @return JRegistry
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
$this->data = unserialize(serialize($this->data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function to render this object as a string using default args of toString method.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation for the JsonSerializable interface.
|
||||
* Allows us to pass JRegistry objects to json_encode.
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @since 12.2
|
||||
* @note The interface is only present in PHP 5.4 and up.
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a default value if not already assigned.
|
||||
*
|
||||
* @param string $key The name of the parameter.
|
||||
* @param string $default An optional value for the parameter.
|
||||
*
|
||||
* @return string The value set, or the default if the value was not previously set (or null).
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function def($key, $default = '')
|
||||
{
|
||||
$value = $this->get($key, (string) $default);
|
||||
$this->set($key, $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a registry path exists.
|
||||
*
|
||||
* @param string $path Registry path (e.g. joomla.content.showauthor)
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function exists($path)
|
||||
{
|
||||
// Explode the registry path into an array
|
||||
if ($nodes = explode('.', $path))
|
||||
{
|
||||
// Initialize the current node to be the registry root.
|
||||
$node = $this->data;
|
||||
|
||||
// Traverse the registry to find the correct node for the result.
|
||||
for ($i = 0, $n = count($nodes); $i < $n; $i++)
|
||||
{
|
||||
if (isset($node->$nodes[$i]))
|
||||
{
|
||||
$node = $node->$nodes[$i];
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if ($i + 1 == $n)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a registry value.
|
||||
*
|
||||
* @param string $path Registry path (e.g. joomla.content.showauthor)
|
||||
* @param mixed $default Optional default value, returned if the internal value is null.
|
||||
*
|
||||
* @return mixed Value of entry or null
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function get($path, $default = null)
|
||||
{
|
||||
$result = $default;
|
||||
|
||||
if (!strpos($path, '.'))
|
||||
{
|
||||
return (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') ? $this->data->$path : $default;
|
||||
}
|
||||
// Explode the registry path into an array
|
||||
$nodes = explode('.', $path);
|
||||
|
||||
// Initialize the current node to be the registry root.
|
||||
$node = $this->data;
|
||||
$found = false;
|
||||
|
||||
// Traverse the registry to find the correct node for the result.
|
||||
foreach ($nodes as $n)
|
||||
{
|
||||
if (isset($node->$n))
|
||||
{
|
||||
$node = $node->$n;
|
||||
$found = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
$found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($found && $node !== null && $node !== '')
|
||||
{
|
||||
$result = $node;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);</pre>
|
||||
*
|
||||
* @param string $id An ID for the registry instance
|
||||
*
|
||||
* @return JRegistry The JRegistry object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public static function getInstance($id)
|
||||
{
|
||||
if (empty(self::$instances[$id]))
|
||||
{
|
||||
self::$instances[$id] = new JRegistry;
|
||||
}
|
||||
|
||||
return self::$instances[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an associative array of values into the default namespace
|
||||
*
|
||||
* @param array $array Associative array of value to load
|
||||
*
|
||||
* @return boolean True on success
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function loadArray($array)
|
||||
{
|
||||
$this->bindData($this->data, $array);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the public variables of the object into the default namespace.
|
||||
*
|
||||
* @param object $object The object holding the publics to load
|
||||
*
|
||||
* @return boolean True on success
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function loadObject($object)
|
||||
{
|
||||
$this->bindData($this->data, $object);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the contents of a file into the registry
|
||||
*
|
||||
* @param string $file Path to file to load
|
||||
* @param string $format Format of the file [optional: defaults to JSON]
|
||||
* @param array $options Options used by the formatter
|
||||
*
|
||||
* @return boolean True on success
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function loadFile($file, $format = 'JSON', $options = array())
|
||||
{
|
||||
$data = file_get_contents($file);
|
||||
|
||||
return $this->loadString($data, $format, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a string into the registry
|
||||
*
|
||||
* @param string $data String to load into the registry
|
||||
* @param string $format Format of the string
|
||||
* @param array $options Options used by the formatter
|
||||
*
|
||||
* @return boolean True on success
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function loadString($data, $format = 'JSON', $options = array())
|
||||
{
|
||||
// Load a string into the given namespace [or default namespace if not given]
|
||||
$handler = JRegistryFormat::getInstance($format);
|
||||
|
||||
$obj = $handler->stringToObject($data, $options);
|
||||
$this->loadObject($obj);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge a JRegistry object into this one
|
||||
*
|
||||
* @param JRegistry $source Source JRegistry object to merge.
|
||||
*
|
||||
* @return boolean True on success
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function merge($source)
|
||||
{
|
||||
if (!$source instanceof JRegistry)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load the variables into the registry's default namespace.
|
||||
foreach ($source->toArray() as $k => $v)
|
||||
{
|
||||
if (($v !== null) && ($v !== ''))
|
||||
{
|
||||
$this->data->$k = $v;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a registry value.
|
||||
*
|
||||
* @param string $path Registry Path (e.g. joomla.content.showauthor)
|
||||
* @param mixed $value Value of entry
|
||||
*
|
||||
* @return mixed The value of the that has been set.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function set($path, $value)
|
||||
{
|
||||
$result = null;
|
||||
|
||||
/**
|
||||
* Explode the registry path into an array and remove empty
|
||||
* nodes, then re-key the array so it's sequential.
|
||||
*/
|
||||
$nodes = array_values(array_filter(explode('.', $path), 'strlen'));
|
||||
|
||||
if ($nodes)
|
||||
{
|
||||
// Initialize the current node to be the registry root.
|
||||
$node = $this->data;
|
||||
|
||||
// Traverse the registry to find the correct node for the result.
|
||||
for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
|
||||
{
|
||||
if (!isset($node->$nodes[$i]) && ($i != $n))
|
||||
{
|
||||
$node->$nodes[$i] = new stdClass;
|
||||
}
|
||||
$node = $node->$nodes[$i];
|
||||
}
|
||||
|
||||
// Get the old value if exists so we can return it
|
||||
$result = $node->$nodes[$i] = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a namespace to an array
|
||||
*
|
||||
* @return array An associative array holding the namespace data
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return (array) $this->asArray($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a namespace to an object
|
||||
*
|
||||
* @return object An an object holding the namespace data
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a namespace in a given string format
|
||||
*
|
||||
* @param string $format Format to return the string in
|
||||
* @param mixed $options Parameters used by the formatter, see formatters for more info
|
||||
*
|
||||
* @return string Namespace in string format
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
public function toString($format = 'JSON', $options = array())
|
||||
{
|
||||
// Return a namespace in a given format
|
||||
$handler = JRegistryFormat::getInstance($format);
|
||||
|
||||
return $handler->objectToString($this->data, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to recursively bind data to a parent object.
|
||||
*
|
||||
* @param object $parent The parent object on which to attach the data values.
|
||||
* @param mixed $data An array or object of data to bind to the parent object.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
protected function bindData($parent, $data)
|
||||
{
|
||||
// Ensure the input data is an array.
|
||||
if (is_object($data))
|
||||
{
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
foreach ($data as $k => $v)
|
||||
{
|
||||
if ((is_array($v) && JArrayHelper::isAssociative($v)) || is_object($v))
|
||||
{
|
||||
$parent->$k = new stdClass;
|
||||
$this->bindData($parent->$k, $v);
|
||||
}
|
||||
else
|
||||
{
|
||||
$parent->$k = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to recursively convert an object of data to an array.
|
||||
*
|
||||
* @param object $data An object of data to return as an array.
|
||||
*
|
||||
* @return array Array representation of the input object.
|
||||
*
|
||||
* @since 11.1
|
||||
*/
|
||||
protected function asArray($data)
|
||||
{
|
||||
$array = array();
|
||||
|
||||
foreach (get_object_vars((object) $data) as $k => $v)
|
||||
{
|
||||
if (is_object($v))
|
||||
{
|
||||
$array[$k] = $this->asArray($v);
|
||||
}
|
||||
else
|
||||
{
|
||||
$array[$k] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user