2005-08-21 06:49:02 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Class collections.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* A repository for class objects, each registered with a key.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2006-01-12 02:10:47 +00:00
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.9.2
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Class Collections.
|
|
|
|
*
|
2006-01-12 02:10:47 +00:00
|
|
|
* A repository for class objects, each registered with a key.
|
2005-09-17 02:22:07 +00:00
|
|
|
* If you try to add an object with the same key twice, nothing will come of it.
|
|
|
|
* If you need a second instance of an object, give it another key.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2007-01-31 23:25:05 +00:00
|
|
|
class ClassRegistry {
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Names of classes with their objects.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-09-17 02:22:07 +00:00
|
|
|
* @var array
|
2005-08-21 06:49:02 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2007-02-05 04:53:31 +00:00
|
|
|
var $__objects = array();
|
|
|
|
/**
|
|
|
|
* Names of class names mapped to the object in the registry.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__map = array();
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Return a singleton instance of the ClassRegistry.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @return ClassRegistry instance
|
2007-05-20 06:30:19 +00:00
|
|
|
* @access public
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
|
|
|
if (!$instance) {
|
2007-02-05 04:53:31 +00:00
|
|
|
$instance[0] =& new ClassRegistry();
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
return $instance[0];
|
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Add $object to the registry, associating it with the name $key.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @param string $key Key for the object in registry
|
|
|
|
* @param mixed $object Object to store
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the object was written, false if $key already exists
|
2007-05-20 06:30:19 +00:00
|
|
|
* @access public
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function addObject($key, &$object) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
2006-11-27 01:04:19 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2007-02-05 04:53:31 +00:00
|
|
|
if (array_key_exists($key, $_this->__objects) === false) {
|
|
|
|
$_this->__objects[$key] = &$object;
|
2007-09-20 14:50:42 +00:00
|
|
|
return true;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-09-20 14:50:42 +00:00
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-03-12 00:11:40 +00:00
|
|
|
/**
|
|
|
|
* Remove object which corresponds to given key.
|
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @param string $key Key of object to remove from registry
|
|
|
|
* @access public
|
2006-03-12 00:11:40 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function removeObject($key) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
2006-11-27 01:04:19 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2007-02-05 04:53:31 +00:00
|
|
|
if (array_key_exists($key, $_this->__objects) === true) {
|
|
|
|
unset($_this->__objects[$key]);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Returns true if given key is present in the ClassRegistry.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param string $key Key to look for
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean true if key exists in registry, false otherwise
|
2007-05-20 06:30:19 +00:00
|
|
|
* @access public
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function isKeySet($key) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
2006-11-27 01:04:19 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2007-06-20 06:15:35 +00:00
|
|
|
if (array_key_exists($key, $_this->__objects)) {
|
2007-02-05 04:53:31 +00:00
|
|
|
return true;
|
|
|
|
} elseif (array_key_exists($key, $_this->__map)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-04-27 20:16:38 +00:00
|
|
|
/**
|
|
|
|
* Get all keys from the regisrty.
|
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @return array Set of keys stored in registry
|
|
|
|
* @access public
|
2007-04-27 20:16:38 +00:00
|
|
|
*/
|
|
|
|
function keys() {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
return array_keys($_this->__objects);
|
|
|
|
}
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-09-17 02:22:07 +00:00
|
|
|
* Return object which corresponds to given key.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @param string $key Key of object to look for
|
|
|
|
* @return mixed Object stored in registry
|
|
|
|
* @access public
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function &getObject($key) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
2006-11-27 01:04:19 +00:00
|
|
|
$key = Inflector::underscore($key);
|
2007-03-16 23:54:24 +00:00
|
|
|
|
2007-06-20 07:51:52 +00:00
|
|
|
if (isset($_this->__objects[$key])) {
|
2007-02-05 04:53:31 +00:00
|
|
|
return $_this->__objects[$key];
|
|
|
|
} else {
|
|
|
|
$key = $_this->__getMap($key);
|
2007-06-20 07:51:52 +00:00
|
|
|
if (isset($_this->__objects[$key])) {
|
2007-02-05 04:53:31 +00:00
|
|
|
return $_this->__objects[$key];
|
|
|
|
}
|
|
|
|
}
|
2007-05-20 06:30:19 +00:00
|
|
|
|
2007-03-16 23:54:24 +00:00
|
|
|
$return = false;
|
|
|
|
return $return;
|
2007-02-05 04:53:31 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Add a key name pair to the registry to map name to class in the regisrty.
|
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @param string $key Key to include in map
|
|
|
|
* @param string $name Key that is being mapped
|
|
|
|
* @access public
|
2007-02-05 04:53:31 +00:00
|
|
|
*/
|
2007-03-16 23:54:24 +00:00
|
|
|
function map($key, $name) {
|
2007-02-05 04:53:31 +00:00
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$key = Inflector::underscore($key);
|
|
|
|
$name = Inflector::underscore($name);
|
|
|
|
if (array_key_exists($key, $_this->__map) === false) {
|
|
|
|
$_this->__map[$key] = $name;
|
|
|
|
}
|
|
|
|
}
|
2007-04-27 20:16:38 +00:00
|
|
|
/**
|
|
|
|
* Get all keys from the map in the regisrty.
|
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @return array Keys of registry's map
|
|
|
|
* @access public
|
2007-04-27 20:16:38 +00:00
|
|
|
*/
|
|
|
|
function mapKeys() {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
return array_keys($_this->__map);
|
|
|
|
}
|
2007-02-05 04:53:31 +00:00
|
|
|
/**
|
|
|
|
* Return the name of a class in the registry.
|
|
|
|
*
|
2007-05-20 06:30:19 +00:00
|
|
|
* @param string $key Key to find in map
|
|
|
|
* @return string Mapped value
|
|
|
|
* @access private
|
2007-02-05 04:53:31 +00:00
|
|
|
*/
|
2007-03-16 23:54:24 +00:00
|
|
|
function __getMap($key) {
|
2007-02-05 04:53:31 +00:00
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
if (array_key_exists($key, $_this->__map)) {
|
|
|
|
return $_this->__map[$key];
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-10-22 20:58:55 +00:00
|
|
|
/**
|
|
|
|
* Flushes all objects from the ClassREgistry.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function flush() {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$_this->__objects = array();
|
|
|
|
$_this->__map = array();
|
|
|
|
}
|
2005-09-17 02:22:07 +00:00
|
|
|
}
|
2007-05-03 04:35:25 +00:00
|
|
|
?>
|