- md5
- 14b63787726d91db405c248ba0e43727
- sha1
- 20da8e194bcfa7c91be23ef9a624d16c34766822
- sha256
- 5aa4a9bbfa18d264870b79347052603f02ae5a648c912cc481f878e9f8087f71
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
<?php
/**
* Part of the Joomla Framework Registry Package
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Registry;
\jimport('joomla.utilities.arrayhelper');
/**
* Registry class
*
* @since 1.0
*/
class Registry implements \JsonSerializable, \ArrayAccess
{
/**
* Registry Object
*
* @var object
* @since 1.0
*/
protected $data;
/**
* Registry instances container.
*
* @var array
* @since 1.0
*/
protected static $instances = array();
/**
* Constructor
*
* @param mixed $data The data to bind to the new Registry object.
*
* @since 1.0
*/
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 Registry
*
* @since 1.0
*/
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 1.0
*/
public function __toString()
{
return $this->toString();
}
/**
* Implementation for the JsonSerializable interface.
* Allows us to pass Registry objects to json_encode.
*
* @return object
*
* @since 1.0
* @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 mixed $default An optional value for the parameter.
*
* @return mixed The value set, or the default if the value was not previously set (or null).
*
* @since 1.0
*/
public function def($key, $default = '')
{
$value = $this->get($key, $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 1.0
*/
public function exists($path)
{
// Explode the registry path into an array
$nodes = explode('.', $path);
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); $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 1.0
*/
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 Registry object, only creating it
* if it doesn't already exist.
*
* This method must be invoked as:
* <pre>$registry = Registry::getInstance($id);</pre>
*
* @param string $id An ID for the registry instance
*
* @return Registry The Registry object.
*
* @since 1.0
*/
public static function getInstance($id)
{
if (empty(self::$instances[$id]))
{
self::$instances[$id] = new self;
}
return self::$instances[$id];
}
/**
* Load a associative array of values into the default namespace
*
* @param array $array Associative array of value to load
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadArray($array)
{
$this->bindData($this->data, $array);
return $this;
}
/**
* Load the public variables of the object into the default namespace.
*
* @param object $object The object holding the publics to load
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadObject($object)
{
$this->bindData($this->data, $object);
return $this;
}
/**
* 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 Registry Return this object to support chaining.
*
* @since 1.0
*/
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 Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadString($data, $format = 'JSON', $options = array())
{
// Load a string into the given namespace [or default namespace if not given]
$handler = AbstractRegistryFormat::getInstance($format);
$obj = $handler->stringToObject($data, $options);
$this->loadObject($obj);
return $this;
}
/**
* Merge a Registry object into this one
*
* @param Registry $source Source Registry object to merge. [@note Framework typehints this param]
* @param boolean $recursive True to support recursive merge the children values.
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function merge($source, $recursive = false)
{
if (!$source instanceof Registry)
{
return false;
}
$data = $source->toArray();
$array = array();
foreach ($data as $k => $v)
{
if (($v !== null) && ($v !== ''))
{
$array[$k] = $v;
}
}
$this->bindData($this->data, $array, $recursive);
return $this;
}
/**
* Checks whether an offset exists in the iterator.
*
* @param mixed $offset The array offset.
*
* @return boolean True if the offset exists, false otherwise.
*
* @since 1.0
*/
public function offsetExists($offset)
{
return (boolean) ($this->get($offset) !== null);
}
/**
* Gets an offset in the iterator.
*
* @param mixed $offset The array offset.
*
* @return mixed The array value if it exists, null otherwise.
*
* @since 1.0
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Sets an offset in the iterator.
*
* @param mixed $offset The array offset.
* @param mixed $value The array value.
*
* @return void
*
* @since 1.0
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Unsets an offset in the iterator.
*
* @param mixed $offset The array offset.
*
* @return void
*
* @since 1.0
*/
public function offsetUnset($offset)
{
$this->set($offset, null);
}
/**
* 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 1.0
*/
public function set($path, $value)
{
$result = null;
/**
* Explode the registry path into an array and remove empty
* nodes that occur as a result of a double dot. ex: joomla..test
* Finally, re-key the array so they are 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 1.0
*/
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 1.0
*/
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 1.0
*/
public function toString($format = 'JSON', $options = array())
{
// Return a namespace in a given format
$handler = AbstractRegistryFormat::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.
* @param boolean $recursive True to support recursive bindData.
*
* @return void
*
* @since 1.0
*/
protected function bindData($parent, $data, $recursive = true)
{
// 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)
{
// Modification from joomla/registry package - Using CMS JArrayHelper versus importing joomla/utilities package
if ($recursive && ((is_array($v) && \JArrayHelper::isAssociative($v)) || is_object($v)))
{
if (!isset($parent->$k))
{
$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 1.0
*/
protected function asArray($data)
{
$array = array();
if (is_object($data))
{
$data = get_object_vars($data);
}
foreach ($data as $k => $v)
{
if (is_object($v) || is_array($v))
{
$array[$k] = $this->asArray($v);
}
else
{
$array[$k] = $v;
}
}
return $array;
}
}
The same content is in 7 releases, Joomla 3.3.0 to 3.3.6. See them all, or every version of this file.