<?php
/**
* Registry class
*
* $Id: Registry.php 157 2011-02-10 06:06:24Z indy $
*
* $LastChangedBy: indy $
*
* $LastChangedDate: 2011-02-10 08:06:24 +0200 (Thu, 10 Feb 2011) $
*
* $Rev: 157 $
*
* @author Indiana Jones <hide@address.com>
* @version 1.0
* @copyright 2010 PhpIndex
* @package PhpIndex
*/
/**
* Generic storage class helps to manage global data.
*/
class PhpIndex_Registry extends ArrayObject
{
/**
* Registry object provides storage for shared objects.
*
* @var PhpIndex_Registry
*/
protected static $_registry = null;
/**
* Constructs a parent ArrayObject with default
* ARRAY_AS_PROPS to allow acces as an object
*
* @param array $array data array
*/
public function __construct($array = array())
{
parent::__construct($array, parent::ARRAY_AS_PROPS);
}
/**
* Retrieves the default registry instance.
*
* @return PhpIndex_Registry
*/
public static function getInstance()
{
if (is_null(self::$_registry)) {
self::_init();
}
return self::$_registry;
}
/**
* Set the default registry instance to a specified instance.
*
* @param PhpIndex_Registry $registry An object instance of type PhpIndex_Registry,
* or a subclass.
* @return void
* @throws Exception if registry is already initialized.
*/
public static function setInstance(PhpIndex_Registry $registry)
{
if (!is_null(self::$_registry)) {
throw new Exception('Registry is already initialized');
}
self::$_registry = $registry;
}
/**
* getter method, basically same as offsetGet().
*
* This method can be called from an object of type PhpIndex_Registry, or it
* can be called statically. In the latter case, it uses the default
* static instance stored in the class.
*
* @param string $index - get the value associated with $index
* @return mixed
* @throws Exception if no entry is registerd for $index.
*/
public static function get($index)
{
$instance = self::getInstance();
if (!$instance->offsetExists($index)) {
throw new Exception("No entry is registered for key '" . $index . "'");
}
return $instance->offsetGet($index);
}
/**
* setter method, basically same as offsetSet().
*
* This method can be called from an object of type PhpIndex_Registry, or it
* can be called statically. In the latter case, it uses the default
* static instance stored in the class.
*
* @param string $index The location in the ArrayObject in which to store
* the value.
* @param mixed $value The object to store in the ArrayObject.
* @return void
*/
public static function set($index, $value)
{
$instance = self::getInstance();
$instance->offsetSet($index, $value);
}
/**
* Returns TRUE if the $index is a named value in the registry,
* or FALSE if $index was not found in the registry.
*
* @param string $index
* @return boolean
*/
public static function isRegistered($index)
{
if (self::$_registry === null) {
return false;
}
return self::$_registry->offsetExists($index);
}
/**
* Check if an offset exists
*
* @param string $index
* @returns mixed
*/
public function offsetExists($index)
{
return array_key_exists($index, $this);
}
/**
* Initialize the default registry instance.
*
* @return void
*/
protected static function _init()
{
self::setInstance(new PhpIndex_Registry());
}
}
/* EOF */