5 * A repository for class objects, each registered with a key.
9 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12 * Licensed under The MIT License
13 * Redistributions of files must retain the above copyright notice.
15 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16 * @link http://cakephp.org CakePHP(tm) Project
18 * @subpackage cake.cake.libs
19 * @since CakePHP(tm) v 0.9.2
20 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
26 * A repository for class objects, each registered with a key.
27 * If you try to add an object with the same key twice, nothing will come of it.
28 * If you need a second instance of an object, give it another key.
31 * @subpackage cake.cake.libs
36 * Names of classes with their objects.
41 var $__objects = array();
44 * Names of class names mapped to the object in the registry.
52 * Default constructor parameter settings, indexed by type
57 var $__config = array();
60 * Return a singleton instance of the ClassRegistry.
62 * @return ClassRegistry instance
65 function &getInstance() {
66 static $instance = array();
68 $instance[0] =& new ClassRegistry();
74 * Loads a class, registers the object in the registry and returns instance of the object. ClassRegistry::init()
75 * is used as a factory for models, and handle correct injecting of settings, that assist in testing.
78 * Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
80 * Exapanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
82 * Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
84 * When $class is a numeric keyed array, multiple class instances will be stored in the registry,
85 * no instance of the object will be returned
88 * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
89 * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
90 * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model')
93 * @param mixed $class as a string or a single key => value array instance will be created,
94 * stored in the registry and returned.
95 * @param string $type Only model is accepted as a valid value for $type.
96 * @return object instance of ClassName
100 function &init($class, $type = null) {
101 $_this =& ClassRegistry
::getInstance();
102 $id = $false = false;
109 if (is_array($class)) {
111 if (!isset($class[0])) {
112 $objects = array($class);
115 $objects = array(array('class' => $class));
117 $defaults = isset($_this->__config
[$type]) ?
$_this->__config
[$type] : array();
118 $count = count($objects);
120 foreach ($objects as $key => $settings) {
121 if (is_array($settings)) {
123 $settings = array_merge($defaults, $settings);
124 $class = $settings['class'];
126 list($plugin, $class) = pluginSplit($class);
128 $pluginPath = $plugin . '.';
131 if (empty($settings['alias'])) {
132 $settings['alias'] = $class;
134 $alias = $settings['alias'];
136 if ($model =& $_this->__duplicate($alias, $class)) {
137 $_this->map($alias, $class);
141 if (class_exists($class) || App
::import($type, $pluginPath . $class)) {
142 $
{$class} =& new $class($settings);
143 } elseif ($type === 'Model') {
144 if ($plugin && class_exists($plugin . 'AppModel')) {
145 $appModel = $plugin . 'AppModel';
147 $appModel = 'AppModel';
149 $settings['name'] = $class;
150 $
{$class} =& new $appModel($settings);
153 if (!isset($
{$class})) {
154 trigger_error(sprintf(__('(ClassRegistry::init() could not create instance of %1$s class %2$s ', true), $class, $type), E_USER_WARNING
);
158 if ($type !== 'Model') {
159 $_this->addObject($alias, $
{$class});
161 $_this->map($alias, $class);
163 } elseif (is_numeric($settings)) {
164 trigger_error(__('(ClassRegistry::init() Attempted to create instance of a class with a numeric name', true), E_USER_WARNING
);
176 * Add $object to the registry, associating it with the name $key.
178 * @param string $key Key for the object in registry
179 * @param mixed $object Object to store
180 * @return boolean True if the object was written, false if $key already exists
184 function addObject($key, &$object) {
185 $_this =& ClassRegistry
::getInstance();
186 $key = Inflector
::underscore($key);
187 if (!isset($_this->__objects
[$key])) {
188 $_this->__objects
[$key] =& $object;
195 * Remove object which corresponds to given key.
197 * @param string $key Key of object to remove from registry
202 function removeObject($key) {
203 $_this =& ClassRegistry
::getInstance();
204 $key = Inflector
::underscore($key);
205 if (isset($_this->__objects
[$key])) {
206 unset($_this->__objects
[$key]);
211 * Returns true if given key is present in the ClassRegistry.
213 * @param string $key Key to look for
214 * @return boolean true if key exists in registry, false otherwise
218 function isKeySet($key) {
219 $_this =& ClassRegistry
::getInstance();
220 $key = Inflector
::underscore($key);
221 if (isset($_this->__objects
[$key])) {
223 } elseif (isset($_this->__map
[$key])) {
230 * Get all keys from the registry.
232 * @return array Set of keys stored in registry
237 $_this =& ClassRegistry
::getInstance();
238 return array_keys($_this->__objects
);
242 * Return object which corresponds to given key.
244 * @param string $key Key of object to look for
245 * @return mixed Object stored in registry or boolean false if the object does not exist.
249 function &getObject($key) {
250 $_this =& ClassRegistry
::getInstance();
251 $key = Inflector
::underscore($key);
253 if (isset($_this->__objects
[$key])) {
254 $return =& $_this->__objects
[$key];
256 $key = $_this->__getMap($key);
257 if (isset($_this->__objects
[$key])) {
258 $return =& $_this->__objects
[$key];
265 * Sets the default constructor parameter for an object type
267 * @param string $type Type of object. If this parameter is omitted, defaults to "Model"
268 * @param array $param The parameter that will be passed to object constructors when objects
269 * of $type are created
270 * @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns
271 * the previously-set value of $param, or null if not set.
275 function config($type, $param = array()) {
276 $_this =& ClassRegistry
::getInstance();
278 if (empty($param) && is_array($type)) {
281 } elseif (is_null($param)) {
282 unset($_this->__config
[$type]);
283 } elseif (empty($param) && is_string($type)) {
284 return isset($_this->__config
[$type]) ?
$_this->__config
[$type] : null;
286 $_this->__config
[$type] = $param;
290 * Checks to see if $alias is a duplicate $class Object
292 * @param string $alias
293 * @param string $class
298 function &__duplicate($alias, $class) {
300 if ($this->isKeySet($alias)) {
301 $model =& $this->getObject($alias);
302 if (is_object($model) && (is_a($model, $class) ||
$model->alias
=== $class)) {
303 $duplicate =& $model;
311 * Add a key name pair to the registry to map name to class in the registry.
313 * @param string $key Key to include in map
314 * @param string $name Key that is being mapped
318 function map($key, $name) {
319 $_this =& ClassRegistry
::getInstance();
320 $key = Inflector
::underscore($key);
321 $name = Inflector
::underscore($name);
322 if (!isset($_this->__map
[$key])) {
323 $_this->__map
[$key] = $name;
328 * Get all keys from the map in the registry.
330 * @return array Keys of registry's map
335 $_this =& ClassRegistry
::getInstance();
336 return array_keys($_this->__map
);
340 * Return the name of a class in the registry.
342 * @param string $key Key to find in map
343 * @return string Mapped value
347 function __getMap($key) {
348 if (isset($this->__map
[$key])) {
349 return $this->__map
[$key];
354 * Flushes all objects from the ClassRegistry.
361 $_this =& ClassRegistry
::getInstance();
362 $_this->__objects
= array();
363 $_this->__map
= array();