2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class collections.
|
|
|
|
*
|
|
|
|
* A repository for class objects, each registered with a key.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-31 02:46:14 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-31 02:46:14 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP(tm) v 0.9.2
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class ClassRegistry {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Names of classes with their objects.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__objects = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Names of class names mapped to the object in the registry.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__map = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 15:37:26 +00:00
|
|
|
/**
|
|
|
|
* Default constructor parameter settings, indexed by type
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2008-07-30 19:53:12 +00:00
|
|
|
var $__config = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return a singleton instance of the ClassRegistry.
|
|
|
|
*
|
|
|
|
* @return ClassRegistry instance
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
|
|
|
if (!$instance) {
|
|
|
|
$instance[0] =& new ClassRegistry();
|
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
return $instance[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-04-03 14:50:05 +00:00
|
|
|
* Loads a class, registers the object in the registry and returns instance of the object. ClassRegistry::init()
|
|
|
|
* is used as a factory for models, and handle correct injecting of settings, that assist in testing.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* Examples
|
|
|
|
* Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2011-04-03 14:50:05 +00:00
|
|
|
* Exapanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-02-04 05:00:59 +00:00
|
|
|
* Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2008-09-30 18:59:17 +00:00
|
|
|
* When $class is a numeric keyed array, multiple class instances will be stored in the registry,
|
2009-02-04 05:00:59 +00:00
|
|
|
* no instance of the object will be returned
|
|
|
|
* {{{
|
|
|
|
* array(
|
2011-04-03 14:50:05 +00:00
|
|
|
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
|
|
|
|
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
|
|
|
|
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model')
|
2009-02-04 05:00:59 +00:00
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
* @param mixed $class as a string or a single key => value array instance will be created,
|
|
|
|
* stored in the registry and returned.
|
2011-04-03 14:50:05 +00:00
|
|
|
* @param string $type Only model is accepted as a valid value for $type.
|
2009-02-04 05:00:59 +00:00
|
|
|
* @return object instance of ClassName
|
2008-09-25 16:49:56 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function &init($class, $type = null) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$id = $false = false;
|
|
|
|
$true = true;
|
2008-10-12 02:36:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$type) {
|
|
|
|
$type = 'Model';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($class)) {
|
|
|
|
$objects = $class;
|
|
|
|
if (!isset($class[0])) {
|
|
|
|
$objects = array($class);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$objects = array(array('class' => $class));
|
|
|
|
}
|
2008-08-25 22:33:36 +00:00
|
|
|
$defaults = isset($_this->__config[$type]) ? $_this->__config[$type] : array();
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = count($objects);
|
2008-10-12 02:36:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($objects as $key => $settings) {
|
|
|
|
if (is_array($settings)) {
|
2009-11-16 00:55:20 +00:00
|
|
|
$pluginPath = null;
|
2008-07-30 15:37:26 +00:00
|
|
|
$settings = array_merge($defaults, $settings);
|
2008-08-25 22:33:36 +00:00
|
|
|
$class = $settings['class'];
|
2009-11-16 00:55:20 +00:00
|
|
|
|
|
|
|
list($plugin, $class) = pluginSplit($class);
|
|
|
|
if ($plugin) {
|
2008-09-24 13:37:06 +00:00
|
|
|
$pluginPath = $plugin . '.';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-25 22:33:36 +00:00
|
|
|
if (empty($settings['alias'])) {
|
|
|
|
$settings['alias'] = $class;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-08-25 22:33:36 +00:00
|
|
|
$alias = $settings['alias'];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-12 02:36:39 +00:00
|
|
|
if ($model =& $_this->__duplicate($alias, $class)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_this->map($alias, $class);
|
2008-08-07 15:36:26 +00:00
|
|
|
return $model;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 13:37:06 +00:00
|
|
|
if (class_exists($class) || App::import($type, $pluginPath . $class)) {
|
2008-08-25 22:33:36 +00:00
|
|
|
${$class} =& new $class($settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($type === 'Model') {
|
2008-09-24 02:12:35 +00:00
|
|
|
if ($plugin && class_exists($plugin . 'AppModel')) {
|
|
|
|
$appModel = $plugin . 'AppModel';
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$appModel = 'AppModel';
|
|
|
|
}
|
2008-09-12 05:11:34 +00:00
|
|
|
$settings['name'] = $class;
|
|
|
|
${$class} =& new $appModel($settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset(${$class})) {
|
|
|
|
trigger_error(sprintf(__('(ClassRegistry::init() could not create instance of %1$s class %2$s ', true), $class, $type), E_USER_WARNING);
|
|
|
|
return $false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type !== 'Model') {
|
|
|
|
$_this->addObject($alias, ${$class});
|
|
|
|
} else {
|
|
|
|
$_this->map($alias, $class);
|
|
|
|
}
|
|
|
|
} elseif (is_numeric($settings)) {
|
|
|
|
trigger_error(__('(ClassRegistry::init() Attempted to create instance of a class with a numeric name', true), E_USER_WARNING);
|
|
|
|
return $false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($count > 1) {
|
|
|
|
return $true;
|
|
|
|
}
|
|
|
|
return ${$class};
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Add $object to the registry, associating it with the name $key.
|
|
|
|
*
|
|
|
|
* @param string $key Key for the object in registry
|
|
|
|
* @param mixed $object Object to store
|
|
|
|
* @return boolean True if the object was written, false if $key already exists
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function addObject($key, &$object) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$key = Inflector::underscore($key);
|
2008-10-12 02:36:39 +00:00
|
|
|
if (!isset($_this->__objects[$key])) {
|
2008-08-27 16:43:15 +00:00
|
|
|
$_this->__objects[$key] =& $object;
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Remove object which corresponds to given key.
|
|
|
|
*
|
|
|
|
* @param string $key Key of object to remove from registry
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function removeObject($key) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$key = Inflector::underscore($key);
|
2008-10-12 02:36:39 +00:00
|
|
|
if (isset($_this->__objects[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($_this->__objects[$key]);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if given key is present in the ClassRegistry.
|
|
|
|
*
|
|
|
|
* @param string $key Key to look for
|
|
|
|
* @return boolean true if key exists in registry, false otherwise
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function isKeySet($key) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$key = Inflector::underscore($key);
|
2008-10-12 02:36:39 +00:00
|
|
|
if (isset($_this->__objects[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
2008-10-12 02:36:39 +00:00
|
|
|
} elseif (isset($_this->__map[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-09-08 16:58:39 +00:00
|
|
|
* Get all keys from the registry.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array Set of keys stored in registry
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function keys() {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
return array_keys($_this->__objects);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return object which corresponds to given key.
|
|
|
|
*
|
|
|
|
* @param string $key Key of object to look for
|
2009-11-30 14:18:24 +00:00
|
|
|
* @return mixed Object stored in registry or boolean false if the object does not exist.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function &getObject($key) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$key = Inflector::underscore($key);
|
2008-08-07 18:53:05 +00:00
|
|
|
$return = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($_this->__objects[$key])) {
|
2008-08-07 18:53:05 +00:00
|
|
|
$return =& $_this->__objects[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$key = $_this->__getMap($key);
|
|
|
|
if (isset($_this->__objects[$key])) {
|
2008-08-07 18:53:05 +00:00
|
|
|
$return =& $_this->__objects[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-25 16:49:56 +00:00
|
|
|
/**
|
2008-07-30 15:37:26 +00:00
|
|
|
* Sets the default constructor parameter for an object type
|
|
|
|
*
|
|
|
|
* @param string $type Type of object. If this parameter is omitted, defaults to "Model"
|
|
|
|
* @param array $param The parameter that will be passed to object constructors when objects
|
|
|
|
* of $type are created
|
|
|
|
* @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns
|
|
|
|
* the previously-set value of $param, or null if not set.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2008-07-30 15:37:26 +00:00
|
|
|
*/
|
2008-07-30 19:53:12 +00:00
|
|
|
function config($type, $param = array()) {
|
2008-07-30 15:37:26 +00:00
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
|
|
|
|
if (empty($param) && is_array($type)) {
|
|
|
|
$param = $type;
|
|
|
|
$type = 'Model';
|
|
|
|
} elseif (is_null($param)) {
|
2008-07-30 19:53:12 +00:00
|
|
|
unset($_this->__config[$type]);
|
2008-07-30 15:37:26 +00:00
|
|
|
} elseif (empty($param) && is_string($type)) {
|
2008-07-30 19:53:12 +00:00
|
|
|
return isset($_this->__config[$type]) ? $_this->__config[$type] : null;
|
2008-07-30 15:37:26 +00:00
|
|
|
}
|
2008-07-30 19:53:12 +00:00
|
|
|
$_this->__config[$type] = $param;
|
2008-07-30 15:37:26 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if $alias is a duplicate $class Object
|
|
|
|
*
|
|
|
|
* @param string $alias
|
|
|
|
* @param string $class
|
|
|
|
* @return boolean
|
2008-10-12 02:36:39 +00:00
|
|
|
* @access private
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-10-12 02:36:39 +00:00
|
|
|
function &__duplicate($alias, $class) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$duplicate = false;
|
2008-10-12 02:36:39 +00:00
|
|
|
if ($this->isKeySet($alias)) {
|
|
|
|
$model =& $this->getObject($alias);
|
|
|
|
if (is_object($model) && (is_a($model, $class) || $model->alias === $class)) {
|
2008-08-07 15:36:26 +00:00
|
|
|
$duplicate =& $model;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
unset($model);
|
|
|
|
}
|
|
|
|
return $duplicate;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-07 15:36:26 +00:00
|
|
|
* Add a key name pair to the registry to map name to class in the registry.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $key Key to include in map
|
|
|
|
* @param string $name Key that is being mapped
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function map($key, $name) {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$key = Inflector::underscore($key);
|
|
|
|
$name = Inflector::underscore($name);
|
2008-10-12 02:36:39 +00:00
|
|
|
if (!isset($_this->__map[$key])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_this->__map[$key] = $name;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-15 17:11:54 +00:00
|
|
|
* Get all keys from the map in the registry.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return array Keys of registry's map
|
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function mapKeys() {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
return array_keys($_this->__map);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return the name of a class in the registry.
|
|
|
|
*
|
|
|
|
* @param string $key Key to find in map
|
|
|
|
* @return string Mapped value
|
|
|
|
* @access private
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function __getMap($key) {
|
2008-10-12 02:36:39 +00:00
|
|
|
if (isset($this->__map[$key])) {
|
|
|
|
return $this->__map[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Flushes all objects from the ClassRegistry.
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2008-09-25 16:49:56 +00:00
|
|
|
* @static
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function flush() {
|
|
|
|
$_this =& ClassRegistry::getInstance();
|
|
|
|
$_this->__objects = array();
|
|
|
|
$_this->__map = array();
|
|
|
|
}
|
|
|
|
}
|