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
|
<?php
/* SVN FILE: $Id: class_registry.php 7691 2008-10-02 04:59:12Z nate $ */
/**
* Class collections.
*
* A repository for class objects, each registered with a key.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 0.9.2
* @version $Revision: 7691 $
* @modifiedby $LastChangedBy: nate $
* @lastmodified $Date: 2008-10-02 00:59:12 -0400 (Thu, 02 Oct 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Class Collections.
*
* A repository for class objects, each registered with a key.
* 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.
*
* @package cake
* @subpackage cake.cake.libs
*/
class ClassRegistry{
/**
* Names of classes with their objects.
*
* @var array
* @access private
*/
var $_objects = array();
/**
* Return a singleton instance of the ClassRegistry.
*
* @return ClassRegistry instance
*/
function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = &new ClassRegistry;
}
return $instance[0];
}
/**
* Add $object to the registry, associating it with the name $key.
*
* @param string $key
* @param mixed $object
*/
function addObject($key, &$object) {
$_this =& ClassRegistry::getInstance();
$key = Inflector::underscore($key);
if (array_key_exists($key, $_this->_objects) === false) {
$_this->_objects[$key] = &$object;
}
}
/**
* Remove object which corresponds to given key.
*
* @param string $key
* @return void
*/
function removeObject($key) {
$_this =& ClassRegistry::getInstance();
$key = Inflector::underscore($key);
if (array_key_exists($key, $_this->_objects) === true) {
unset($_this->_objects[$key]);
}
}
/**
* Returns true if given key is present in the ClassRegistry.
*
* @param string $key Key to look for
* @return boolean Success
*/
function isKeySet($key) {
$_this =& ClassRegistry::getInstance();
$key = Inflector::underscore($key);
return array_key_exists($key, $_this->_objects);
}
/**
* Get all keys from the regisrty.
*
* @return array
*/
function keys() {
$_this =& ClassRegistry::getInstance();
return array_keys($_this->_objects);
}
/**
* Return object which corresponds to given key.
*
* @param string $key
* @return mixed
*/
function &getObject($key) {
$_this =& ClassRegistry::getInstance();
$key = Inflector::underscore($key);
return $_this->_objects[$key];
}
}
?>
|